欢迎关注博主公众号:【纯洁的明依】文章由陈晓阳原创。
本人微信:chenxiaoyangzxy. 免费提供本人大量学习资料。
SrpingCloud —github上面如何使用webHook实现配置文件修改 ,客户端自动刷新
一:客户端(消费者)端添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。
二Controller层开启自动更新机制
需要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值。
package com.demo.web;
import com.demo.feign.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by wo on 2018-03-08.
*/
@RestController
@RefreshScope// 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,
// 自动将新的配置更新到该类对应的字段中
public class ConsumeController {
@Autowired
private HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.dc();
}
@Value("${neo.hello}")
private String hello;
@RequestMapping("/hello")
public String from() {
return this.hello;
}
}
三:配置文件修改
springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置
management.security.enabled=false
四:测试
,以post请求的方式来访问http://localhost:port/refres 就会更新修改后的配置文件。
五:提交代码就自动调用客户端来更新
5.1## github上配置WebHook
参数说明:
Payload URL :触发后回调的URL
Content type :数据格式,两种一般使用json
Secret :用作给POST的body加密的字符串。采用HMAC算法
events :触发的事件列表。
events事件类型 描述
push 仓库有push时触发。默认事件
create 当有分支或标签被创建时触发
这样我们就可以利用hook的机制去触发客户端的更新,但是当客户端越来越多的时候hook支持的已经不够优雅,另外每次增加客户端都需要改动hook也是不现实的。其实Spring Cloud给了我们更好解决方案,后面文章来介绍。