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:



    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


    enabled: false
    enabled: true

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

4.3 configuration class


public class OkHttpConfig {

    public OkHttpClient okHttpClient(){
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(10 , 5L, TimeUnit.MINUTES))
                .addInterceptor(new OkHttpLogInterceptor())

    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


public class OkHttpLogInterceptor implements Interceptor {
    public Response intercept(Chain chain) throws IOException {"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



5.2 configuration files open okhttp


      enabled: false
    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