Я использую @FeignClient
(при поддержке Ribbon в качестве LB и Eureka в качестве реестра служб).
Я хотел бы написать перехватчик, который вызывается при отправке запросов на распределение нагрузки.
Я нашел RetryLoadBalancerInterceptor
от Spring Cloud, но не могу сделать так, чтобы его вызывали.
Я использую следующую конфигурацию:
@Configuration
@AutoConfigureAfter({RibbonAutoConfiguration.class})
@AutoConfigureBefore({LoadBalancerAutoConfiguration.RetryInterceptorAutoConfiguration.class})
class InterceptorConfig {
@Bean
public RetryLoadBalancerInterceptor ribbonInterceptor(LoadBalancerClient loadBalancerClient,
LoadBalancerRetryProperties properties,
LoadBalancerRequestFactory requestFactory,
LoadBalancedRetryFactory loadBalancedRetryFactory) {
return new RibbonRetryInterceptor(loadBalancerClient, properties, requestFactory, loadBalancedRetryFactory);
}
}
RibbonRetryInterceptor
является моей реализацией и выглядит следующим образом:
public class RibbonRetryInterceptor extends RetryLoadBalancerInterceptor {
public RibbonRetryInterceptor(LoadBalancerClient loadBalancer, LoadBalancerRetryProperties lbProperties,
LoadBalancerRequestFactory requestFactory, LoadBalancedRetryFactory lbRetryFactory) {
super(loadBalancer, lbProperties, requestFactory, lbRetryFactory);
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
System.out.println("Intercepting the request...");
return super.intercept(request, body, execution);
}
}
Метод перехвата никогда не вызывается.
Я использую Spring Cloud BOM Greenwich.RELEASE, вот мой `pom.xml``
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acme</groupId>
<artifactId>address.service.client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>address.service.client</name>
<url>http://www.acme.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Required for health checks and info pages advertised by the service
and read by Eureka -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Required to enable hystrix / circuit breaking -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- For use of Feign client for HTTP / REST requests. -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Required for Ribbon-Loadbalanced REST Templates to do retries. -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<!-- For custom Ribbon Client Implementation -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>