Feign replace the default feign client []

Description:

feign default HTTP is used in the case of the JDK URLConnection original request sent to the connection pool is not used, but remain connected to each address length, i.e., the HTTP persistence connection. We can use the Apache HTTP client HTTP client to replace the original,, tuning service call by setting the connection pool timeout and so on. spring cloud support this replacement operation from after Brixtion.SR5 version.

1, the use of the Apache HTTP client feign replace the default client

a, project dependencies:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
</parent>

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Cloud OpenFeign的Starter的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- 使用Apache HttpClient替换Feign原生httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>8.17.0</version>
        </dependency>
</dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>

 

b, the project started categories:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class SpringCloudFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudFeignApplication.class, args);
    }
}

 

c, write test code:

client interfaces:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json 彩云天气API
@FeignClient(name = "caiyunapp", url = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552")
public interface HelloFeignService {

    @RequestMapping(value = "/forecast.json", method = RequestMethod.GET)
    ResponseEntity<byte[]> searchRepo();

}

controller class:

import cn.springcloud.book.feign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloFeignController {

    @Autowired
    private HelloFeignService helloFeignService;

    // 服务消费者对位提供的服务
    @GetMapping(value = "/search")
    public ResponseEntity<byte[]> searchGithubRepoByStr() {
        return helloFeignService.searchRepo();
    }

}

 

d, project configuration file:

server:
  port: 8011
spring:
  application:
    name: httpclient-demo

feign:
  httpclient:
      enabled: true

 

e, to start the project, browser access interface:

 

 

 Description Replace the default client success, the interface normal access.

 

2, using the default client OKHTTP replace feign

OKHTTP is more fire an HTTP client, has the following advantages:

  • Support SPDY, you can combine multiple requests to the same host.
  • Connection pooling reduction request latency.
  • The GZIP compression reduces the amount of data transferred.
  • In response to the request buffer to avoid duplication.

a, project dependencies:

With the above dependence similar to dependence exclude the following:

<!-- 使用OKHTTP无需这2个依赖 -->
<!--
使用Apache HttpClient替换Feign原生httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-httpclient</artifactId> <version>8.17.0</version> </dependency>

Add the following configuration:

<dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
</dependency>

 

B, in the coding based on the above, the preparation of a class configuration as follows:

import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class{FeignOkHttpConfig 
    @Bean 
    public okhttp3.OkHttpClient okHttpClient () {
         return  new new okhttp3.OkHttpClient.Builder ()
                  // Set connection timeout 
                .connectTimeout (60 , TimeUnit.SECONDS)
                 // Set the read timeout 
                .readTimeout (60 , TimeUnit.SECONDS)
                 / / set the write timeout 
                .writeTimeout (60 , TimeUnit.SECONDS)
                 // whether to automatically reconnect 
                .retryOnConnectionFailure ( to true ) 
                .connectionPool ( new new ConnectionPool ())
                // Construction OkHttpClient objects 
                .build (); 
    } 

}

 

c, the configuration file:

server:
  port: 8011
spring:
  application:
    name: okhttp-demo


feign:
    httpclient:
         enabled: false
    okhttp:
         enabled: true

 d, start the project, access interface, you can get to the normal data, indicating a successful replacement.

Guess you like

Origin www.cnblogs.com/idoljames/p/11680781.html