gpt4 book ai didi

java - 为 Feign 客户端添加 OkHttp 自定义拦截器

转载 作者:行者123 更新时间:2023-11-30 07:45:21 24 4
gpt4 key购买 nike

我在为我的 @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,因为我需要向它注入(inject)其他依赖项。


@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.SR1OkHttp版本为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 :

The OkHttpClient and ApacheHttpClient feign clients can be used by setting feign.okhttp.enabled or feign.httpclient.enabled to true, respectively, and having them on the classpath. You can customize the HTTP client used by providing a bean of either ClosableHttpClient when using Apache or OkHttpClient whe using OK HTTP.

https://github.com/OpenFeign/feign/blob/master/okhttp/src/main/java/feign/okhttp/OkHttpClient.java

关于java - 为 Feign 客户端添加 OkHttp 自定义拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com