spring cloud config+bus实现配置中心的自动刷新
程序员文章站
2022-06-13 10:30:09
...
-
流程图:
根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:
1、提交代码触发post给config server发送bus/refresh
2、config server接收到请求从git端更新配置并且发送给Spring Cloud Bus
3、Spring Cloud bus接到消息并通知给其它客户端
4、其它客户端接收到通知,请求Server端获取最新配置
5、全部客户端均获取到最新的配置
-
上代码
- 我的环境: springboot:2.3.1 springcloud:Hoxton.SR6(均为当前最新)
- 三个服务:Eureka服务 ConfigServer服务 ConfigClient服务
- 能学到config和bus的就不上EurekaServer的代码了(很简单,eureka服务端口号8888)
ConfigServer服务代码(端口号8889)
- pom.xml
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
- application.yml
server:
port: 8889
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/joseph349/spingcloudconfig.git
search-paths: config-repo2
default-label: master
username:
password:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints: #SpringCloud 2.0.0版本以后暴露接口方式
web:
exposure:
include: "*"
#服务注册到eureka地址
eureka:
client: #客户端注册进eureka服务列表内
# register-with-eureka: true #表示是否将自己注册进eurekaServer默认为true
# fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须为true才能配合ribbon使用负载均衡
service-url:
defaultZone: http://localhost:8888/eureka
- 启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 我的git配置(如果用的是dev环境,文件名必须含有dev)
ConfigClient服务(端口号8881)(敲黑板...下面的文件都很重要)
- pom.xml
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--bus消息总线动态刷新配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
- bootstrap.properties(bootstrap优先级高于application文件)
server.port=8881
spring.application.name=config-client
spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri= http://localhost:8889/
##默认 false,这里设置 true,表示开启读取配置中心的配置
#spring.cloud.config.discovery.enabled=true
##对应 eureka 中的配置中心 serviceId,默认是 configserver
#spring.cloud.config.discovery.serviceId=config-server
## 开启消息跟踪
#spring.cloud.bus.trace.enabled=true
##启动失败时能够快速响应
#spring.cloud.config.fail-fast=true
eureka.client.service-url.defaultZone: http://localhost:8888/eureka/
##暴露shutdown端点服务
management.endpoints.web.exposure.include=*
##rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
- 启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
- 测试类
@RestController
@RefreshScope
public class TestController {
@Value("${neo.hello.hello}")
private String hello;
@RequestMapping("/hello")
public String from() {
return hello;
}
}
- 测试结果
依次启动EurekaServer服务,ConfigServer服务,ConfigClient服务
第一次测试 http://localhost:8881/hello 结果如下图
然后修改git中的值为 neo.hello.hello=hello dev22222222
重要的一步,利用actuaror刷新服务(pom中已加入该依赖)
http://localhost:8881/actuator/bus-refresh 一定要POST请求
这时configClient服务控制台很显示刷新记录
再次测试http://localhost:8881/hello,如下图
上一篇: Ubuntu Linux下使用SQLITE JDBC
下一篇: 蓝桥杯JAVA数列求和
推荐阅读
-
Spring Cloud 配置中心内容加密的配置方法
-
利用Spring Cloud Config结合Bus实现分布式配置中心的步骤
-
详解spring cloud config整合gitlab搭建分布式的配置中心
-
Spring Cloud微服务架构的构建:分布式配置中心(加密解密功能)
-
spring cloud实现Eureka注册中心的HA的方法
-
Spring Cloud Config实现分布式配置中心
-
详解如何在低版本的Spring中快速实现类似自动配置的功能
-
利用Spring Cloud Config结合Bus实现分布式配置中心的步骤
-
详解spring cloud config整合gitlab搭建分布式的配置中心
-
spring cloud config分布式配置中心的高可用问题