SpringCloud从零构建(六)——消息总线Bus+Rabbit MQ实现动态刷新

消息总线Bus+Rabbit MQ实现动态刷新

上一讲中我们讲了config的配置实现了Server从远端Git拉取配置文件且不用重启服务,但是Client并未更新,这一讲我们就来解决这个问题。
在开始项目之前我们要理解以下两个内容:
1)Spring Cloud Bus是什么

Spring Cloud Bus 将分布式的节点和轻量的消息代理连接起来。这可以用于广播配置文件的更改或者其他的管理工作。一个关键的思想就是,消息总线可以为微服务做监控,也可以作为应用程序之间相互通讯。

1)Rabbit MQ以及安装
     Rabbit MQ是一个消息中间件,具体内容不做介绍,自行搜索,下载地址:https://www.rabbitmq.com/download.html
在这里插入图片描述
我这里是放在本地测试是Windows系统,所以需要下载Windows,如果你在虚拟机或者服务器上可以下载对应的Linux版本。具体安装过程在这里不在赘述。

一)创建项目

主要涉及config-bus-server和config-bus-client两个模块的修改
A、Server模块的修改
1、首先对config-bus-server的application.properties的模块增加以下内容,如图1所示:
在这里插入图片描述
代码所示:

#bus总线配置
management.endpoints.web.exposure.include=bus-refresh
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled =true
#rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

management.endpoints.web.exposure.include=bus-refresh:暴露配置,不要改动
spring.cloud.bus.enabled=true:是否开始bus,是
spring.cloud.bus.trace.enabled =true:这个也说不清,这样配置一下
spring.rabbitmq.host=localhostspring.rabbitmq.host=localhost:这个是Rabbit启动的地址,在本地就写localhost,在服务器上就写服务器的地址。
spring.rabbitmq.port=5672:这个端口很重要,千万不要改,很多人看是官网写着15672,其实这边还是5672
spring.rabbitmq.username=guest:默认账号
spring.rabbitmq.password=guest:默认密码
如果按照我之前的步骤走,到这一步就好了,网上说需要添加很多依赖的很麻烦,我这样就很简单。
B、Client模块的修改
2、对config-bus-client的bootstrap.properties的模块增加以下内容,如图2所示:
在这里插入图片描述
代码如下所示:

#bus总线配置
management.endpoints.web.exposure.include=bus-refresh
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled =true
#rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

没错,这个和client一样。
3、对config-bus-client的ConfigBusClientController的增加注释,如图3标红所示:
在这里插入图片描述
代码如下:

@RestController
@RefreshScope
public class ConfigBusClientController {

        @Value("${neo.hello}")
        private String hello;

        @RequestMapping("/hello")
        public String from() {
            return this.hello;
        }
}

这样就改造完成了

二)启动项目

1、依次启动ConfigBusServerApplication、ConfigBusClientApplication。
2、启动Rabbit MQ,访问地址是http://localhost:15672/#/queues,账号密码就是上面配置文件中的内容
在这里插入图片描述
如果在Queues中可以看到两个队列,则说明前面的配置没问题。

三)测试项目

最好用Postman这个软件进行测试,不然可能 会报错。
1、按如图所示选择Get方式输入url:http://localhost:8082/config-properties/dev;在Headers里面设置Headers的key为Content-Type:application/json点击Send进行请求,这个地址就是我们上一篇讲到的config--bus-server请求git上项目配置文件的地址。
在这里插入图片描述
我们就会看到上图中neo.hello的值为hello,world,this is updatetest;
2、按照上面的方式我们将url改为:http://localhost:8083/hello,这是我们上篇讲到的config--bus-client中controller的请求地址,在这里插入图片描述
发现neo.hello的值也是hello,world,this is updatetest;
3、此时我们修改config-bus-client的application.properties中neo.hello的值并提交git,比如我们改成:

neo.hello= hello,world,this is test,ok

按照1和2的方式分别进行请求发现
http://localhost:8082/config-properties/dev的值为 “neo.hello”: “hello,world,this is test,ok”
在这里插入图片描述
http://localhost:8083/hello的值为hello,world,this is updatetest,没变
在这里插入图片描述
但此时如果我们再发送一个请求:http://localhost:8082/actuator/bus-refresh,
要注意以下两点
1、是post方式不是get
2、要设置1中Header的值,不然会报错。

在这里插入图片描述
然后这时再次请求http://localhost:8083/hello发现值已经变了
在这里插入图片描述
这说明我们已经修改成功了!
这只是一个简单的案例后面在实际场景中进行改动。

SpringCloud从零构建(一)——Eureka注册中心
SpringCloud从零构建(二)——创建服务端Server
SpringCloud从零构建(三)——创建消费者Customer
SpringCloud从零构建(四)——Feign实现负载均衡
SpringCloud从零构建(五)——Config配置中心
SpringCloud从零构建(六)——消息总线Bus+Rabbit MQ实现动态刷新

github地址:https://github.com/gjen1996/microservice
如果有问题欢迎小伙伴留言和我沟通交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Bus是一个用于在分布式系统中传播状态变化的消息总线。它基于Spring Cloud Stream和Spring Cloud Config构建,可以将消息广播到整个系统中的所有服务实例。通过使用Spring Cloud Bus,可以实现配置的动态刷新、事件的传播和集群中的状态同步。 下面是使用Spring Cloud Bus自定义消息总线的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Cloud Bus的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ``` 2. 配置消息代理:在应用的配置文件中配置消息代理,例如使用RabbitMQ作为消息代理: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 发送自定义消息:在需要发送自定义消息的地方,使用Spring Cloud Bus提供的API发送消息。例如,可以使用`/actuator/bus-refresh`端点发送刷新配置的消息: ```shell curl -X POST http://localhost:8080/actuator/bus-refresh ``` 4. 接收自定义消息:在需要接收自定义消息的地方,使用Spring Cloud Bus提供的注解和监听器来接收消息。例如,可以使用`@RefreshScope`注解来刷新配置: ```java @RefreshScope @RestController public class ConfigController { // ... } ``` 通过以上步骤,您可以使用Spring Cloud Bus自定义消息总线实现配置的动态刷新、事件的传播和集群中的状态同步。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值