我在OkHttp
为我的@FeignClient
bean 设置全局拦截器时遇到问题。我没有遇到任何错误,但是拦截器被忽略了。
我的理解是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 :
可以通过将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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句