IDEA 2022 jdk 1.8 所学的链接:springCloud — 初级篇(1) 内容更多
没有其他废话,就直接开始写项目
需要先创建一个数据库 cloudtest
create database cloudtest;
use cloudtest;
create table payment(
id bigint(20) primary key auto_increment,
serial varchar(200)
)character set utf8;
创建 SpringCloudDemo 项目
创建一个 Maven 项目,选中 Maven 直接点击下一步
项目名 SpringCloudDemo
点击 finish 进行创建
点击 pom.xml 文件,添加以下依赖
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<lombok.version>1.18.10</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.1.46</mysql.version>
<druid.version>1.2.6</druid.version>
<mybatis.spring.boot.version>1.3.2</mybatis.spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>{mysql.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>{druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>{mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>{junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
默认创建的 src 文件夹可以直接删除了,当前示例用不到
添加一个模块,右键选中项目文件夹 – New – Module…,选中 Maven,点击下一步,
创建cloud-api-commons模块
存放一些公共功能的类
pom 文件中添加依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
创建实体类 Payment
package com.zcq.springcloud.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
创建一个 JSON 工具类,用于返回响应的数据
package com.zcq.springcloud.util;
import lombok.Data;
@Data
public class JsonResult<T> {
private Integer code;
private String message;
private T data;
public JsonResult(Integer code, String message) {
this(code, message, null);
}
public JsonResult(Integer code, String message, T data) {
super();
this.code = code;
this.message = message;
this.data = data;
}
public JsonResult() {
super();
}
}
目录结构如下
创建 cloud-provider-payment8001 模块
创建模块步骤和上面创建 cloud-api-commons 模块的步骤一样,这个模块名设为 cloud-provider-payment8001,这个模块充当支付服务
pom 依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<springboot.version>2.6.5</springboot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>{springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator </artifactId>
<version>{springboot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>{springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<version>{springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>{springboot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>{springboot.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
Dao 层的 Mapper
package com.zcq.springcloud.mapper;
import com.zcq.springcloud.entity.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param(("id")) Long id);
}
Service 层
PaymentService
package com.zcq.springcloud.service;
import com.zcq.springcloud.entity.Payment;
import org.apache.ibatis.annotations.Param;
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param(("id")) Long id);
}
PaymentServiceImpl
package com.zcq.springcloud.service.impl;
import com.zcq.springcloud.entity.Payment;
import com.zcq.springcloud.mapper.PaymentDao;
import com.zcq.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
Controller 层
package com.zcq.springcloud.controller;
import com.zcq.springcloud.entity.Payment;
import com.zcq.springcloud.service.PaymentService;
import com.zcq.springcloud.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
@PostMapping("/create")
public JsonResult<Payment> create(@RequestBody Payment payment) {
final int result = paymentService.create(payment);
log.info("-- 插入结果:" + result);
if (result > 0) {
return new JsonResult<>(200, "success", null);
} else {
return new JsonResult<>(400, "fail", null);
}
}
@GetMapping("/{id}")
public JsonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
final Payment result = paymentService.getPaymentById(id);
log.info("-- 执行操作:");
if (result != null) {
return new JsonResult<>(200, "success", result);
} else {
return new JsonResult<>(400, "fail", null);
}
}
}
添加对应的 mapper xml 动态SQL文件,resources 文件夹下创建 mapper 文件夹,创建 PaymentDao.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zcq.springcloud.mapper.PaymentDao">
<resultMap type="com.zcq.springcloud.entity.Payment" id="PaymentMap">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<insert id="create" parameterType="com.zcq.springcloud.entity.Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial)
values (#{serial});
</insert>
<select id="getPaymentById" parameterType="Long" resultMap="PaymentMap">
select *
from payment
where id = #{id};
</select>
</mapper>
resources 文件夹下创建 application.yml 配置文件
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudtest?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zcq.springcloud.entity
创建 SpringBoot 启动类 PaymentMain8001
package com.zcq.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
创建 cloud-consumer-order8002 模块
创建模块步骤和上面创建 cloud-api-commons 模块的步骤一样,这个模块名设为 cloud-consumer-order8002,这个模块充当订单服务
pom 依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator </artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
resources 文件夹下创建 application.yml 配置文件
server:
port: 8002
spring:
application:
name: cloud-order-service
创建配置类
用于配置 RestTemplate
package com.zcq.springcloud.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Java
创建 controller
package com.zcq.springcloud.controller;
import com.zcq.springcloud.entity.Payment;
import com.zcq.springcloud.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/consumer")
public class OrderController {
private static final String PAYMENT_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@SuppressWarnings("unchecked")
@GetMapping("/payment/create")
public JsonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, JsonResult.class);
}
@SuppressWarnings("unchecked")
@GetMapping("/payment/get/{id}")
public JsonResult<Payment> getPayment(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL + "/payment/" + id, JsonResult.class);
}
}
创建 SpringBoot 启动类 OrderMain8002
package com.zcq.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderMain8002 {
public static void main(String[] args) {
SpringApplication.run(OrderMain8002.class, args);
}
}
OK,两个测试的模块创建完成!下面开始进行单元测试
测试
我们可以直接在 cloud-consumer-order8002 模块的 test/java 文件夹下,创建一个 SpringBoot 测试类
package com.zcq.springcloud;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@SpringBootTest
public class OrderMain8002Test {
@Autowired
private WebApplicationContext wac;
private MockMvc mvc;
private MockHttpSession session;
@BeforeEach
public void setupMockMvc() {
System.out.println("-- Before > setupMockMvc()");
mvc = MockMvcBuilders.webAppContextSetup(wac).build();
session = new MockHttpSession();
}
@Test
public void test01() {
String url = "/consumer/payment/create?id=1&serial=zhangsan";
try {
mvc.perform(MockMvcRequestBuilders.get(url)
.session(session))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
;
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行我们刚刚创建的两个模块 PaymentMain8001、OrderMain8002
然后点击运行 OrderMain8002Test 中的 test01() 方法进行测试,执行之后输出执行结果
Java 项目中不存在跨域问题,因为只有跟浏览器有关的内容的时候才会有跨域问题
Eureka 服务
创建 cloud-provider-payment8001 模块
pom 依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
创建 SpringBoot 启动类 EurekaMain7001
package com.zcq.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
创建 application.yml 配置
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: https://${eureka.instance.hostname}:${server.port}/eureka/
然后启动这个项目,访问localhost:7001,可以看到如下画面:
这仅仅是将注册中心eureka的server搭建好了,但是我们并没有把服务注册进去,接下来我们把payment8001注册进eureka中。
将payment8001注册进eureka中:
修改 cloud-provider-payment8001 模块的 pom.xml,添加eureka client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
并添加这个模块的 application.yml 的 eureka 配置
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
对 PaymentMain8001 类添加 @EnableEurekaClient 注解
这样就行了,启动payment8001,再刷新localhost:7001,就会发现eureka中有 payment8001 这个服务了,如下图:
将 cloud-consumer-order8002 注册进 eureka:
修改 cloud-consumer-order8002 模块的 pom.xml 依赖,添加 Eureka 的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
修改这个模块的 application.yml 文件,添加 eureka 配置
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
OrderMain8002 启动类添加 @EnableEurekaClient 注解
把 OrderController 类中的 PAYMENT_URL 变量中的链接修改为 http://CLOUD-PAYMENT-SERVICE
然后启动 OrderMain8002 ,刷新一下 http://localhost:7001/ 即可看到已经添加了服务
给 ApplicationConfig 类中的 getRestTemplate() 方法上添加 @LoadBalanced 注解,不加会报类似如下的错误
I/O error on GET request for “http://CLOUD-PAYMENT-SERVICE/payment/get/31”: CLOUD-PAYMENT-SERVICE; nested exception is java.net.UnknownHostException: CLOUD-PAYMENT-SERVICE] with root cause
然后访问 http://localhost:8002/consumer/payment/get/1 看有没有获取到数据
(内容未完,主要篇幅有点长,可查看 springCloud — 初级篇(1) 这篇文章中下部分可继续跟着做)
|