我有问题为我的OkHttp
豆设置一个全球@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
。
以下是有关依赖项配置和版本的信息......
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假装客户端。您可以通过在使用Apache时使用ClosableHttpClient的bean或使用OK HTTP提供OkHttpClient来自定义所使用的HTTP客户端。
https://github.com/OpenFeign/feign/blob/master/okhttp/src/main/java/feign/okhttp/OkHttpClient.java
解决方案是让Spring自动配置完成它的工作。
为了实现这一点,必须从pom.xml
文件中删除以下依赖项:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
必须手动包含以下内容:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
完成此操作后,一切都按预期的方式使用提供的配置。