Spring Cloud Config作为配置中心服务于分布式系统,而且其Spring Environment和PropertySource特性与Spring程序非常契合,特别适合Spring项目中。
特性
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
package com.tenmao.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.properties
)spring.application.name=config-server
server.port=7001
# Git 仓库位置
spring.cloud.config.server.git.uri=/data/workspace/config-repo.git
# 仓库路径下相对搜索位置,可配置多个
spring.cloud.config.server.git.search-paths=config
# 创建仓库config-repo
mkdir -p /data/workspace/config-repo.git
cd /data/workspace/config-repo.git
git init --bare
cd /data/workspace
git clone file:///data/workspace/config-repo.git
# 创建配置文件config/blog.properties和config/blog-dev.properties
mkdir config
cd config
touch blog.properties
cat threshold=default-0.1 > blog.properties
touch blog-dev.properties
cat threshold=dev-0.1 > blog-dev.properties
git add .
# 提交到远程仓库
git commit -m "initial config"
git push
# 创建新的版本(label)
git checkout -b 2.0
# 修改配置文件
git add .
# 提交到远程仓库
git commit -m "set threshold to 0.2"
git push --set-upstream origin 2.0
体验配置文件
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
pom.xml
<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>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
HomeController.java
@RestController
public class HomeController {
@Value("${threshold}")
private String threshold;
@GetMapping("threshold")
public String threshold() {
return threshold;
}
}
bootstrap.properties
注意:不是__application.properties
spring.application.name=blog
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001
测试配置参数
借助Spring Boot的Actuator的接口/refresh
pom.xml
)<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
@RefreshScope
@RefreshScope
@RestController
public class HomeController {
private String threshold;
@Value("${threshold}")
public void setThreshold(String threshold) {
this.threshold = threshold;
}
@GetMapping("threshold")
public String threshold() {
return threshold;
}
}
refresh
接口(application.properties
)management.endpoints.web.exposure.include=refresh
使用postman
通过POST
方法访问接口http://localhost:8080/actuator/refresh
注意:Spring Boot 2.x,是/actuator/refresh
,而不是/refresh
spring.cloud.config.server.git.search-paths=config
refresh
接口不存在:可能是没有引入spring-boot-actuator
,也或者没有修改配置文件management.endpoints.web.exposure.include=refresh