1.微服务架构理论入门
微服务概念
- 微服务是一种架构风格
- 一个应用拆分为一组小型服务
- 每个服务运行在自己的进程内,也就是可独立部署和升级
- 服务之间使用轻量级HTTP交互
- 服务围绕业务功能拆分
- 可以由全自动部署机制独立部署
- 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术
分布式微服务架构-落地维度
- 服务调用
- 服务降级
- 服务注册与发先
- 服务熔断
- 负载均衡
- 服务消息队列
- 服务网关
- 配置中心管理
- 自动化构建部署
- 服务监控
- 全链路追踪
- 服务定时任务
- 调度操作
Spring Cloud简介
SpringCloud分布式微服务架构的站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶
Spring Cloud技术栈
2.Boot和Cloud版本选型
Spring Boot 2.X 版
- 源码地址
- Spring Boot 2 的新特性
- Spring Boot 官网
- 通过上面官网发现,Boot官方强烈建议你升级到2.X以上版本
Spring Cloud 2021.0.x版
Spring Boot 与 Spring Cloud 兼容性查看
"spring-cloud": { "Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1", "2020.0.6": "Spring Boot >=2.4.0.M1 and <2.6.0-M1", "2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3", "2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1", "2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1", "2021.0.3": "Spring Boot >=2.6.1 and <3.0.0-M1", "2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.0.0-M2", "2022.0.0-M2": "Spring Boot >=3.0.0-M2 and <3.0.0-M3", "2022.0.0-M3": "Spring Boot >=3.0.0-M3 and <3.1.0-M1" }
接下来开发用到的组件版本
- Cloud - 2021.0.3
- Boot - 2.7.1
- Cloud Alibaba - 2021.0.1.2
- Java - Java 8
- Maven - 3.8.4
- MySQL - 5.7.37
3.Cloud组件停更说明
4.父工程Project空间新建
约定 > 配置 > 编码
创建微服务cloud整体聚合父工程Project,有8个关键步骤:
- New Project - maven工程 - create from archetype: maven-archetype-site
- 聚合总父工程名字
- Maven选版本
- 工程名字
- 字符编码 - Settings - File encoding
- 注解生效激活 - Settings - Annotation Processors
- Java编译版本选8
- File Type过滤 - Settings - File Type
5.父工程pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pers.itqiqi</groupId>
<artifactId>springcloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-study</name>
<description>springcloud-study</description>
<packaging>pom</packaging>
<modules>
<module>cloud-provider-payment8001</module>
<module>cloud-admin12000</module>
<module>cloud-consumer-order80</module>
<module>cloud-eureka-server7001</module>
<module>cloud-eureka-server7002</module>
<module>cloud-eureka-server7003</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.7.1</spring.boot.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-boot-admin.version>2.7.1</spring-boot-admin.version>
<mybatis-plus.version>3.5.2</mybatis-plus.version>
</properties>
<dependencies>
<dependency>
<groupId>pers.itqiqi.springcloud-study</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</exclude>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
6.创建spring-boot-admin服务监控
建名为cloud-admin12000的Maven工程
改POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>springcloud-study</artifactId> <groupId>pers.itqiqi</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>cloud-admin12000</artifactId> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> </dependencies> </project>
改application.yml
server: port: 12000 spring: application: name: cloud-admin12000
在主启动类添加注解
@EnableAdminServer
7.支付模块构建
创建微服务模块套路:
- 建Module
- 改POM
- 写YML
- 主启动
- 业务类
sql
CREATE TABLE `payment`( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', `serial` varchar(200) DEFAULT '', PRIMARY KEY (id) )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
建名为cloud-provider-payment8001的Maven工程
改POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>springcloud-study</artifactId> <groupId>pers.itqiqi</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>cloud-provider-payment8001</artifactId> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> </dependencies> </project>
创建Payment实体类
@Data @AllArgsConstructor @NoArgsConstructor @TableName("payment") public class Payment { @TableId private Long id; @TableField("serial") private String serial; }
创建CommonResult实体类
@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult { private Integer code; private String msg; private Object data; public CommonResult(Integer code, String msg) { this(code, msg, null); } }
创建dao层
@Mapper public interface PaymentDao extends BaseMapper<Payment> {}
创建service层
public interface PaymentService extends IService<Payment> {}
@Service public class PaymentServiceImpl extends ServiceImpl<PaymentDao, Payment> implements PaymentService {}
编写controller层
@RestController @Slf4j public class PaymentController { @Autowired private PaymentService paymentService; /** * 添加payment记录 * @param payment payment的json * @return 返回公共返回json */ @PostMapping("/payment/add") public CommonResult addPayment(@RequestBody Payment payment) { boolean save = paymentService.save(payment); log.info("payment 是否添加成功:{}",save); if (save) { return new CommonResult(200, "添加成功!", payment); } else { return new CommonResult(401, "添加失败!", payment); } } /** * 根据id查询单条payment * @param id payment的id值 * @return 返回公共返回json */ @GetMapping("/payment/get/{id}") public CommonResult getPayment(@PathVariable Long id) { Payment payment = paymentService.getById(id); log.info("payment 是否查询成功:{}", payment); if (payment != null) { return new CommonResult(200, "查询成功!", payment); } else { return new CommonResult(401, "查询失败!"); } } }
8.消费者订单模块
创建名为cloud-consumer-order80的maven工程
改POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>springcloud-study</artifactId> <groupId>pers.itqiqi</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>cloud-consumer-order80</artifactId> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency> </dependencies> </project>
配置application.yml
server: port: 80 spring: application: name: cloud-consumer-order80 boot: admin: client: url: http://localhost:12000 instance: service-host-type: ip management: endpoints: web: exposure: include: '*'
创建实体类
@Data @AllArgsConstructor @NoArgsConstructor public class Payment { private Long id; private String serial; }
@Data @AllArgsConstructor @NoArgsConstructor public class CommonResult { private Integer code; private String msg; private Object data; public CommonResult(Integer code, String msg) { this(code, msg, null); } }
编写controller层
@RestController @Slf4j public class PaymentController { public static final String PAYMENT_URL = "http://localhost:8001"; @Autowired private RestTemplate restTemplate; @PostMapping("/consumer/payment") public CommonResult create(Payment payment) { return restTemplate.postForObject(PAYMENT_URL + "/payment/add", payment, CommonResult.class); } @GetMapping("/consumer/payment") public CommonResult getPayment(Long id){ return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class); } }
9.工程重构
由于实体类有重复冗余代码,所有重构!
新建 - cloud-api-commons
改POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>pers.itqiqi.springcloud-study</groupId> <artifactId>cloud-api-commons</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-api-commons</name> <description>cloud-api-commons</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.3</version> </dependency> </dependencies> </project>
复制pojo层代码
maven打包并安装
父项目引入依赖
<dependency> <groupId>pers.itqiqi.springcloud-study</groupId> <artifactId>cloud-api-commons</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
本文含有隐藏内容,请 开通VIP 后查看