Categories
Uncategorized

Follow me SpringCloud | Chapter XX: Spring Cloud of okhttp

1. What is okhttp?

okhttp is open source company by the square an http client. On the Java platform, Java standard library provides classes for HTTP communication HttpURLConnection. But HttpURLConnection API itself is not friendly enough, the functionality provided is limited. Most Java programs are choosing to use Apache open source projects HttpClient as HTTP client. Apache HttpClient library of powerful, usage is also high.

2. Why use okhttp?

okhttp is designed is simple and efficient, which is why we selected it one of the important reasons. Its advantages are as follows:

    Supports HTTP / 2 protocol.

    Allow all requests to connect to the same host address, the request to improve efficiency.

    Share Socket, reducing the number of server requests.

    Through a connection pool, the request to reduce latency.

    Response data cache to reduce duplicate network request.

    Reduce the consumption of data traffic.

    Automatic processing GZip compression.

3. actual target

    Feign used okhttp alternative httpclient

    Alternatively httpclient Zuul used okhttp

4. okhttp in the Feign

First, some engineering structures, this demonstration project contains provider-server, consumer-server, eureka-server and zuul-server.

4.1 consumer-server-dependent pom.xml as follows:

代码清单:chapter19/consumer-server/pom.xml
***


    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
    
        io.github.openfeign
        feign-okhttp
    

    feign-okhttp do not need to specify the version here, feign-okhttp introduced the current version is 10.2.3, and okhttp version is 3.8.1, as:

4.2 Profile application.yml

代码清单:chapter19/consumer-server/src/main/resources/application.yml
***

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

    In the configuration file to be closed to use feign httpclient and open okhttp.

4.3 configuration class OkHttpConfig.java

代码清单:chapter19/consumer-server/src/main/java/com/springcloud/consumerserver/config/OkHttpConfig.java
***

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class OkHttpConfig {

    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .connectionPool(new ConnectionPool(10 , 5L, TimeUnit.MINUTES))
                .addInterceptor(new OkHttpLogInterceptor())
                .build();
    }
}

    Injection in the configuration class OkHttpClient Spring container, that we specify the size of the connection pool, the maximum number of holding connection 10, and is removed after 5 minutes of inactivity.

    Here the author is configured with a okhttp log interceptors.

4.4 log interceptor OkHttpLogInterceptor.java

代码清单:
***

@Slf4j
public class OkHttpLogInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        log.info("OkHttpUrl : " + chain.request().url());
        return chain.proceed(chain.request());
    }
}

    Here is the interface implemented okhttp3.Interceptor, not Spring Boot the Interceptor.

    Here simply print path okhttp request, check if there are traffic rights and other needs can be placed interceptor implemented.

Remote Feign calling code ignored, there is a need readers can get access Github repository.

5. okhttp in the Zuul

5.1 pom.xml added okhttp dependent

代码清单:chapter19/zuul-server/pom.xml
***


    io.github.openfeign
    feign-okhttp

5.2 configuration files open okhttp

代码清单:chapter19/zuul-server/src/main/resources/application.yml
***

ribbon:
  http:
    client:
      enabled: false
  okhttp:
    enabled: true

    Because the load balancing implementation Zuul is achieved through the Ribbon, so the configuration Http client is configured Ribbon natural components.

6. Test

We modify the idea to start the configuration, start the provider-server respectively in 8000 and 8001 ports, and in turn the rest of the project started, open a browser to access the link: http: // localhost: 8080 / consumer / hello, repeatedly refresh, you can see Hello Spring Cloud Port:! 8000 and Hello Spring Cloud Port:! 8001 book appear alternately, may prove load balancing has been successful, you can view consumer-server log, as follows:

2019-09-23 23:15:27.097  INFO 10536 --- [nio-9000-exec-5] c.s.c.intercepter.OkHttpLogInterceptor   : OkHttpUrl : http://host.docker.internal:8001/hello
2019-09-23 23:15:27.593  INFO 10536 --- [nio-9000-exec-6] c.s.c.intercepter.OkHttpLogInterceptor   : OkHttpUrl : http://host.docker.internal:8000/hello
2019-09-23 23:15:27.942  INFO 10536 --- [nio-9000-exec-7] c.s.c.intercepter.OkHttpLogInterceptor   : OkHttpUrl : http://host.docker.internal:8001/hello
2019-09-23 23:15:28.251  INFO 10536 --- [nio-9000-exec-9] c.s.c.intercepter.OkHttpLogInterceptor   : OkHttpUrl : http://host.docker.internal:8000/hello
2019-09-23 23:15:47.877  INFO 10536 --- [nio-9000-exec-8] c.s.c.intercepter.OkHttpLogInterceptor   : OkHttpUrl : http://host.docker.internal:8001/hello

You can see we have just log normal custom printing, now have access to really prove to be accessed by the okhttp.

7. Sample Code

Sample Code -Github

Sample Code -Gitee

Leave a Reply