前言介绍在实际开发中经常会有一个叫做配置中心的服务,这个服务经过变更参数来动态刷新线上业务数据行为配置。比如;行为开关、活动数据、黑白名单、本地/预发/线上环境切换等等,这些配置信息往往需要在我们不重启系统的时候就可以被更新执行。那么我们一般会使用具备此类属性在分布式系统中适合的组件进行开发配置中心,像是zookeeper、redis发布订阅、或者http定时轮许拉取,他们都可以做成统一配置中心服务。而在Spring Cloud Config 中,默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,在加上Github的Webhook钩子服务,可以在我们push等行为操作的时候,自动执行我们的http行为,以达到自动刷新配置服务。 环境准备
案例说明通过在个人Git创建配置服务工程,开启Webhooks服务添加回调钩子http://xxx:port/actuator/refresh在更新配置后自动刷新服务配置内容,如图; 代码示例itstack-demo-springcloud-06├── itstack-demo-springcloud-config-client │ └── src │ └── main │ ├── java │ │ └── org.itstack.demo │ │ ├── web │ │ │ └── ConfigClientController.java │ │ └── ConfigClientApplication.java │ └── resources │ ├── application.yml │ └── bootstrap.yml └── itstack-demo-springcloud-config-server └── src └── main ├── java │ └── org.itstack.demo │ └── ConfigServerApplication.java └── resources └── application.yml 完整代码欢迎关注公众号:bugstack虫洞栈 回复“SpringCloud专题”进行下载 itstack-demo-springcloud-config-client | 配置获取客户端方,提供自动刷新Http
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 * 论坛:http:// * Create by 付政委 on @2019 */@RestController@RefreshScopepublic class ConfigClientController {@Value("${info.profile:error}")private String profile;@GetMapping("/config")public Mono<String> config() {return Mono.justOrEmpty(profile);}}
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 * 论坛:http:// * Create by 付政委 on @2019 */@SpringBootApplicationpublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}}
spring: application:name: itstack-demo-springcloud-config-client server: port: 9001# /actuator/refresh 这个 Endpoint 暴露出来 management: endpoints:web: exposure:include: refresh
spring: cloud:config: uri: http://localhost:7397 # 配置中心的具体地址;itstack-demo-springcloud-config-server name: config-client # 对应 {application} 部分,例如;config-client-dev = 只取最后一个符号'-'之前的 profile: dev # 对应 {profile} 部分 label: master # 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用 #配置文件会被转换成 Web,访问规则如下; #/{application}/{profile}[/{label}]#/{application}-{profile}.yml #/{label}/{application}-{profile}.yml #/{application}-{profile}.properties #/{label}/{application}-{profile}.properties itstack-demo-springcloud-config-server | 配置提供服务端方,链接Git配置工程地址
/** * 微信公众号:bugstack虫洞栈 | 沉淀、分享、成长,专注于原创专题案例 * 论坛:http:// * Create by 付政委 on @2019 */@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}
server: port: 7397spring: application:name: itstack-demo-springcloud-config cloud:config: server:git: uri: https://github.com/fuzhengwei/itstack-demo-config # 换成自己的配置Git仓库的地址,如果没有可以新建工程地址,也可以克隆我的 search-paths: config-repo # Git仓库地址下的底层配置文件名称,如果配置多个用逗号','分割。 # 如果配置中心需要访问权限,则开启配置 # spring.cloud.config.server.git.username:Github账户 # spring.cloud.config.server.git.password:Github密码 测试验证
dev bus 综上总结
|
|