【发布时间】:2019-01-20 09:00:05
【问题描述】:
我在为我的 @FeignClient bean 设置全局 OkHttp 拦截器时遇到问题。我没有遇到任何错误,但拦截器被忽略了。
我的理解是 Spring Cloud 的自动配置应该选择我声明的 OkHttpClient.Builder bean 并使用它来创建底层的 OkHttpClient 实例,但我可能对此有误。
这是我的 Spring 应用程序的相关部分:
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeignConfig.class)
@EnableCircuitBreaker
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class);
}
}
@Configuration
public class FeignConfig {
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
@Bean
public OkHttpClient.Builder okHttpClientBuilder(MyInterceptor interceptor) {
return new OkHttpClient.Builder().addInterceptor(interceptor);
}
}
public class MyInterceptor implements okhttp3.Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
System.out.println("Hey there, this is my request: " + request);
Response response = chain.proceed(request);
System.out.println("Hey there, this is my response: " + response);
return response;
}
}
上面的intercept 方法永远不会被调用。我需要 MyInterceptor 成为 Spring bean,因为我需要向它注入其他依赖项。
@FeignClient(name = "myClient", fallback = MyClientFallback.class)
public interface MyClient {
// method declarations
}
@Component
public class MyClientFallback implements MyClient {
// method fallback implementations
}
这是我的application.properties 文件的相关部分:
feign.hystrix.enabled = true
feign.okhttp.enabled = true
ribbon.eureka.enabled = false
ribbon.eager-load.enabled = true
ribbon.eager-load.clients = myClient
myClient.ribbon.listOfServers = <IP_LIST>
myClient.ribbon.ServerListRefreshInterval = 10000
正如您从上面声明的属性中看到的,我没有使用 Eureka,而是使用 Ribbon 来对我的 rest 客户端进行负载平衡。我还使用 Hystrix 来启用回退响应,并且我已将 feign.okhttp.enabled 属性设置为 true。
以下是有关依赖配置和版本的信息...
Spring Boot 版本为2.0.3.RELEASE,Spring Cloud 版本为Finchley.SR1,而OkHttp 版本为3.11.0。
在我的pom.xml 文件中,我有这个spring-cloud-dependencies 配置:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
</dependencies>
</dependencyManagement>
我还包含了以下 Spring Boot 和 Spring Cloud 依赖项,以及 OkHttp 依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
...
</dependencies>
【问题讨论】:
-
如何使用提供的构建器创建 feign 客户端?我认为您的构建器未使用。
-
@MạnhQuyếtNguyễn 我认为 Spring 自动配置应该选择我声明的构建器并使用它来创建底层
OkHttp客户端。 -
你的 Feign 客户端是如何创建的?
-
@MạnhQuyếtNguyễn 我要把那部分交给 Spring Cloud
-
是类 MyInterceptor 编译。我看到缺少回报
标签: java rest spring-cloud spring-cloud-feign feign