前面介绍了spring cloud config 基于本地配置文件,git,mysql的配置开发示例,但是存在一个问题,如果我们要更新配置,按照之前的处理,则需要修改完配置之后重启配置,这样不适合生产环境,为此,spring cloud提供了基于spring cloud bus的机制来实现配置更改后的动态通知和刷新。下面进行这块的入门尝试, 以spring cloud config git为例说明
建立config-server-git,大体与之前相同,但是增加了spring cloud bus相关依赖:
<?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">
<parent>
<artifactId>spring-cloud-test-001</artifactId>
<groupId>com.leo.test</groupId>
<version>1.0.0-snapshot</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-consumer-001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中主要改变在于引入了spring cloud bus amqp依赖,以及spring cloud actuator暴露对外刷新接口。
编写配置application.yml
:
spring:
rabbitmq:
host: xxxx
port: 5672
profiles:
active: remote
cloud:
config:
server:
git:
uri: https://github.com/hanxueming126/open-config-source
search-paths: config
username:xxx
password: xxxx
default-label: develop
force-pull: true #本地与git有冲突时,强制拉取覆盖本地
application:
name: config-server-git
server:
port: 9060
management:
endpoints:
web:
exposure:
include: "*" #需要开放才能通过接口请求刷新
#debug: true
建立启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerGitApplication {
// 需要 post请求 /actuator/bus-refresh才能刷新, 返回 204响应
public static void main(String[] args) {
SpringApplication.run(ConfigServerGitApplication.class);
}
}
这样spring cloud cofnig git 服务端搭建完毕
搭建spring cloud config客户端,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">
<parent>
<artifactId>spring-cloud-test-001</artifactId>
<groupId>com.leo.test</groupId>
<version>1.0.0-snapshot</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-consumer-001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
模块配置文件:bootstrap.xml
spring:
application:
name: config-consumer-001
cloud:
config:
uri: http://localhost:9060
fail-fast: true
profiles:
active: dev
rabbitmq:
host: 10.201.83.207
port: 5672
management:
endpoints:
web:
exposure:
include: '*'
#debug: true
编写启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigConsumerApplication.class);
}
}
编写controller:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config/consumer")
@RefreshScope
public class ConsumerController {
@Value("${hello}")
private String hello;
@RequestMapping("/test")
public String testConfig(){
return String.format("hello is %s ",hello);
}
}
开始时,git上config/下配置如下:
请求端会以 applicationName
-profile
来获取对应配置。
启动config-server和confi-consumer
访问: http://localhost:9080/config/consumer/test
更改git上配置如下:
我这里没有配置webhook,所以手动刷新config-server来刷新配置信息:
post请求config-server端: http://localhost:9060//actuator/bus-refresh
再次重新访问:http://localhost:9080/config/consumer/test
显示如下:
配置刷新成功,更新配置成功。
如果测试需要rabbitmq,安装可参考 rabbitmq 安装