代码之家  ›  专栏  ›  技术社区  ›  fps

将OkHttp自定义拦截器添加到外部客户端

  •  2
  • fps  · 技术社区  · 6 年前

    我在建立一个全球性的 OkHttp @FeignClient 豆。我没有遇到任何错误,但拦截程序被忽略。

    我的理解是SpringCloud的自动配置应该选择 OkHttpClient.Builder 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


    @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>
    

    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>
    
    2 回复  |  直到 6 年前
        2
  •  1
  •   fps    5 年前

    解决方案是让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>
    

    完成此操作后,所提供配置的所有功能都将按预期工作。