1、引用相关的 maven 包。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring cloud config 客户端包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>2、初始化配置文件
bootstrap.yml
spring: profiles: active: dev --- spring: profiles: prod application: name: config-single-client cloud: config: uri: :3301 label: master profile: prod --- spring: profiles: dev application: name: config-single-client cloud: config: uri: :3301 label: master profile: dev配置了两个版本的配置,并通过 spring.profiles.active 设置当前使用的版本,例如本例中使用的 dev 版本。
application.yml
server: port: 3302 management: endpoint: shutdown: enabled: false endpoints: web: exposure: include: "*" data: env: NaN user: username: NaN password: NaN其中 management 是关于 actuator 相关的,接下来自动刷新配置的时候需要使用。
data 部分是当无法读取配置中心的配置时,使用此配置,以免项目无法启动。
3、要读取配置中心的内容,需要增加相关的配置类,Spring Cloud Config 读取配置中心内容的方式和读取本地配置文件中的配置是一模一样的。可以通过 @Value 或 @ConfigurationProperties 来获取。
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }4、要读取配置中心的内容,需要增加相关的配置类,Spring Cloud Config 读取配置中心内容的方式和读取本地配置文件中的配置是一模一样的。可以通过 @Value 或 @ConfigurationProperties 来获取。
使用 @Value 的方式:
@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; }使用 @ConfigurationProperties 的方式:
@Component @Data @ConfigurationProperties(prefix = "data") public class GitAutoRefreshConfig { public static class UserInfo { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserInfo{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } } private String env; private UserInfo user; }4、增加一个 RESTController 来测试使用配置
@RestController public class GitController { @Autowired private GitConfig gitConfig; @Autowired private GitAutoRefreshConfig gitAutoRefreshConfig; @GetMapping(value = "show") public Object show(){ return gitConfig; } @GetMapping(value = "autoShow") public Object autoShow(){ return gitAutoRefreshConfig; } }5、项目启动类
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }启动项目,访问 RESTful 接口
:3302/show,结果如下:
{ "env": "localhost-dev-edit", "username": "fengzheng-dev", "password": "password-dev" }:3302/autoShow,结果如下:
{ "env": "localhost-dev-edit", "user": { "username": "fengzheng-dev", "password": "password-dev" } } 实现自动刷新Spring Cloud Config 在项目启动时加载配置内容这一机制,导致了它存在一个缺陷,修改配置文件内容后,不会自动刷新。例如我们上面的项目,当服务已经启动的时候,去修改 github 上的配置文件内容,这时候,再次刷新页面,对不起,还是旧的配置内容,新内容不会主动刷新过来。
但是,总不能每次修改了配置后重启服务吧。如果是那样的话,还是不要用它了为好,直接用本地配置文件岂不是更快。
它提供了一个刷新机制,但是需要我们主动触发。那就是 @RefreshScope 注解并结合 actuator ,注意要引入 spring-boot-starter-actuator 包。
1、在 config client 端配置中增加 actuator 配置,上面大家可能就注意到了。
management: endpoint: shutdown: enabled: false endpoints: web: exposure: include: "*"其实这里主要用到的是 refresh 这个接口