配置中心
在git上创建几个配置文件如上图,文件命名规则{project}-{profile},如config-client-dev文件对应的项目名:config-client,版本是:dev。
spring cloud config server端
pom.xml
<dependencies>
<!--指定项目以web方式启动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
application.yml
spring:
application:
name: cloud_config
cloud:
config:
server:
git:
uri: https://gitee.com/liu0829/CloudConfig.git
username: *** #git账号
password: *** #git密码
default-label: dev #默认配置文件版本
search-paths: config #配置文件在git仓库的目录
server:
port: 7060
ConfigApplication.java
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableConfigServer //關鍵注解
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
至此,server端配置完成。
检测server端情况:
启动server端服务,通过http请求验证配置,Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
亲测了第一种和第三种可以,不知道为什么第二种不行。
spring cloud config 客户端
pom.xml
<dependencies>
<!-- 指定项目以web方式启动 -->
<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.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
bootstrap.yml
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:7060 # 指定config提供者路径
label: master # 指定git仓库的版本
profile: dev # 指定环境为dev环境的配置文件
application.yml
server:
port: 7061
spring:
application:
name: config-client
profiles:
active: dev
application-dev.yml
management:
endpoint:
shutdown:
enabled: false
endpoints:
web:
exposure:
include: "*"
## 配置默认的配置参数,避免启动时找不到参数报错,如果代码中有@Value("${data.env}")这种获取参数方式,找不到配置参数,启动时会报错。如果没有这种配置则可以不用配置默认参数。推荐不使用该方式进行配置获取。
data:
env: NaN
user:
username: NaN
password: NaN
两种获取参数的方式:
GitConfig.java
@Data
@Component
public class GitConfig {
@Value("${data.env}")
private String env;
@Value("${data.user.username}")
private String username;
@Value("${data.user.password}")
private String password;
}
GitAutoRefreshConfig.java
@Component
@Data
@ConfigurationProperties(prefix = "data")
public class GitAutoRefreshConfig {
@Data
public static class UserInfo {
private String username;
private String password;
@Override
public String toString() {
return "UserInfo{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
private String env;
private UserInfo user;
}
启动类:
ConfigClientApplication.java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
编写两个测试接口来获取配置中心的参数:
TestController.java
@RestController
@RefreshScope
public class TestController {
@Autowired
private GitConfig gitConfig;
@Autowired
private GitAutoRefreshConfig gitAutoRefreshConfig;
@GetMapping(value = "show")
public Object show(){
return gitConfig;
}
@GetMapping(value = "autoShow")
public Object autoShow(){
return gitAutoRefreshConfig;
}
}
启动client服务,测试接口:
更新配置文件
@RefreshScope 注解
目前客户端已经可以成功的获取到配置中心的配置参数,但是当配置参数改变时,需要重启client端才能获取到最新参数,这显然是在正常运行的项目中不可取的。所以提供了@RefreshScope注解,@RefreshScope的作用是重新拉取配置参数,在需要获取配置文件的类上加上这个注解,然后调用/actuator/refresh接口进行配置文件刷新即可。
本demo中的spring-boot-starter-actuator依赖和management配置就是为了这一点起作用的。
更新测试:
修改git上配置文件内容,然后调用/actuator/refresh接口获取最新配置。
注意刷新接口需要post请求:
再次调用测试接口:
很奇怪有没有,两种获取配置文件的方式,一种更新了,另一种没更新。百度说是由于@Value(“”)的实现方式的问题。所以再次提倡代码中用@ConfigurationProperties(prefix = “data”)这种方式进行配置参数获取。
至此配置参数的更新已经完成,但是手动更新很是不便,接着往下看,如何自动更新。
基于git的配置文件自动更新
webhooks给我提供了更新配置文件的时候去帮我们调用更新接口的方法,这样配置岂不是就完成了自动更新。
基于Spring Cloud Bus 的配置文件自动更新
这部分没有亲自实践,等后面搭建bus环境后再更新。