Spring Cloud Bus总线的配置

上篇文章我们介绍了当我们的配置中心服务端发生变化后客户端的配置如果要得到及时的通知需要加上@RefreshScope的注解并且 还要使用post请求,请求下客户端的监控进行刷新操作,在服务非常多的情况下,非常的麻烦
curl -X POST "http://localhost:3355/actuator/refresh"

什么是总线
在微服务的架构中,通常会使用轻量级的消息代理来构建公用的消息主题,并让系统中所有的微服务实例都链接上,由于该主题中产生的消息会被所有的实例监听和消费所以称它为消息总线,在总线上的各个实例,都可以方便的广播一些需要让其他连接在该主题上的实例都知道的消息

基本原理: configclient 实例都监听mq中的同一个topic,默认是springcloudBus 当一个服务刷新数据的时候,它会把这个信息放入到topic中,这样其它监听同一个topic的服务都能得到通知,然后自身去更新自己的配置

本片博文我将介绍下spring cloud如何借助rabbitmq实现广播发送请求 激活客户端更新配置信息的
服务端3344的pom.xml在之前的服务基础上再加上rabbitmq的支持

<!-- 添加消息总线RabbitMQ支持 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

服务端3344的application.yml的配置

server:
  port: 3344
spring:
  application:
    name: cloud-config-center  # 注册进eureka服务器的微服务名字
  cloud:
    config:
      server:
        git:
          uri: https://github.com/sofencyXiao/spring-cloud-config.git
          search-paths:
            - spring-cloud-config  #搜索目录
      label: master  #读取分支
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
  
rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

## rabbitmq的相关配置 暴露bus刷新配置的端点
management:
	endpoints: #暴露bus刷新配置的端点
		web:
			exposure:
				include: "bus-refresh"

同理在3355和3366的pom.xml中添加上rabbitmq的依赖,并且在bootstrap.yml中添加上如下配置

management:
  endpoints: #暴露bus刷新配置的端点
	web:
	 exposure:
	   include: "*" 

测试 :
注意要将将更新配置信息的消息发送给其它的客户端的服务
要发送post请求给
curl -X POST "http://localhost:3344/actuator/bus-refresh"
这样就发生了刷新

定点更新配置信息
在发送post请求的时候指定发送的实例
http://localhost:{配置中心}/actuator/bus-refresh/{destination}
http://localhost:3344/actuator/bus-refresh/config-cliient:3355 只发送给spring.applicationname为config-client并且端口为3355的服务

猜你喜欢

转载自blog.csdn.net/qq_43079376/article/details/108318886