解决配置手动刷新问题-Spring Cloud Bus

1.为什么使用Spring Cloud Bus?

等gitee(领取)上配置刷新时需要通知所有服务端更新,一个手动更新不方便

2.consumer2项目操作

(1)consumer2项目加入依赖包

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-bus-amqp</artifactId>
	</dependency>    

(2)bootstrap.properties spring.application.name=spring-cloud-consumers2添加rabbitmq配置
management.endpoints.web.exposure.include=*
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3

producer2项目
(1)添加依赖包

org.springframework.cloud
spring-cloud-starter-bus-amqp
`
(2)修改application.yml(和consumer2的bootstrap.properties对应起来

cloud:
config:
server:
git:
uri: https://gitee.com/li-weiwen/my-test.git
search-paths: chapter6/springcloud-config
username: li-weiwen
password: as2dfgl234
bus:
trace:
enabled: true
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: “*”

4.效果验证

(1)运行项目Eureka,producer2,
再运行项目consumers2两次,两次的Program Argument参数不同
–server.port=8028
–server.port=8128
(2)win+R ,cmd,输入curl -X POST http : //localhost : 8028/actuator/bus - refresh
或者是curl - X POST "http://localhost:8028/bus/refresh"
报错

java C : \ Users \hhz > curl - X POST HTTP :/ /本地主机:8028 /总线/刷新{“日期”:“2021-06-14T03:08: 41.732 + 0000” ,“状态” :500 ,“错误” : "Internal Server Error" , "exception" : "org.springframework.messaging.MessageHandlingException" , "message" : "error发生在消息处理程序 [org.springframework. integration.amqp.outbound.AmqpOutboundEndpoint@1651d15]; 嵌套异常是 org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection denied: connect" , "path" : "/bus/refresh" }

原因:rabbitMQ没安装,先安装rabbitMQ,安装教程:https://www.cnblogs.com/saryli/p/9729591.html
注意安装步骤rabbitmq-server.bat
(3)有时候可能需要部分刷新,即只刷新指定的config-client,spring同样支持,只需要在调用/bus/refresh接口时传递destination参数即可,如:

/bus/refresh?destination=config-client:8031 或 /bus/refresh?destination=config-client:**

(4)访问http://localhost:8028/hello/spring
在这里插入图片描述

5.IDEA plugins报红

在这里插入图片描述

(1) 删除lastUpdated文件河南本地仓库位置:E:\softwaresummary\mavenrepository则转到E:\softwaresummary\mavenrepository\org\apache\maven\plugins删除以lastUpdated结尾文件
(2)点击reimport

6我将项目放到Gitee,怎么使用

(1)电脑安装rabbitmq,安装完后点击rabbitmq-server.bat
(2)注册个gitee账户,创建一个my-test的仓库
(3)将我的项目chapet6下的压缩包spring cloud bus下载解压导入到eclipse或者IDEA
(4)将我的项目chapter6上传到你的gitee仓库
(5)修改comsumer2的application.yml, username password成你的gitee账号密码,uri换成你创建的仓库名https地址(克隆/下载那儿可看到https地址)
cloud:
config:
server:
git:
uri: https://gitee.com/li-weiwen/my-test.git
search-paths: chapter6/springcloud-config
username: your_gitee_username
password: your_gitee_password

(6)依次运行eureka、producer2、comsumer2,
浏览器输入
http://localhost:8028/hello/spring
打开命令提示符,输入
curl -X POST “http://localhost:8028/bus/refresh”
然后再访问
http://localhost:8028/hello/spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值