我在为自己的OkHttp
bean设置全局@FeignClient
拦截器时遇到问题。我没有遇到任何错误,但是拦截器被忽略了。
我的理解是,Spring Cloud的自动配置应该选择我声明的OkHttpClient.Builder
bean,并使用它创建基础的OkHttpClient
实例,但是我对此可能是错的。
这是我的Spring应用程序的相关部分:
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeignConfig.class)
@EnableCircuitBreaker
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(GeoSigoStarter.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来平衡其余客户端的负载。我还使用Hystrix启用后备响应,并将
feign.okhttp.enabled
属性设置为true
。以下是有关dependecies配置和版本的信息...
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>
最佳答案
您应该提供一个OkHttpClient
bean作为stated in the doc:
可以通过将feign.okhttp.enabled或feign.httpclient.enabled分别设置为true并将它们放在类路径中来使用OkHttpClient和ApacheHttpClient feign客户端。您可以通过提供使用Apache时的ClosableHttpClient Bean或使用OK HTTP的OkHttpClient来定制HTTP客户端。
https://github.com/OpenFeign/feign/blob/master/okhttp/src/main/java/feign/okhttp/OkHttpClient.java
关于java - 将OkHttp自定义拦截器添加到Feign客户端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51825568/