Chapter2 消息总线ConfigClient配置自动刷新

Spring Cloud Bus:

Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ、Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化或者其他的管理指令。使用Spring Cloud Bus后的架构如图所示。

准备工作:

需要安装rabbitMQ,安装rabbitMQ的详情请参考:https://blog.csdn.net/qq_35098526/article/details/80009424这篇文章主要讲Windows下的安装过程。

环境搭建

创建三个模块:

eureka-server  eureka服务注册中心

config-server   配置中心服务端

config-client    配置中心客户端

1.     eureka-server 模块pom文件配置如下,这里跟第一章节一样 ,不多说。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.harry</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-server</name><description>EUREKA 服务注册中心</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.M9</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

2. config-server   配置中心服务端pom配置,这里引入了spring-cloud-starter-bus-amqp。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.harry</groupId><artifactId>config-server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-server</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.M9</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>

application.yml 配置

eureka:client:serviceUrl:defaultZone: http://localhost:8889/eureka/
spring:application:name: config-servercloud:config:server:git:uri: https://gitee.com/honghh/spring-cloud-config # 配置git仓库地址searchPaths: config-doc # 配置仓库路径
#          username: # 访问git仓库的用户名
#          password: # 访问git仓库的用户密码
#      label: #配置仓库的分支server:port: 8989

在application.yml可以看到引入了git仓库地址,如果仓库是私有的,需要输入访问的账号和密码,这个地址是我自己测试用的,是公开的,可以不输入账号密码。searchPaths:config-doc# 配置仓库路径如果仓库路径是根目录则不需要写这个,如果是多级目录,可以写成A/B/C格式。

2.     config-client  配置中心客户端这里需要注意pom文件中引入了spring-cloud-starter-bus-amqp,spring-boot-starter-actuator,spring-retry

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.harry</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-client</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.M9</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>
</project>

bootstrap.yml 配置

eureka:client:serviceUrl:defaultZone: http://localhost:8889/eureka/server:port: 8881spring:application:name: config-clientcloud:config:label: masterprofile: devdiscovery:enabled: trueservice-id: config-serverrabbitmq:host: localhostport: 5672username: guestpassword: guest#management:
#  security:
#    enabled: false  #spring boot1.5.x 版本配置management:endpoints:web:exposure:include: bus-refresh

这里写入rabbitMQ的配置和management的配置,我们可以看到有一段注释的代码,解析看下面注意有坑①。

启动项目

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

1.访问http://localhost:8881/hi浏览器显示:

2这时我们去代码仓库将config-client-dev.properties下的foo的值改为“foo version 4”,即改变配置文件foo的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。此时,我们只需要发送post请求:http://localhost:8881/bus/refresh,你会发现config-client会重新读取配置文件。可以看到控制台打印日志如图。注意有坑 ①

   

3.     再次请求http://localhost:8881/hi

到这里操作就已经完成。接下来我们讲一下遇到的问题

注意有坑

①  上面提到请求 http://localhost:8881/bus/refresh但是你在运行的时候遇到报404的错误

图5.1

图5.2

首先,你要确认是post请求,如图5.1是get请求,是会报404的错误的

但是图5.2中用的是post请求为什么还会报错

这里就是我所说的坑看项目中的pom文件我用的是spring boot 2.0.1.RELEASE版本,查资料看到一篇文章 https://ask.csdn.net/questions/684123

详情如下:

SpringBoot2.0Config客户端自动刷新时没有/bus/refresh端点

Spring Cloud ConfigClient的配置自动刷新

目前的博客和视频都是Spring Boot1.5.x的,安装rabbitmq,导入依赖,配置yml,添加@RefreshScope,启动Config client时日志都会打印出/bus/refresh这个端口,但是到spring boot2.0就不再打印这个端口,也无法访问。即使yml配置暴露actuator的所有端口,也就只有/actuator/refresh这个端口(这个端口是只更新一个的)。

请问Spring boot 2.0如何实现Config Client自动刷新配置

Spring boot 2.0的改动较大,/bus/refresh全部整合到actuador里面了,所以之前1.x的management.security.enabled全部失效,不适用于2.0

适用于2.0的配置是这样的:

 management:endpoints:web:exposure:include: bus-refresh

另外注解

@RefreshScope

需要在配置的页面加上,就是说附带@Value的页面加上此注解

请求刷新的页面由原来1.5.x的localhost:8888/bus/refresh

变成:http://localhost:8888/actuator/bus-refresh

注意:config-server和config-client的配置都得加上

 management:endpoints:web:exposure:include: bus-refresh

所以这就是上面为什么这样改的原因,执行刷新的的请求为:

http://localhost:8881/actuator/bus-refresh

下载地址:

代码我已同步到码云git :https://gitee.com/honghh/spring-cloud-config

Chapter2 消息总线 ConfigClient配置自动刷新相关推荐

  1. Spring Cloud Bus 消息总线实现配置自动刷新

    why 当微服务太多的时候,服务之间需要建立通信或一个服务的改变需要广播到所有其它服务,这时就需要有一个总线来承担对应的职责. what spring cloud bus 是通过轻量消息代理连接各个分 ...

  2. SpringCloud教程-消息总线Bus 客户端(client)刷新(SpringCloud版本Greenwich.SR4)

    文章目录 消息总线(Bus)介绍 项目示例 config-client-bus 代码地址:github-spring-cloud地址 前言:前面文章讲了Spring Cloud Config配置中心如 ...

  3. SpringCloud 入门教程(三): 配置自动刷新

    Spring Cloud 入门教程(三): 配置自动刷新 之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Se ...

  4. springcloud21---Config-bus实现配置自动刷新

    Pivotal(毕威拓)有VMware和EMC成立的. RabbitMQ是由ERlang(爱立信开发的,面向并发的编程语言),安装RabbitMQ先要安装ERlang. package com.itm ...

  5. SpringCloud config 配置中心集群配置以及整合消息总线BUS实现关联微服务配置自动刷新

    一.SpringCloud Config 基本配置中的问题 在上一章节<SpringCloud config 配置中心介绍与基本配置使用>中我们现实了配置中心的配置集中管理.调用微服务应用 ...

  6. (四)spring cloud微服务分布式云架构-配置中心和消息总线(配置中心终结版)...

    Spring Cloud Bus Spring cloud bus通过轻量消息代理连接各个分布的节点.这会用在广播状态的变化(例如配置变化)或者其他的消息指令.Spring bus的一个核心思想是通过 ...

  7. springcloud(九):配置中心和消息总线(配置中心终结版)

    我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...

  8. 第十六章:SpringCloud Config 配置自动刷新

    ####准备工作 我们首先需要下载rabbitMq(默认4396端口) MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对 ...

  9. nacos配置自动刷新

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 nacos自动刷新配置 一.简单实现 二.工具类自动刷新 1.使用PostConstruct注解 2.Application ...

最新文章

  1. 使用NATAPP.cn测试微信支付回调接口
  2. Asp.Net异步页面处理(转)
  3. VTK:3DArray用法实战
  4. ES6新特性之函数优化-参数默认值
  5. 【转】2.4SharePoint服务器端对象模型 之 访问网站和列表数据(Part 4)
  6. 实例分割模型Mask R-CNN详解——从R-CNN,Fast R-CNN,Faster R-CNN再到Mask R-CNN
  7. c语言线性顺序表,C语言程序——线性顺序表.doc
  8. Lucas+阶乘打表+费马小定理模板2.0
  9. 69. Php部分常见问题总结
  10. vue混入html,vue混入(mixins)的应用
  11. 备案 前置或专项审批的内容
  12. 使用联想恢复盘安装正版Win7 Professional
  13. 如何查询手机当前基站编号信息
  14. css3中文参考手册
  15. ajax异步上传图片文件并将其转换为base64格式
  16. Python入门基础(2)——基本类型与字符串处理
  17. 【有手就行】使用你自己的声音做语音合成
  18. Resilience4j:请求1秒超时504,Response took longer than configured timeout
  19. 如何用好Google?
  20. 听普罗科菲耶夫的二部小提琴协奏曲

热门文章

  1. 【PHPWord】TitleTOC
  2. 【教程】1、加载静态内容
  3. 计算机中职高考,中职计算机高考中的应用
  4. show index mysql_MySQL SHOW INDEX 语法解析
  5. python 读法-python 怎么读,python 怎么读
  6. apache bench linux,linux – 如何在CentOS上安装Apache Benchmark?
  7. mvc 职能划分_【转】MVC中的操作如何分类(MVC层次的划分)
  8. python 代码转成 c_如何将此C代码转换为Python?
  9. css怎么动画中该透明度,通过css3动画和opacity透明度实现呼吸灯效果
  10. 如何设置鼠标移开后css,如何设置鼠标离开时由hover设置的样式不变?