微服务篇-C 深入理解第一代微服务(SpringCloud)_VIII 深入理解Bus消息总线

原创作者:田超凡(程序员田宝宝)

版权所有,引用请注明原作者,严禁复制转载

Part 1 理论部分

1 配置文件如何刷新?

在SpringCloud中,配置文件的刷新有手动刷新和实时刷新两种方式。

手动刷新使用Actuator端点实现,

实时刷新使用SpringCloud Bus消息总线实现。

2 什么是SpringCloud Bus消息总线?

在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题连接到各个微服务实例,它广播的消息会被注册中心中所有的微服务实例监听和消费,这就是消息总线。

SpringCloud中也有对应的解决方案,SpringCloud Bus将分布式的节点用轻量的消息代理连接起来,可以很容易搭建消息总线,配合SpringCloud Config实现微服务应用配置信息的动态更新。

需要注意的是,消息总线并不属于消息中间件,消息总线只是为了能够整合消息中间件实现服务之间的消息通讯,类似于消息代理,从应用程序中传入消息并执行一些特别的操作,市面上开源的消息中间件有很多,比如ActiveMQ,RabbitMQ,Kafka,RocketMQ等等,目前SpringCloud Bus消息总线仅支持整合RabbitMQ和Kafka消息中间件。

Part 2 实践部分

Git环境搭建

使用码云环境搭建git服务器端

Git服务器上传配置文件

命名规范 服务名称-版本.yml 例如configclient_dev.yml

搭建Eureka 注册中心

搭建config-server

Maven依赖

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--spring-cloud 整合 ++config++-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-++config++-server</artifactId> <version>2.0.2.RELEASE</version> </dependency> <!-- SpringBoot整合++eureka++ 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-++netflix++ -++eureka++-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone\</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> |

application.yml

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ###服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka spring: application: ####注册中心应用名称 name: config-server cloud: config: server: git: ###git环境地址 uri: https://gitee.com/ittcf/config_003.git ####搜索目录 search-paths: - config ####读取分支 label: master ####端口号 server: port: 8888 |

启动config-server

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class AppConfigServer { public static void main(String[] args) { SpringApplication.run(AppConfigServer.class , args); } } |

读取配置文件http://127.0.0.1:8888/configclient-dev.yml

搭建config-client

Maven依赖

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus-parent</artifactId> <version>2.0.0.RC2</version> <type>++pom++ </type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-++config++-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> <!-- SpringBoot整合++eureka++ 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-++netflix++ -++eureka++-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> <!--核心jar包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-++amqp++</artifactId> </dependency> <!-- actuator监控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone\</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> |

application.yml

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| spring: application: ####注册中心应用名称 name: configclient cloud: config: ####读取后缀 profile: dev ####读取config-server注册地址 discovery: service-id: config-server enabled: true ##### eureka服务注册地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka server: port: 8882 |

启动config-client

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @RestController public class ConfigClientController { // 年龄 @Value("${userAge}") private String userAge; @RequestMapping("/userAge") public String userAge() { return userAge; } } |

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @SpringBootApplication @EnableEurekaClient public class AppConfigClient { public static void main(String[] args) { SpringApplication.run(AppConfigClient.class , args); } } |

使用消息总线

三个微服务项目中都加上该依赖信息

Maven依赖

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <!--核心jar包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-++amqp++</artifactId> </dependency> <!-- actuator监控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |

配置文件

|-----------------------------------------------------------------------|
| ###开启bus刷新 management: endpoints: web: exposure: include: bus-refresh |

刷新接口 http://127.0.0.1:8882/actuator/bus-refresh

本文部分素材转载自蚂蚁课堂

相关推荐
考虑考虑21 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613521 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊1 天前
Java学习第22天 - 云原生与容器化
java
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java
间彧1 天前
Spring Secutiy基本原理及工作流程
java
Java水解1 天前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学1 天前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole1 天前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端