一, 简介
既然项目中已经有了配置中心 config-server , 那么我们怎么去消费配置服务呢? 当然就是 config-client ;
二, 创建 config-client 服务 cloud-g
1, pom.xml
4.0.0 com.gy.cloud cloud 1.0-SNAPSHOT cloud-g 0.0.1-SNAPSHOT jar ${project.artifactId} Demo project for config-client org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-actuator
2, bootstrap.properties
# 和 application 配置文件相比, bootstrap 配置文件具有以下几个特性:# 1, bootstrap 由父 ApplicationContext 加载,比 application 优先加载;# 2, bootstrap 里面的属性不能被覆盖;# 3, bootstrap 和application 的应用场景:# application: 主要用于spring boot 项目的自动化配置;# bootstrap:# a, 使用 spring Cloud config 配置中心时, 这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;# b, 一些固定的不能被覆盖的配置; c, 一些加密/解密的场景;server.port=8767spring.application.name=config-clienteureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=config-server# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)#spring.cloud.config.uri= http://localhost:8766/spring.cloud.config.label=masterspring.cloud.config.profile=dev# 开放配置刷新接口 /actuator/refreshmanagement.endpoints.web.exposure.include=refresh
3, CloudGApplication
package com.gy.cloud.cloudg;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RefreshScope // 配置刷新@RestController@EnableDiscoveryClient@SpringBootApplicationpublic class CloudGApplication { public static void main(String[] args) { SpringApplication.run(CloudGApplication.class, args); System.out.println("=== 服务G config-client 启动SUCCESS ==="); } @Value("${testUsername}") private String param; @GetMapping("getConfigParam") public String getConfigParam() { return param; }}
4, 启动 cloud-g
启动日志 :
2019-01-11 11:03:04.093 INFO 10844 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://XUQ3937QAYKT5WO:8766/2019-01-11 11:03:04.729 INFO 10844 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=f2f23650522db4787e8a2a7d3b030058a8598899, state=null2019-01-11 11:03:04.730 INFO 10844 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/ge.yang/SpringBoot//src/main/resources/config/config-client-dev.properties'}]}......2019-01-11 11:03:09.462 INFO 10844 --- [ main] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient2019-01-11 11:03:09.474 INFO 10844 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'2019-01-11 11:03:09.486 INFO 10844 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map)
根据启动日志, 发现 POST 接口 : /actuator/refresh
1, 访问 : http://localhost:8767/getConfigParam
2, 修改Git仓库 testUsername 值 访问 : http://localhost:8767/getConfigParam
3, POST 访问 : http://localhost:8767/actuator/refresh
4, 访问 : http://localhost:8767/getConfigParam
三, 总结
注意 1, bootstrap 与 application 的区别;
2, 注意 POST /actuator/refresh 刷新接口的使用;
学习文档
:
项目源码: