Why does HTTP connection pooling improve performance?

Background principles of HTTP

A. Establishing an HTTP connection between two servers is a very complicated process, involving the exchange of multiple packets and consuming a lot of time. B. The 3 handshakes and 4 breakups required for Http connections are expensive, even more so for the large number of relatively small Http messages.

Optimized solution

A. If we directly use HTTP connection pool, we save a lot of 3 handshakes and 4 breakups; This can greatly improve throughput. B. Feign’s HTTP client supports three frameworks; HttpURLConnection, httpClient, okHTTP; The default is HttpURLConnection. C. Traditional HttpURLConnection is native to the JDK and does not support connection pooling. If you want to implement connection pooling, you need to manage connection objects yourself. For relatively complex operations such as network requests, there is no need to manage the connection objects yourself if alternative schemes are available. D. HttpClient compared with the traditional JDK native HttpURLConnection, it encapsulates HTTP access headers, parameters, content body, response, etc.; It not only makes it easy for clients to send HTTP requests, but also facilitates developers to test interfaces (based on HTTP protocol), which improves the efficiency of development and the robustness of the code. In addition, when high concurrency large number of requests network, or use “connection pool” to improve throughput.

Case implementation

1. Add related dependencies

Add httpClient dependencies and add Feign support for httpClient

<! - using Apache HttpClient replace Feign native httpURLConnection - > < the 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 >Copy the code

2. Modify the configuration file

Enable Feign Settings for httpClient in the configuration file.

# enable httpClient feign.httpClient. enabled=trueCopy the code

Note: If you use HttpClient as Feign’s client tool. Note the annotations on the definition interface if a custom object is passed (the object will be delivered in JSON format). You need to specify the type.

consumes=MediaType.APPLICATION_JSON_VALUE)

Copy the code
/** ** Product * @author Administrator ** / @requestMapping ("/ Product ") public interface ProductService @RequestMapping(value="/findAll",method=RequestMethod.GET) public List<Product> findAll(); @requestMapping (value="/getProductById",method= requestmethod.get) public Product @requestMapping (value="/getProductById",method= requestmethod.get) public Product getProductById(@RequestParam("id") Integer id); // Add goods to pass multiple parameters @requestMapping (value="/add",method= requestmethod. GET) public Product addProduct(@requestParam ("id") Integer id,@RequestParam("name") String name); / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Httpclient -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / add goods way for passing multiple arguments. 2: POST way @ RequestMapping (value = "/ add2", method = RequestMethod. POST, consumes = MediaType. APPLICATION_JSON_VALUE) public Product addProduct2(@RequestBody Product product); // Use the HttpClient tool to add goods passing multiple parameters: Based on the GET method @ RequestMapping (value = "/ add3", method = RequestMethod. GET, consumes = MediaType. APPLICATION_JSON_VALUE) public Product addProduct3(Product product); }Copy the code