Spring Cloud 应用篇 之 Spring Cloud Config(配置中心)一文中介绍了 Spring Cloud Config 的使用,已经实现了配置文件的统一管理(git 仓库),但是,每次修改配置文件后,还需要重新启动应用才能加载到修改后的配置文件,这还没有达到我们的目的,我们最终想要的是,修改完配置文件后,不需要重启我们的应用,就可以重新加载到修改后的配置文件,其实 Spring Cloud 已经为我们提供了这样的支持,那就是 Spring Cloud Bus 组件。
(一)简介
下面是官方文档介绍,我们可以使用消息中间件 RabbitMQ 或者 Kafka,只需要在项目中引用相应的依赖。至于消息中间件的使用,可以查阅相关资料。
-
Spring Cloud Bus works
by adding Spring Boot autconfiguration
if it detects itself
on the classpath.
To enable the bus, add
-
spring-cloud-starter-bus-amqp
or spring-cloud-starter-bus-kafka
to your dependency management. Spring Cloud takes care
of the rest.
-
Make sure the broker (RabbitMQ
or Kafka)
is available
and configured.
When running
on localhost, you need
not
do anything.
If you
-
run remotely, use Spring Cloud Connectors
or Spring Boot conventions
to define the broker credentials
下面我们就基于上篇文章的项目进行改造即可。
(二)改造 spring-cloud-config
2.1 修改 spring-cloud-config 的 pom 文件,添加依赖
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-config-server
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-starter-bus-amqp
</artifactId>
-
</dependency>
2.2 修改配置文件
从上面官方文档介绍来看,如果你的消息中间件是运行在本地(localhost),那么配置文件可以不用多余的配置,如果连接的是远程的消息中间件,那么就要配置消息中间件的相关信息如下:
-
server:
-
port:
8889
-
-
spring:
-
cloud:
-
config:
-
server:
-
git:
-
uri: https:
//github.com/shmilyah/cloud-config-samples.git
-
search-paths:
'{application}'
-
application:
-
name: spring-cloud-config
-
rabbitmq:
-
host:
192.168.
174.128
-
port:
5672
-
username: guest
-
password: guest
-
eureka:
-
client:
-
service-url:
-
defaultZone: http:
//localhost:8761/eureka/
-
management:
-
endpoints:
-
web:
-
exposure:
-
include:
'*'
我这里使用 rabbitmq,运行在我的虚拟机里,采用默认端口 5672,用户名和密码都是默认配置的 guest,这里其实不用配置也可以。
2.3 启动 spring-cloud-config
下面启动 spring-cloud-config,访问 http://192.168.174.128:15672,登录后,可以看到队列中已经多出一个
(三)改造 spring-cloud-config-client
3.1 修改 pom 文件
- <code class=”language-plain”><dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <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>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-bus-amqp</artifactId>
- </dependency></code>
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
-
</dependency>
-
<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>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-actuator
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-starter-bus-amqp
</artifactId>
-
</dependency>
3.2 启动 spring-cloud-config-client
启动 spring-cloud-config-client,访问 http://192.168.174.128:15672,可以看到又多出一个 Queue
(四)验证动态刷新
此时 config-client 的 git 仓库配置文件内容如下:
访问 http://localhost:8989/hello,如下
下面我们修改下 git 仓库的配置文件内容为 hello: world,再次访问 http://localhost:8989/hello,结果发现内容并没有更新
其实我们还需要做一个操作,那就是发送一个 post 请求,curl -X POST http://localhost:8889/actuator/bus-refresh
注意,因为我们使用的 spring cloud 的是 F 版本,与低版本有所不同,这里暴露的端点是 /actuator/bus-refresh,这个在之前的文章中有讲解过。
ok,下面我们就发送一次 post 请求
这个时候,我们应该能刷新 config-client 的配置信息了吧,我们再次访问 http://localhost:8989/hello,结果发现内容还是没变,截图就不贴了,why?
这里面还有一个坑,我们还需要加一个注解 @RefreshScope 在需要刷新的地方
下面我们修改下 spring-cloud-config-client 的接口类,然后重启应用
-
@SpringBootApplication
-
@RestController
-
@EnableEurekaClient
-
@RefreshScope
-
public
class ConfigClientApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(ConfigClientApplication.class, args);
-
}
-
-
@Value(
"${hello}")
-
private String hello;
-
-
@RequestMapping(
"hello")
-
public String hello() {
-
return hello;
-
}
-
}
访问 http://localhost:8989/hello,内容已经变为 world 了,因为我们重启了应用,所以配置内容更新了
OK,我们再次修改 config-client 的配置文件内容
再次发送 post 请求,curl -X POST http://localhost:8889/actuator/bus-refresh
我们可以从 spring-cloud-config-client 的后台日志看到,应用已经自动重新获取配置文件了,日志如下
再次访问 http://localhost:8989/hello
这个时候,内容已经自动刷新了,此时并没有重启应用,我们的初始目的已经达到了,但是这个时候还是有点麻烦,因为我们要手动去发送 post 请求,这个时候我们可以利用 GitHub 提供的 Webhooks
(五)使用 Webhooks
然后填写你要请求的 URL,Content type 选择 application/json
还有一个 Just the push event 的选择,这个选择是当你 push 内容到 GitHub 时,GitHub 就会去向你填写的 URL 自动发送 post 请求,就不用你自己手动发送请求了。
新版本有两个坑,就是上面所说的一个发送的 post 请求的 URL 有变化,暴露的端口为 /actuator/bus-refresh;还有一个就是要加一个 @RefreshScope 注解