定义拦截器
@Slf4j
@Component
public class MyRibbonInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
log.info("host,path:{},{}",httpRequest.getURI().getHost(),httpRequest.getURI().getPath());
httpRequest.getHeaders().set("aaa","bbb");
return execution.execute(httpRequest,bytes);
}
}
@Configuration
public class WebConfig {
@Autowired
private MyRibbonInterceptor myRibbonInterceptor;
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(myRibbonInterceptor));
return restTemplate;
}