Spring Cloud 集成 WebHook

绿林寻猫
2021-12-08 / 0 评论 / 176 阅读 / 正在检测是否收录...

1.配置 WebHook 地址

登录 GitHub,点击 GitHub 的 WebHook 菜单,右侧面板中 Payload URL 填写 <配置中心 url>/actuator/bus-refresh, Content-type 选择 applicaton/json,保存即可。

因为需要用到外网,这里使用natapp外网穿透做外网映射

设置 WebHook 操作:

 

2.修改config-server

添加:

import org.springframework.stereotype.Component;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import java.io.ByteArrayInputStream;import java.io.IOException;@Componentpublic class WebHookFilter implements Filter {    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String url = new String(httpServletRequest.getRequestURI());        // 只过滤 /actuator/bus-refresh 请求
        if (!url.endsWith("/actuator/bus-refresh")) {
            chain.doFilter(request, response);            return;
        }        // 使用 HttpServletRequest 包装原始请求达到修改 post 请求中 body 内容的目的
        CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest);

        chain.doFilter(requestWrapper, response);

    }    @Override
    public void destroy() {

    }    private class CustometRequestWrapper extends HttpServletRequestWrapper {        public CustometRequestWrapper(HttpServletRequest request) {            super(request);
        }        @Override
        public ServletInputStream getInputStream() throws IOException {            byte[] bytes = new byte[0];
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);            return new ServletInputStream() {                @Override
                public boolean isFinished() {                    return byteArrayInputStream.read() == -1 ? true : false;
                }                @Override
                public boolean isReady() {                    return false;
                }                @Override
                public void setReadListener(ReadListener readListener) {

                }                @Override
                public int read() throws IOException {                    return byteArrayInputStream.read();
                }
            };
        }
    }
}

 

 

3.测试

  1. 完成以上配置后,重启项目

  2. 修改github文件

  3. 再执行 http://localhost/test/getConfigInfo

因为网络原因,等几秒钟加载几次就能拉去新的数据了

 

 

 

 

 

 

 

 

0

评论 (0)

取消