This can be summarized in the performance tuning section, which, while very simple, can be surprisingly good;
Share a real case where our service is deployed overseas and the response is a bit exaggerated when accessing the service during a domestic visit; Some interfaces that return relatively large data take 600ms+ time, but our service RT is less than 20ms, and most of the overhead is spent on network transmission
In view of such a scenario, in addition to buy cloud service provider network channel, another intuitive idea is to reduce the size of the packet, directly configure gzip compression in nginx layer is a solution, this article mainly introduces the next, SpringBoot how to enable Gzip compression
I. Gizp compression configuration
1. The configuration
SpringBoot does not enable gzip compression by default, so you need to manually enable it by adding two lines to the configuration file
server:
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/plain,text/css,application/x-javascript
Copy the code
Note the mime-types in the above configuration. In spring2.0+, the default values are as follows, so we generally do not need to add this configuration
// org.springframework.boot.web.server.Compression#mimeTypes
/** * Comma-separated list of MIME types that should be compressed. */
private String[] mimeTypes = new String[] { "text/html"."text/xml"."text/plain"."text/css"."text/javascript"."application/javascript"."application/json"."application/xml" };
Copy the code
2. The test
Write a test demo
@RestController
public class HelloRest {
@GetMapping("bigReq")
public String bigReqList(a) {
List<String> result = new ArrayList<>(2048);
for (int i = 0; i < 2048; i++) {
result.add(UUID.randomUUID().toString());
}
returnJSON.toJSONString(result); }}Copy the code
Here is a comparison of datagrams before and after compression is turned on
3. The instructions
With the above configuration, gzip compression is enabled, but it is important to note that not all interfaces use gzip compression. By default, only 2048 bytes or more are compressed
If we need to change this value, we can just change the configuration
server:
compression:
min-response-size: 1024
Copy the code
II. The other
0. Project
Web Blog series
- 191018-SpringBoot Web tutorial Filter Usage Guide extension
- 191016-SpringBoot Tutorial web guide to Filter usage
- 191012- Custom Exception Handling HandlerExceptionResolver in the Web part of the SpringBoot tutorial series
- 191010- Global exception handling in SpringBoot Tutorial Web
- 190930- Configuration of 404 and 500 exception pages in SpringBoot Tutorial Web
- 190929- Redirection of the SpringBoot Tutorial Web chapter
- 190913-SpringBoot tutorial Web returns text, web pages, and image manipulation postures
- 190905-SpringBoot Series tutorial Web Chinese garble problem solving
- 190831- How to Customize the Parameter Parser in SpringBoot Tutorial Web
- 190828- Summary of Post request parameter resolution postures in the Web part of the SpringBoot tutorial series
- 190824-SpringBoot Tutorial Web chapter summary of Get Request parameter parsing posture
- 190822- Beetl Environment Setup in SpringBoot Tutorial Web
- 190820- Thymeleaf Setup for SpringBoot Tutorial Web
- 190816- Freemaker Environment setup in SpringBoot Tutorial Web
- 190421-SpringBoot Advanced WEB WebSocket instructions
- 190327-Spring-resttemplate urlencode parameter parsing exception
- 190317-Spring MVC web application building based on Java Config without XML configuration
- 190316-Spring MVC web Application Building based on XML configuration
- 190213- The temporary upload location XXX is not valid
Program source code
- Project: github.com/liuyueyi/sp…
- Project: github.com/liuyueyi/sp…
1. An ashy Blog
As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate
Below a gray personal blog, record all the study and work of the blog, welcome everyone to go to stroll
- A grey Blog Personal Blog blog.hhui.top
- A Grey Blog-Spring feature Blog Spring.hhui.top