微服务篇-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

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

相关推荐
ajsbxi5 分钟前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
StayInLove24 分钟前
G1垃圾回收器日志详解
java·开发语言
对许27 分钟前
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“
java·log4j
无尽的大道31 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
小鑫记得努力40 分钟前
Java类和对象(下篇)
java
binishuaio44 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE1 小时前
【Java SE】StringBuffer
java·开发语言
老友@1 小时前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
wrx繁星点点1 小时前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
Upaaui1 小时前
Aop+自定义注解实现数据字典映射
java