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配置中心服务端启动>>>>>>>>>>>>>>>>>>");
}
}
亲测有效,希望能够帮助到和我一样在前行道路上的朋友