赞
踩
目录
Micrometer Tracing 集成实现分布式链路追踪
集成Spring Authorization Server 与Spring Security Oauth2.1统一认证
只介绍如何集成及使用,概念性的东西就不细说了。慢慢更新
代码我传到了gitHub上,地址如下:
ricardo-m-yu/spring-cloud-alibaba (github.com)
spring-cloud-alibaba/README-zh.md at 2022.x · alibaba/spring-cloud-alibaba · GitHub
我这边使用的都是最新版
spring.cloud.alibaba.version | 2022.0.0.0-RC2 |
spring.cloud.version | 2022.0.0 |
spring.boot.version | 3.1.0 |
spring-cloud-alibaba、spring-cloud、springboot 三者的版本关系说明
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
因为需要使用新版本的springboot及spring.cloud.alibaba。
springboot3.1x版本特性:
- 调整 JDK 基线到 17 版本,兼容 JDK19,即最低的JDK要求17
- 将 Java EE 依赖迁移到 Jakarta EE;比如 javax.servlet.Servlet 已经成了 jakarta.servlet.Servlet
- 支持应用使用 GraalVM 技术生成原生镜像,取代了之前实验性质的 Spring Native 项目,让 Spring Boot 应用直接迈向 GraalVM;
- 使用 Micrometer 和 Micrometer Tracing 提升了应用可观测性,比如 sleuth已经废弃,使用了Micrometer Tracing
- Docker Compose的支持
- ssl的配置
- 授权服务器的自动配置: Spring Authorization Server
组件关系
- 服务注册与发现:Nacos
- 分布式事务:Seata
- 网关:Spring Cloud Gateway
- 服务调用:OpenFeign
- 鉴权:Spring Authorization Server 、Oauth2.1
- 消息队列:rocketmq
- 限流、熔断:sentinel
- 链路追踪:Micrometer Tracing
- 接口文档:knife4j
我是用的VMware虚拟机里面跑的。
具体怎么装,看下面
(312条消息) VMware16安装 CentOS7_ricardo.M.Yu的博客-CSDN博客
我这边搭建了一个nexus maven私服
(310条消息) docker-compose 搭建maven私服 nexus与配置_ricardo.M.Yu的博客-CSDN博客
(310条消息) docker-compose安装nacos 2.2.1及配置_ricardo.M.Yu的博客-CSDN博客
我这边使用的是springboot多模块项目,最终效果如下
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>org.example</groupId>
- <artifactId>cloud-alibaba</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>pom</packaging>
-
- <modules>
- <module>common</module>
- <module>file</module>
- <module>auth</module>
- <module>gateway</module>
- <module>admin</module>
- <module>data</module>
- <module>biz</module>
- <module>log</module>
- <module>consumer</module>
- </modules>
-
- <properties>
- <maven.compiler.source>17</maven.compiler.source>
- <maven.compiler.target>17</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.cloud.alibaba.version>2022.0.0.0-RC2</spring.cloud.alibaba.version>
- <spring.cloud.version>2022.0.0</spring.cloud.version>
- <spring.boot.version>3.1.0</spring.boot.version>
- <spring.boot.admin.version>2.5.1</spring.boot.admin.version>
-
- <lombok.version>1.18.20</lombok.version>
- <fastjson.version>1.2.73</fastjson.version>
- </properties>
-
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>3.1.0</version>
- <relativePath/>
- </parent>
-
- <dependencyManagement>
- <!-- cloud -->
- <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>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${spring.cloud.alibaba.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
- <!-- admin -->
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-server</artifactId>
- <version>${spring.boot.admin.version}</version>
- </dependency>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</artifactId>
- <version>${spring.boot.admin.version}</version>
- </dependency>
-
- <!-- 第三方工具 -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>${lombok.version}</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>${fastjson.version}</version>
- </dependency>
-
-
- </dependencies>
- </dependencyManagement>
-
- <!--统一环境-->
- <profiles>
- <profile>
- <id>prod</id>
- <properties>
- <profiles.active>prod</profiles.active>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>dev</id>
- <properties>
- <profiles.active>dev</profiles.active>
- </properties>
- </profile>
- </profiles>
-
-
- <!--maven插件-->
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <mainClass>none</mainClass> <!-- 取消查找本项目下的Main方法:为了解决Unable to find main class的问题 -->
- <classifier>execute</classifier> <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-
- </project>
部分可以参考官方的示例
这边我定义了4组配置文件,说一下他们的作用:
引导类配置;
bootstrap.yaml:定义当前生效的配置
- spring:
- profiles:
- active: dev
bootstrap-dev.yaml:定义引导类当前生效的参数
- spring:
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
-
应用级配置:
application.yaml
应用级别的基础配置
- logging:
- level:
- root: info
- com:
- sy: debug
application-dev.yaml
定义应用的名称以及端口等信息
- server:
- port: 9110
- spring:
- application:
- name: biz
引入依赖
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-loadbalancer</artifactId>
- </dependency>
启动后,查看nacos控制台服务列表,已经能看到这个实例
需要在新建一个consumer服务,来测试
分别在两个模块建两个controller, biz模块下:
- @RestController
- @RefreshScope
- @RequestMapping("/")
- public class BizController {
-
- @GetMapping(value = "/echo/{string}")
- public String echo(@PathVariable String string) {
- return "Hello Nacos Discovery " + string;
- }
- }
consumer模块下
-
- @Slf4j
- @RestController
- @RequestMapping("/")
- public class ConsumerController {
-
- @Autowired
- private RestTemplate restTemplate;
-
- @Autowired
- private LoadBalancerClient loadBalancerClient;
-
- @Value("${spring.application.name}")
- private String appName;
-
- @GetMapping("/echo/app-name")
- public String echoAppName() {
- //使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问
- ServiceInstance serviceInstance = loadBalancerClient.choose("biz");
- String url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
- System.out.println("request url:" + url);
- String result = restTemplate.getForObject(url, String.class);
- log.info("result -> {}", result);
- return result;
- }
- }
用consumer下的http测试接口,发现已经能正常返回数据,appName即为consumer
配置管理测试,主要集成从nacos拉取公共配置并测试,
biz模块下新增依赖和配置文件
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
- </dependency>
增加配置中心config配置,文件扩展为yaml
- spring:
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
- #配置中心
- config:
- server-addr: http://192.168.1.115:8108/
- group: dev
- file-extension: yaml
- # 共享配置
- # shared-configs:
- # - data-id: application-dev.yaml
- # group: dev
- # extension-configs:
- # - dataId: test01.yml
- # group: dev
- application:
- name: biz
nacos web页面配置列表新增一个配置,命名格式和group要对,dataId格式
${spring.application.name}-${profile}.${file-extension:properties}
示例: biz-dev.yaml
我这边建了个示例
修改 BizController 增加方法:
-
- @Value("${username}")
- private String username;
-
- @GetMapping("/get")
- public String get() {
- return username;
- }
http测试:可以看到username的值已经为 nacos的设置值
quick-start | Sentinel (sentinelguard.io)
demo
spring-cloud-alibaba/readme-zh.md at 2022.x · alibaba/spring-cloud-alibaba · GitHub
详细的看下面文章
(305条消息) Spring Cloud Alibaba 之 sentinel-dashboard 安装及docker-compose镜像制作_ricardo.M.Yu的博客-CSDN博客
pom文件,新增依赖
- <!-- sentinel -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- </dependency>
修改bootstrap-dev.yaml, 增加 sentinel相关配置,
dashboard即为 sentinel控制台的地址,
port为当前项目需要暴露的接口,与控制台通信使用
- spring:
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
- sentinel:
- transport:
- dashboard: localhost:9988
- port: 8899
启动biz项目,调用下之前的 get接口,然后刷新 sentinel控制台,已经看到了biz的相关监控数据,其他详细的功能,可以看官方文档
nacos整合Spring Cloud Gateway 实现路由与服务动态发现
pom集成
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
- </dependency>
gateway模块配置文件增加
- spring:
- main:
- web-application-type: reactive
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
- sentinel:
- transport:
- dashboard: 192.168.1.115:9988
- port: 8899
- gateway:
- # discovery:
- # locator:
- # enabled: true
- routes:
- - id: consumer-service
- uri: https://www.qq.com
- predicates:
- - Method=GET,POST
- - Path=/consumer-service/**
- # - id: biz
- # uri: http://192.168.1.125:9110
- # predicates:
- # - Method=GET,POST
- # - Path=/biz/**
- - id: biz
- uri: lb://biz
- predicates:
- - Method=GET,POST
- - Path=/biz/**
id:路由的ID,名称可以随意定义,但必须保证唯一
uri: 目标URI,路由到微服务的地址
上面的配置中第一个是直接转发到 qq 网站;
下面是 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名
order:路由的优先级,数字越小,优先级越高。
predicates:配置断言,通过PredicateDefinition类进行接收配置
discovery:
locator: # 是否与服务发现组件结合,通过serviceId转发到具体服务实例
enabled: true # 是否开启基于服务发现的路由规则
如:
上面的方式中:lb://biz 配置即可找到 nacos中的biz服务
postman测试
已经收到了回复的消息
关于路由规则,过滤器、自定义的规则等知识,详细见官网。
引入依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
biz模块新增一个API
- @GetMapping(value = "/info")
- public Map<String, Object> getInfo(@RequestParam String username) {
- HashMap<String, Object> map = new HashMap<>();
- map.put("username", username);
- map.put("password", "123456");
- return map;
- }
consumer模块启动类增加注解@EnableFeignClients
- @EnableFeignClients
- @EnableDiscoveryClient
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
consumer模块新增一个 client,意思是调用biz模块的上面方法
- @Component
- @FeignClient(value = "biz")
- public interface BizClient {
-
- @GetMapping("/biz/info")
- Object getInfo(@RequestParam String username);
- }
consumer模块controller新增一个方法供调用
- @Resource
- private BizClient bizClient;
-
- @GetMapping("/username")
- public Object getUserInfo(@RequestParam String username) {
- return bizClient.getInfo(username);
- }
postmant调用 consumer的方法,效果如下:
可以看到已经成功调用
Seata 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题。
有些麻烦,放在了另一处,链接
(319条消息) Spring Cloud Alibaba 整合Seata 之概念介绍及Seata-server搭建_ricardo.M.Yu的博客-CSDN博客
我使用的是seata默认的AT模式,可以通过注解的方式无侵入的方式实现集成,需要额外的一张表如下
- Seata AT 模式 需要使用到 undo_log 表。
- -- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
- CREATE TABLE `undo_log` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `branch_id` bigint(20) NOT NULL,
- `xid` varchar(100) NOT NULL,
- `context` varchar(128) NOT NULL,
- `rollback_info` longblob NOT NULL,
- `log_status` int(11) NOT NULL,
- `log_created` datetime NOT NULL,
- `log_modified` datetime NOT NULL,
- `ext` varchar(100) DEFAULT NULL,
- PRIMARY KEY (`id`),
- UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
几个测试表,提供测试
- SET NAMES utf8mb4;
- SET FOREIGN_KEY_CHECKS = 0;
-
- -- ----------------------------
- -- Table structure for account_tbl
- -- ----------------------------
- DROP TABLE IF EXISTS `account_tbl`;
- CREATE TABLE `account_tbl` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
- `money` int(11) NULL DEFAULT 0,
- PRIMARY KEY (`id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of account_tbl
- -- ----------------------------
- INSERT INTO `account_tbl` VALUES (11, '1001', 981);
-
- -- ----------------------------
- -- Table structure for order_tbl
- -- ----------------------------
- DROP TABLE IF EXISTS `order_tbl`;
- CREATE TABLE `order_tbl` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
- `commodity_code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
- `count` int(11) NULL DEFAULT 0,
- `money` int(11) NULL DEFAULT 0,
- PRIMARY KEY (`id`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of order_tbl
- -- ----------------------------
- INSERT INTO `order_tbl` VALUES (6, '1001', '2001', 1, 5);
- INSERT INTO `order_tbl` VALUES (8, '1001', '2001', 1, 5);
- INSERT INTO `order_tbl` VALUES (9, '1001', '2001', 1, 5);
- INSERT INTO `order_tbl` VALUES (10, '1001', '2001', 1, 5);
-
- -- ----------------------------
- -- Table structure for stock_tbl
- -- ----------------------------
- DROP TABLE IF EXISTS `stock_tbl`;
- CREATE TABLE `stock_tbl` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `commodity_code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
- `count` int(11) NULL DEFAULT 0,
- PRIMARY KEY (`id`) USING BTREE,
- UNIQUE INDEX `commodity_code`(`commodity_code`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-
- -- ----------------------------
- -- Records of stock_tbl
- -- ----------------------------
- INSERT INTO `stock_tbl` VALUES (2, '2001', 11107);
之前的biz模块修改为provider模块,新建1个新的父模块 biz,并在下面建4个子模块 biz-account,biz-order,biz-stock,biz-web,新的结构如下
在biz父模块引入依赖
- <dependency>
- <groupId>com.sy</groupId>
- <artifactId>common</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
- </dependency>
子模块pom,这里我为了方便,使用了 knife4j的最新接口文档,支持springboot3.0
- <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.sy</groupId>
- <artifactId>biz</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
-
- <artifactId>biz-web</artifactId>
-
- <properties>
- <maven.compiler.source>17</maven.compiler.source>
- <maven.compiler.target>17</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>com.github.xiaoymin</groupId>
- <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
- <version>4.1.0</version>
- </dependency>
- </dependencies>
-
- </project>
每个文件都添加seata的配置,如下
- spring:
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
- sentinel:
- transport:
- dashboard: 192.168.1.115:8109
- port: 8900
-
-
- application:
- name: provider
- seata:
- enabled: true
- application-id: ${spring.application.name}
- tx-service-group: my-tx-group
- service:
- vgroup-mapping:
- my-tx-group: seata-server
- grouplist:
- seata-server: 192.168.1.115:8091
完整的就不弄了,到时下下来源码去看,主要说几个重要的
controller,提供接口
-
- @RequestMapping("/api/business")
- @RestController
- public class BusinessController {
-
- @Autowired
- private BusinessService businessService;
-
- /**
- * 购买下单,模拟全局事务提交
- *
- * @return
- */
- @GetMapping("/purchase/commit")
- public Boolean purchaseCommit(HttpServletRequest request) {
- businessService.purchase("1001", "2001", 1);
- return true;
- }
-
- /**
- * 购买下单,模拟全局事务回滚
- *
- * @return
- */
- @GetMapping("/purchase/rollback")
- public Boolean purchaseRollback() {
- try {
- businessService.purchase("1002", "2001", 1);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
-
- return true;
- }
- }
service提供多个服务的接口调用,方便测试
-
- @Slf4j
- @Service
- public class BusinessService {
-
-
- @Autowired
- private StockClient stockClient;
- @Autowired
- private OrderClient orderClient;
-
- /**
- * 减库存,下订单
- *
- * @param userId
- * @param commodityCode
- * @param orderCount
- */
- @GlobalTransactional
- public void purchase(String userId, String commodityCode, int orderCount) {
- log.info("purchase begin ... xid: " + RootContext.getXID());
- stockClient.deduct(commodityCode, orderCount);
- orderClient.create(userId, commodityCode, orderCount);
- }
- }
- @FeignClient(value = "biz-stock")
- public interface StockClient {
-
- @GetMapping("/api/stock/deduct")
- Object deduct(@RequestParam String commodityCode, @RequestParam Integer count);
- }
mapper及xml文件不一一列出了,下载源码去看,结构如下
knife4j配置
- @Configuration
- @EnableKnife4j
- public class SwaggerConfiguration {
-
- @Value("${spring.application.name}")
- private String appName;
-
- @Bean
- public OpenAPI customOpenAPI() {
- return new OpenAPI()
- .info(new Info()
- .title(appName + "系统API")
- .version("1.0")
- .description( "Knife4j集成springdoc-openapi示例")
- .termsOfService("http://doc.xiaominfo.com")
- .license(new License().name("Apache 2.0")
- .url("http://doc.xiaominfo.com")));
- }
- }
打开biz-web的接口
http://192.168.1.125:9203/doc.html
测试
如果打断点,undo_log这个表会有相关的信息
事务完成后,会删除
biz-web调用控制台效果图
seata-server端打印的日志
因为开发需求,需要整合各个服务的接口文档,发现了knife4j最新版本(2023.3)出来了一个好用的插件,可以方便整合 SpringCloud Gateway 与各个业务服务的接口文档,不需要之前我们再硬编码写各种过滤器和拦截器去手动整合了。
Spring Cloud Gateway网关聚合 | Knife4j (xiaominfo.com)
在之前的网关模块新增依赖
- <dependency>
- <groupId>com.github.xiaoymin</groupId>
- <artifactId>knife4j-gateway-spring-boot-starter</artifactId>
- <version>4.1.0</version>
- </dependency>
修改网关模块的 bootstrap-dev.yaml配置文件,增加 knife4j的配置
excluded-services:即为排除的网关服务的名称,
此处 strategy: discover 是依赖 nacos的注册中心的服务发现模式,可以自动发现注册的业务服务
- knife4j:
- gateway:
- enabled: true
- strategy: discover
- discover:
- version: openapi3
- enabled: true
- excluded-services:
- - gateway
在上面集成seata模块的过程中,每个服务都集成knife4j的配置
启动4个业务服务即一个网关服务,访问网关的接口文档,发现每个业务模块都被集成了进来,如下图
测试一下接口,调用以下之前biz-web模块下的方法,发现已经正常与网关集成了
集成完毕
参考下面另一篇文章
(324条消息) docker-compose安装 rocketmq server、dashboard_ricardo.M.Yu的博客-CSDN博客
准备两个服务:基于之前的改造:需要provider(最为生产者)以及consumer(作为消费者)
依赖
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
- <version>2022.0.0.0-RC2</version>
- </dependency>
- <!-- 接口文档方便调用-->
- <dependency>
- <groupId>com.github.xiaoymin</groupId>
- <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
- </dependency>
因为rocketmq依赖Spring Cloud Stream,先介绍下Spring Cloud Stream。
注意,因为我们使用最新版本一来的 stream版本为最新的 4.0,需要注意3.0以后的版本,写法有很大区别,一些遗留问题,如@EnableBInding、@StreamListener都已经不再使用(deprecated)
SpringCloud Stream是一个构建消息驱动微服务的框架,应用程序通过inputs或者 outputs来与SpringCloud Stream中的binder进行交互,我们可以通过配置来binding ,而 SpringCloud Stream 的binder负责与中间件交互,弄清楚两个概念:
注意:
bindings:命名 格式 {name}-out-0 这是是 4.x 的一种约定
这边我用了两个组,两个topic来测试
provider下 bootstrap-dev.yaml,增加 rocketmq配置
- spring:
- application:
- name: provider
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
- #配置中心
- config:
- server-addr: http://192.168.1.115:8108/
- group: dev
- file-extension: yaml
- # 共享配置
- # shared-configs:
- # - data-id: application-dev.yaml
- # group: dev
- # extension-configs:
- # - dataId: test01.yml
- # group: dev
- sentinel:
- transport:
- dashboard: 192.168.1.115:8109
- port: 8900
-
- # rocket mq 配置
- stream:
- rocketmq:
- binder:
- name-server: 192.168.1.115:9876
- bindings:
- test-out-0:
- destination: test-topic
- content-type: application/json
- group: test-group
- dev-out-0:
- destination: dev-topic
- content-type: application/json
- group: dev-group
-
-
consumer下 bootstrap-dev.yaml,增加 rocketmq配置
- spring:
- cloud:
- nacos:
- server-addr: http://192.168.1.115:8108/
- discovery:
- group: dev
- sentinel:
- transport:
- dashboard: 192.168.1.115:9988
- port: 8899
-
- # rocket mq 配置
- stream:
- rocketmq:
- binder:
- name-server: 192.168.1.115:9876
-
-
- function:
- definition: test;dev
- bindings:
-
- test-in-0:
- destination: test-topic
- content-type: application/json
- group: test-group
- consumer:
- instance-count: 3
- concurrency: 5
-
- dev-in-0:
- destination: dev-topic
- content-type: application/json
- group: dev-group
- consumer:
- instance-count: 3
- concurrency: 5
写一个person的测试类
- @Data
- public class Person {
-
- private String name;
- }
controller
- @RestController
- @Tag(name = "消息-消息队列发送")
- @RequestMapping("/message/send")
- public class MqSendController {
-
- @Resource
- private MqSendService mqSendService;
-
- @Operation(summary = "发送")
- @PostMapping
- public void send(@RequestParam String channel, @RequestParam String message) {
- mqSendService.send(channel, message);
- }
- }
service
- @Service
- public class MqSendService {
-
- @Resource
- private StreamBridge streamBridge;
-
- public void send(String channel, String message) {
- Person person = new Person();
- person.setName(message);
- Message<Person> build = MessageBuilder.withPayload(person).build();
- streamBridge.send(channel, build);
- }
- }
service
- @Slf4j
- @Service
- public class MqSubService {
-
- @Bean
- public Consumer<Message<Person>> test() {
- return message -> {
- MessageHeaders headers = message.getHeaders();
- Person payload = message.getPayload();
- log.info("消息 test:" + payload + "__" + headers);
- };
- }
-
- @Bean
- public Consumer<Message<Person>> dev() {
- return message -> {
- MessageHeaders headers = message.getHeaders();
- Person payload = message.getPayload();
- log.info("消息 dev:" + payload + "__" + headers);
- };
- }
- }
调用provider的controller方法,
注意,channel必须和配置文件里面的 destination 一致,否则收不到消息。
consumer打印:
web界面
已经正常集成完毕。
几个问题,一个是多组多订阅的问题,即一个组下的consumer订阅的topic要一直,不然消费组订阅关系不一致,会导致消息丢失问题,下面这个文章结束的比较好
RocketMQ的tag还有这个“坑”! - 知乎 (zhihu.com)
通过 stream 的header设置,来设置这两个参数
- public void send(String channel, String message, String tag, String keys) {
- Person person = new Person();
- person.setName(message);
- Message<Person> build = MessageBuilder.withPayload(person)
- .setHeader(MessageConst.PROPERTY_TAGS, tag).setHeader(MessageConst.PROPERTY_KEYS, keys).build();
- streamBridge.send(channel, build);
- }
可以看到,这两个参数已经被设置上
Zipkin是Twitter开源的分布式实时数据跟踪系统,主要功能是收集系统的时序数据,从而追踪微服务架构的系统延时等问题,从而达到链路调用监控跟踪作用,还提供了一个非常友好的UI界面,来帮助分析追踪数据。
安装的链接在这里
(291条消息) docker-compose 搭建 zipkin 服务端_ricardo.M.Yu的博客-CSDN博客
事先没做调研,从maven仓库选择了个最新的依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-zipkin</artifactId>
- <version>2.2.8.RELEASE</version>
- </dependency>
修改 bootstrap-dev.yaml
增加配置,
-
- zipkin:
- base-url: http://192.168.1.115:8112/
- sender:
- type: web
- enabled: true
- sleuth:
- sampler:
- probability: 1
启动后
看着有点变化,测试后没用,一通找文档
最后到了官方文档
Spring Cloud Sleuth Reference Documentation
sleuth停止支持, springboot 3.0.x之后的版本已经不再支持了,
Spring Cloud Sleuth will not work with Spring Boot 3.x onward. The last major version of Spring Boot that Sleuth will support is 2.x.
待寻找其他方案。
springboot 3.0.x之后,官方移除了对 sleuth的支持,将可观测性这部分迁移到了 Micrometer 项目中,下面实现这部分的功能,通过 Micrometer Tracing 实现日志以及通过 zipkin实现收集。
Micrometer Tracing官方文档
Micrometer Application Observability
sleuth迁移官方文档
Spring Cloud Sleuth 3.1 Migration Guide · micrometer-metrics/tracing Wiki (github.com)
需要下面的几个依赖
引入依赖管理器
- <micrometer-tracing.version>1.1.1</micrometer-tracing.version>
-
-
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing-bom</artifactId>
- <version>${micrometer-tracing.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
需要用到的依赖
- <dependency>
- <groupId>io.micrometer</groupId>
- <artifactId>micrometer-tracing-bridge-brave</artifactId>
- </dependency>
- <dependency>
- <groupId>io.zipkin.reporter2</groupId>
- <artifactId>zipkin-reporter-brave</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
基于之前biz模块下面的4个应用,都增加如下配置
-
- management:
- tracing:
- sampling:
- probability: 1.0
- zipkin:
- tracing:
- endpoint: http://192.168.1.115:8112/api/v2/spans
- connect-timeout: 3s
-
- logging:
- pattern:
- level: ${spring.application.name:},%X{traceId:-},%X{spanId:-} %5p
注意:
endpoint为zipkin配置
格式配置必须要为: http://{ip:port}/api/v2/spans,详见ZipkinProperties
日志格式为 应用名+ traceId +spanId
服务起来之后,还是调用biz-web下面的 接口,控制台输出的日志格式如下:
查看 zipkin的控制台已经能看到日志的收集
功能:集成以实现健康 (Health)信息、内存信息、JVM 系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、Spring Boot 缓存查看和管理等功能
准备两个服务,一个是 admin server端,另一个用来做客户端监控
- <spring.boot.admin.version>3.0.4</spring.boot.admin.version>
-
- <!-- admin -->
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-server</artifactId>
- <version>${spring.boot.admin.version}</version>
- </dependency>
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</artifactId>
- <version>${spring.boot.admin.version}</version>
- </dependency>
pom
- <dependency>
- <groupId>com.sy</groupId>
- <artifactId>common</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <!-- admin -->
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-server</artifactId>
- </dependency>
配置文件
- server:
- port: 9002
- spring:
- application:
- name: admin
-
- management:
- endpoint:
- health:
- show-details: always
pom
- <dependency>
- <groupId>de.codecentric</groupId>
- <artifactId>spring-boot-admin-starter-client</artifactId>
- </dependency>
配置文件
- spring:
- application:
- name: consumer
- boot:
- admin:
- client:
- url: http://localhost:9002
-
- management:
- endpoint:
- health:
- show-details: always
- endpoints:
- web:
- exposure:
- include: ["*"]
分别启动两个服务,访问 admin的web界面
已经成功启动,并且可以看见两个实例
点击应用墙
查看 consumer实例详情
可以看到更多详细配置
至此,集成完毕
这块整体比较复杂,我专门开了一篇新文章,专门介绍概念与集成,链接在下面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。