1, 建立配置文件工程,git地址为:
2, 建立config服务工程
@EnableConfigServer@SpringBootApplicationpublic class ConfigServiceApplication { public static void main(String[] args) { SpringApplication.run(ConfigServiceApplication.class, args); }}
server.port=8888# 配置git仓库地址spring.cloud.config.server.git.uri=http://Gitlab.xxx.com/xx/xx_configuration.gitspring.cloud.config.server.git.username=xxxspring.cloud.config.server.git.password=xxxx#Git分支spring.cloud.config.label=master#spring.cloud.config.server.git.search-paths=src/main/resources/config
4.0.0 com.example marketing-config 0.0.1-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE UTF-8 1.8 org.springframework.cloud spring-cloud-config-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Finchley.SR2 pom import org.springframework.boot spring-boot-maven-plugin
3, 配置Client工程
@SpringBootApplicationpublic class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}@RefreshScope@RestControllerclass MessageRestController { @Value("${message1:Hello default}") private String message; @RequestMapping("/message") String getMessage() { return this.message; }}
server.port=8889#对应仓库中ymlspring.application.name=client1management.endpoints.web.exposure.include=*spring.cloud.config.profile=dev spring.cloud.config.label=masterspring.cloud.config.uri=http://localhost:8888
4.0.0 com.example configuration-client 0.0.1-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE UTF-8 1.8 org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Finchley.SR2 pom import org.springframework.boot spring-boot-maven-plugin
3,验证
先启动Service工程,访问 ,如果能看到配置的属性说明服务启动成功。
再启动Client工程,访问:
Config Server启动以后,我们可以通过它暴露的端点获取配置文件内容,http请求地址与配置文件映射关系如下:
# 映射{application}-{profile}.properties文件/{application}/{profile}/[{label}]/{label}/{application}-{profile}.properties/{application}-{profile}.properties/{label}/{application}-{profile}.yml/{application}-{profile}.yml{application}通常使用微服务名称,对应Git仓库中文件名的前缀;
{profile}对应{application}-
后面的dev、pro、test等; {label}对应Git仓库的分支名,默认为master。
4,自动刷新
修改client-dev.yml的属性,然后再用postman以post方式请求http://localhost:8889/actuator/refresh。
再次访问, 可以看到内容变化。
参考资料: