一、当ConfigServer启动后,假如咱们新增配置内容的话,是否是要从新启动一下ConfigServer呢? 二、答案确定是不须要从新启动的,由于 SpringCloud 给咱们提供了一个刷新的触发机制,这样即可以在不从新的状况下从新加载最新配置文件内容; 三、这里还顺便列举下配置路径的规则: /**************************************************************************************** * 配置服务的路劲规则: * * /{application}/{profile}[/{label}] * /{application}-{profile}.yml * /{label}/{application}-{profile}.yml * /{application}-{profile}.properties * /{label}/{application}-{profile}.properties ****************************************************************************************/
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springms-config-client-refresh</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- 客户端配置模块 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 监控和管理生产环境的模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
server: port: 8295 ##################################################################################################### # 配置服务客户端Client应用入口(正常测试 ConfigClient ) # profile: profile-dev ##################################################################################################### ##################################################################################################### # 配置服务客户端Client应用入口(连接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?) # profile: profile-local-dev #####################################################################################################
##################################################################################################### # 配置服务客户端Client应用入口(连接 ClientServer 测试) spring: cloud: config: uri: http://localhost:8220 profile: refresh label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master application: name: foobar #取 foobar-refresh.yml 这个文件的 application 名字,即为 foobar 名称 #####################################################################################################
package com.springms.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 配置客户端Controller。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/10/18 * */ @RestController @RefreshScope public class ConfigClientRefreshController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 单点手动动态刷新ConfigClient配置。<br/> * * ConfigClient 配置客户端服务想要实现自动刷新配置的话,ConfigServer 一端是不要作任何处理,只须要在 ConfigClient 一端处理便可。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/10/18 * */ @SpringBootApplication public class MsConfigClientRefreshApplication { public static void main(String[] args) { SpringApplication.run(MsConfigClientRefreshApplication.class, args); System.out.println("【【【【【【 ConfigClientRefresh微服务 】】】】】】已启动."); } }
/**************************************************************************************** application.yml 涉及到的连接文件内容展现以下: 修改内容前: http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml profile: profile-refresh 修改内容后: http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml profile: profile-refresh-refresh ****************************************************************************************/ /**************************************************************************************** 1、配置刷新服务客户端Client应用入口(单点手动动态刷新配置服务客户端配置): 一、添加注解 RefreshScope,而后添加引用模块 spring-boot-starter-actuator 监控和管理生产环境的模块; 二、编辑 application.yml 文件,添加相关客户端配置; spring: cloud: config: uri: http://localhost:8220 profile: refresh label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master application: name: foobar #取 foobar-refresh.yml 这个文件的 application 名字,即为 foobar 名称 三、启动 springms-config-server 模块服务,启动1个端口; 四、启动 springms-config-client-refresh 模块服务,启动1个端口; 五、在浏览器输入地址 http://localhost:8295/profile 正常状况下会输出远端服务的配置内容(内容为:profile: profile-refresh); 六、修改 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refresh.yml 内容,修改后为 profile: profile-refresh-refresh; 七、打开windows命令窗口,执行命令: >curl.exe -X POST http://localhost:8295/refresh 八、而后刷新 http://localhost:8295/profile 网页,正常状况下会输出远端服务的配置内容(内容为:profile: profile-refresh-refresh); 总结:这里经过执行刷新命令才得以将远端配置内容刷新到配置服务客户端。 ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitjava
SpringCloudTutorial交流QQ群: 235322432git
SpringCloudTutorial交流微信群: 微信沟通群二维码图片连接web
欢迎关注,您的确定是对我最大的支持!!!spring