企业分布式微服务云SpringCloud SpringBoot mybatis -消息总线(Spring Cloud Bus)
发布日期:2021-09-03 18:04:44 浏览次数:2 分类:技术文章

本文共 3744 字,大约阅读时间需要 12 分钟。

Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。

一、准备工作

本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq下载。至于怎么使用 rabbitmq,搜索引擎下。

二、改造config-client

在pom文件加上起步依赖spring-cloud-starter-bus-amqp,完整的配置文件如下:

4.0.0
com.forezp
config-client
0.0.1-SNAPSHOT
jar
config-client
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.retry
spring-retry
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-aop
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-bus-amqp
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
复制代码

在配置文件application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码,代码如下:

spring.rabbitmq.host=localhostspring.rabbitmq.port=5672# spring.rabbitmq.username=# spring.rabbitmq.password=复制代码

如果rabbitmq有用户名密码,输入即可。

依次启动eureka-server、confg-cserver,启动两个config-client,端口为:8881、8882。

访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:foo version 3

这时我们去代码仓库将foo的值改为“foo version 4”,即改变配置文件foo的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。此时,我们只需要发送post请求: 你会发现config-client会重新读取配置文件

这时我们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:foo version 4

另外,/bus/refresh接口可以指定服务,即使用”destination”参数,比如 “/bus/refresh?destination=customers:**” 即刷新服务名为customers的所有服务,不管ip。Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求 一七九一七四三三八零

转载于:https://juejin.im/post/5c734ab951882562c955f20c

转载地址:https://blog.csdn.net/weixin_34194317/article/details/91484340 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:前端面试送命题—面试题篇
下一篇:搞清楚 Python 的迭代器、可迭代对象、生成器

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月03日 15时51分21秒