消息总线bus
在微服务架构中,通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线。
SpringCloud中也有对应的解决方案,SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来,可以很容易搭建消息总线,配合SpringCloud confifig 实现微服务应用配置信息的动态更新。
根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:
提交代码触发post请求给bus/refresh
server端接收到请求并发送给Spring Cloud Bus
Spring Cloud bus接到消息并通知给其它客户端
其它客户端接收到通知,请求Server端获取最新配置
全部客户端均获取到最新的配置
消息总线整合配置中心#
(1) 引入依赖 #
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency>
(2)服务端配置 #
server: port: 10000 #服务端口 spring: application: name: config-server #指定服务名 cloud: config: server: git: uri: https://gitee.com/it-lemon/config-repo.git rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest management: endpoints: web: exposure: include: bus-refresh eureka: client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/ instance: preferIpAddress: true instance-id: ${spring.cloud.client.ip-address}:${server.port} #spring.cloud.client.ip-address:获取ip地址
(3)微服务客户端配置 #
server: port: 9002 eureka: client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/ spring: cloud: config: name: product profile: dev label: master discovery: enabled: true service-id: config-server
需要在码云对应的配置文件中添加rabbitmq的配置信息
重新启动对应的eureka-server , confifig-server , product-service。配置信息刷新后,只需要向配置中心发送对应的请求,即可刷新每个客户端的配置
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2019-07-29 多态、以及常用的关于类的方法(isinstance、issubclass、slots等)运算符重载的实现、上下文管理等