Spring Cloud Config/Bus 数据总线自动刷新无效Webhook请求失败解决方案

Spring Cloud Config/Bus 自动刷新无效Webhook请求失败解决方案

过滤bus-refresh请求,自定义发送refresh请求

一、github 的 Webhooks 配置
在这里插入图片描述

二、自定义过滤器
我是在config-server定义的过滤器,拦截bus-refresh请求后,调用 http://config-client:port/actuator/refresh

package com.jvli.project.filter;
 
import javax.servlet.*; 
import javax.servlet.annotation.WebFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
 

@WebFilter(urlPatterns = "/actuator/bus-refresh")
public class WebHookFilter implements Filter {
	
	@Value("${client.url}")
	private String clientUrl;
	
	@Value("${client.port}")
	private String port;
	
    @Override
    public void init(FilterConfig filterConfig) {
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	try {
    		 
    	   	 RestTemplate restTemplate = new RestTemplate();
             HttpHeaders httpHeaders = new HttpHeaders();
             httpHeaders.add(HttpHeaders.CONTENT_TYPE,"application/json");
             HttpEntity<MultiValueMap<String, String>> httprequest = new HttpEntity<>(null,httpHeaders);
             StringBuffer sb = new StringBuffer();
             String requestUrl = sb.append("http://").append(clientUrl).append(":").append(port).append("/actuator/refresh").toString();
         
             ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(requestUrl, httprequest, String.class);
             System.err.println("请求bus-refresh返回结果>>>>>>>>>>>>>>>" + stringResponseEntity.getBody());
    	}catch (Exception e) {
			e.printStackTrace();
		}
 
    }
 
    @Override
    public void destroy() {}
 
 
}

启动类添加扫描注解
@ServletComponentScan(basePackages = “com.jvli.project.filter”)

package com.jvli.project;

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@ServletComponentScan(basePackages = "com.jvli.project.filter")
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfigserverApplication {
	
	private static final Logger logger = LoggerFactory.getLogger(SpringCloudConfigserverApplication.class);
	
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigserverApplication.class, args);
		System.out.println("<<<<<<<<<<<<<<<<<<Config配置中心服务端启动>>>>>>>>>>>>>>>>>>");
	}

}

亲测有效,希望能够帮助到和我一样在前行道路上的朋友

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值