SpringCloud Bus 消息总线的具体使用

这篇文章主要介绍了SpringCloud Bus 消息总线的具体使用,详细的介绍了什么是消息总线以及具体配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

什么是消息总线

1. 概念

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

2. SpringCloud Bus

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

3. 其他

消息代理属于中间件。设计代理的目的就是为了能够从应用程序中传入消息,并执行一些特别的操作。 开源产品很多如ActiveMQ、Kafka、RabbitMQ、RocketMQ等 目前springCloud仅支持RabbitMQ和Kafka。本文采用RabbitMQ实现这一功能。

搭建分布式配置中心

1. Config 架构

当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

2. Git 环境搭建

使用 码云 环境搭建 git

码云环境地址: https://gitee.com/guopf/springcloud_bus

3. Git服务器上传配置文件

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

4. 搭建 Eureka 服务注册中心

具体搭建环境随后补充,可以使用我自己部署的 http://47.105.86.222:8100 (配置地址http://47.105.86.222:8100/eureka)

5. 搭建 config-server 服务

1. maven 依赖

   org.springframework.bootspring-boot-starter-web org.springframework.cloudspring-cloud-config-server2.0.2.RELEASE org.springframework.cloudspring-cloud-starter-netflix-eureka-client2.0.2.RELEASE

2. 配置文件

 ### 服务地址 端口 server: port: 8800 ### 服务名称 spring: application: name: config_server cloud: config: server: git: ### git 地址 uri: https://gitee.com/guopf/springcloud_bus.git username: password: ### 配置读取文件夹 search-paths: config ### 分支 label: master ### eureka 配置 eureka: client: service-url: defaultZone: http://47.105.86.222:8100/eureka register-with-eureka: true fetch-registry: true

3. 启动

 /** * @EnableEurekaClient : 开启 eureka 客户端 * @EnableConfigServer : 开启 config 服务端 * */ @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } } 

搭建 config-client 服务

1. 手动更新 

1. maven 依赖

   org.springframework.bootspring-boot-starter-web org.springframework.cloudspring-cloud-config-client2.0.2.RELEASE org.springframework.cloudspring-cloud-starter-netflix-eureka-client2.0.2.RELEASE org.springframework.bootspring-boot-starter-actuator

2. 配置文件(bootstrap.yml)

 ### 端口 server: port: 8801 ### eureka 配置中心 eureka: client: service-url: defaultZone: http://47.105.86.222:8100/eureka fetch-registry: true register-with-eureka: true ### 配置服务名称,要和config 配置中心文件保持一致 spring: application: name: configclient cloud: config: ### 读取配置 profile: dev discovery: ### enabled: true ### config 配置中心配置的服务名称 service-id: config_server management: endpoints: web: exposure: include: "*"

3. 启动

 @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } } 
 /** * 在读取配置文件信息的地方进行添加 @RefreshScope 注解 */ @RestController @RefreshScope public class AppController { @Value("${userAge}") private String userAge; @GetMapping("/userAge") public String config(){ System.out.println("userAge : " + userAge); return userAge; } }

修改git仓库中的配置,进行手动更新,post请求

http://127.0.0.1:8801/actuator/refresh  启动刷新器 从cofnig_server读取

2. 使用消息总线 bus 更新

1. 添加依赖信息

在 config_server,config_client 中添加 

  org.springframework.cloudspring-cloud-starter-bus-amqp org.springframework.bootspring-boot-starter-actuator

2. 配置文件修改

添加对 rabbbitMQ的配置, rabbitMQ服务和config_server,config_client 在一个服务器上,使用默认配置即可

 ### rabbitmq 配置信息 rabbitmq: addresses: 47.105.86.222 username: guest password: guest port: 5672 virtual-host: /

3. 刷新

http://127.0.0.1:8801/actuator/bus-refresh   post方式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是SpringCloud Bus 消息总线的具体使用的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java