SrpingCloud ---github上面如何使用webHook实现配置文件修改 ,客户端自动刷新

欢迎关注博主公众号:【纯洁的明依】文章由陈晓阳原创。
本人微信: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给了我们更好解决方案,后面文章来介绍。

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纯洁的明依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值