Recommended reading:

SpringCloud source code read 0-SpringCloud essential knowledge

SpringCloud source code read 1-EurekaServer source code secrets

SpringCloud source code read 2-Eureka client secrets

3 Ribbon Load Balancing

Sending HTTP requests is a very common thing in our development.

How to send an HTTP request

The most basic

1.1 Socket(Basic)

Socket(the most basic) the connection between the client and the server is ultimately Socket connection. In Java, the Socket of the client is connected to the ServerSocket of the server.

Use native Socket, to achieve. It is complicated and error-prone.

Hence, what we call a wheel.

The wheel

2.1 HttpURLConnection(native JDK)

Is JDK java.net.HttpURLConnection provides access to the basic function of the HTTP protocol class.

Java.net.HttpURLConnection inherited from java.net.URLConnection, the difference in with HttpURLConnection only for Http connections.

Java.net.HttpURLConnection and java.net.URLConnection are abstract classes, can not be instantiated. In the JDK sun.net.www.protocol.http.HttpURLConnection inherited java.net.HttpURLConnection, provides for the realization of the request is sent.

HttpURLConnection is difficult to handle sessions, cookies, etc.

2.2 HttpClient

HttpClient is Apache’s utility class for sending HTTP requests. The underlying layer must also encapsulate sockets

The point here is that there are two types of HttpClient

Org.apache.com mons. Httpclient. Httpclient: no longer update. Has been replaced by the Apache HttpComponents project HttpClient and HttpCore. Which is the new HttpClient, org. Apache.. HTTP client. HttpClient

. Org, apache HTTP client. HttpClient: the new HttpClient. This version is recommended.

2.3 OkHttpClient(Square)

A new generation of Http access clients. Compared to other wheels, it provides connection pooling, gziping, caching and other functions. Here does not explain in detail, oneself baidu. OkHttpClient is recommended as a tool for sending HTTP requests.

There are other frameworks that I won’t go through.

Advanced usage of sending HTTP requests

With wheels, we can send HTTP requests. But if you have a project where each developer uses a different wheel to send HTTP requests, the result is definitely not what you want

As a result.

For this creative need, we often use a factory pattern to simplify the instantiation part of the object, reduce the coupling of the code in the system, and increase the extensibility of the system.

3.1 ClientHttpRequestFactory

Factory for creating HTTP request clients. There is only one createRequest method. Used to create HTTP request clients. Return a ClientHttpRequest representing the request client.

  • SimpleClientHttpRequestFactory java.net.URLConnection factory
  • OkHttpClientHttpRequestFactory OkHttpClient factory
  • HttpComponentsClientHttpRequestFactory for HttpClient factory

With the factory model, it’s supposed to be pretty advanced. No, that’s not enough.

3.2 AbstractClientHttpRequestFactoryWrapper

For the AbstractClientHttpRequestFactoryWrapper ClientHttpRequestFactory further packaging. Implementation class:

  • BufferingClientHttpRequestFactory: make the factory to create ClientHttpRequest cache capacity.
  • InterceptingClientHttpRequestFactory: use the create ClientHttpRequest has the ability of the interceptor.

Look familiar? Decorator mode. Think about the use of decorators in IO.

InterceptingClientHttpRequestFactory class has a interceptors attributes used to store the interceptor. InterceptingClientHttpRequestFactory, when creating ClientHttpRequest interceptors will be encapsulated into ClientHttpRequest.

private final List<ClientHttpRequestInterceptor> interceptors;
Copy the code

ClientHttpRequestInterceptor: used to block the request before processing. Something like filter. executes the interceptor before sending the request.

Returns a BufferingClientHttpRequestWrapper BufferingClientHttpRequestFactory: createRequest method. BufferingClientHttpRequestWrapper response will be cached, in order to achieve the purpose of cache.

Conclusion:

The first usage:

  • Socket is the most basic

Second usage: wheels

  • The JDK native
  • The third party box

Level 3 usage: Design patterns

  • ClientHttpRequestFactory Factory mode
  • The factory pattern of AbstracClientHttpRequestFactoryWrapper with special function