0%

【SpringCloud】微服务架构编码构建

简单搭建一个微服务框架,进而了解微服务

1. 父工程构建

    1. 构建总父工程(Maven项目)
      • 选择maven-archetyp-site
        • 注意:选择Maven选择自己的Maven版本
    1. 注解生效激活
      • setting -> Build, Execution, Deployment -> Compiler -> Annotation Processors
      • 勾选Enable annotation processing
    1. File Type过滤(过滤多余文件)
      • setting -> Editor -> File Types
      • Ignore Files and Folders中添加 *.idea;*.iml;

2. 修改pom.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<groupId>com.letere.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<!--统一管理jar包和版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.18</mysql.version>
<druid.verison>1.1.16</druid.verison>
<mybatis.spring.boot.verison>1.3.0</mybatis.spring.boot.verison>
</properties>

<!--子模块继承之后,提供作用:锁定版本+子module不用谢groupId和version-->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.verison}</version>
</dependency>
<!-- mybatis-springboot整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.verison}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>

3. 知识补充

  • dependenciesManagement:用来锁定子模块依赖的版本,子模块写依赖时可以省略版本号,而且此并不会引入依赖
  • Maven跳过单元测试:IDEA -> 右侧栏maven -> 圆形闪电

4. 支付模块构建

目的:用户端口为80,去引用8001端口的支付功能

4.1 微服务模块

  • (1)建module
  • (2)改POM
  • (3)写YML
  • (4)主启动
  • (5)业务类
  • 以上5步总结为:新建一个SpringBoot项目

4.2 新建module

4.3 改POM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  <dependencies>
<!-- spring boot网络服务 -->
<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>
<!-- mybatis与springboot整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 快速生成JavaBean注解 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- SpringBoot单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

4.4 写YML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server:
port: 8001 #端口

spring:
application:
name: cloud-payment-service #服务名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver #mysql驱动
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123

mybatis:
mapper-locations: classpath:mapper/*.xml #mapper文件的位置
type-aliases-package: com.letere.springcloud.entities #所有Entity别名类所在包

4.5 创建主启动类

1
2
3
4
5
6
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}

4.6 业务类

  • (1)数据库表
1
2
3
4
5
CREATE TABLE `payment` (
`id` bigint NOT NULL AUTO_INCREMENT,
`serial` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • (2)创建实体类
1
2
3
4
5
6
7
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable { //序列化:分布式部署需要
private Long id;
private String serial;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
//结果集
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
private Integer code;
private String message;
private T data;

public CommonResult(Integer code, String message) {
this(code, message, null);
}
}
  • (3)创建DAO接口
1
2
3
4
5
6
@Mapper
public interface PaymentDao {
public int create(Payment payment);

public Payment getPaymentById(Long id);
}
  • (4)创建Mapper文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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.letere.springcloud.dao.PaymentDao">

<resultMap id="PaymentMap" type="com.letere.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>

<select id="getPaymentById" parameterType="Payment" resultMap="PaymentMap"> <!--配置了别名类-->
select *
from `payment`
where id = #{id}
</select>

<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> <!--插入成功返回数据-->
insert into `payment`(serial)
values(#{servial})
</insert>
</mapper>
  • (5)Service层
1
2
3
4
5
public interface PaymentService {
public int create(Payment payment);

public Payment getPaymentById(Long id);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Service
public class PaymentServiceImp implements PaymentService{
@Resource
private PaymentDao paymentDao;

@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}

@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
  • (6)Controller层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;

@PostMapping("/payment/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("****插入结果:" + result);
if(result > 0) {
return new CommonResult(200, "插入成功", null);
}
return new CommonResult(400, "插入失败", null);

}

@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("****查询结果:" + payment);
if(payment != null ) {
return new CommonResult(200, "查询成功", payment);
}
return new CommonResult(400, "查询失败", null);
}
}


5. 消费者模块

消费者模块 与 支付模块差不多,所以重复的地方不说了

5.1 依赖引入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependencies>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

5.2 写YML

1
2
server:
port: 81

5.3 实体类

  • 和 支付模块一样的两个类

5.4 注入RestTemplate

  • RestTemplate是Spring用来模拟客户端来发送请求
1
2
3
4
5
6
7
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getTemplate() {
return new RestTemplate();
}
}

5.5 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RestController
@Slf4j
public class OrderController {

private static final String PAYMENT_URL = "http://localhost:8001";

@Autowired
private RestTemplate restTemplate;

@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
//模拟客户端发送Post请求
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class); //参数:1、请求地址, 2、请求参数, 3、返回结果类型
}

@GetMapping("/consumer/payment/get/{id}")
public CommonResult getPayment(@PathVariable("id") Long id) {
//模拟客户端发送Get请求
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); //参数:1、请求地址, 2、返回结果类型
}
}

6. 工程重构

6.1 新建共用工程

  • 创建Maven项目:cloud-api-commons

6.2 修改POM

  • 引入依赖
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <!-- SpringBoot热部署 -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
    </dependency>
    <!-- 快速创建JavaBean -->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
    <optional>true</optional>
    </dependency>
    <!-- hutool工具包 -->
    <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.7</version>
    </dependency>

6.3 install项目

6.4 引入依赖

  • 其他项目引入此公共项目依赖即可
1
2
3
4
5
<dependency>
<groupId>com.letere.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>