SpringBoot Web tutorial series of articles open GZIP Data Compression

Benpian can be summarized in performance tuning articles, although the content is very simple, but the effect may be unexpected good;

Share a real case, our services are deployed overseas, when accessing services when the country visit, in response to an exaggeration; some relatively large return data interface, time-consuming 600ms + on, but our service is in rt 20ms or less, must most of the cost is spent on the transmission network

For such a scenario, except buying cloud service provider's network channels, in addition to an intuitive idea is to reduce the size of the data packets directly in the layer configuration nginx is a gzip compression program, under article describes, SpringBoot how to turn on gzip compression

I. gizp compression configuration

1. Configuration

SpringBoot default is not open gzip compression, we need to manually open, add two lines in the configuration file

server:
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/plain,text/css,application/x-javascript

Note that under the above configuration mime-types, in spring2.0 + version, the default values are as follows, so in general we do not need to deliberately 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" };

2. Test

Write a test demo

@RestController
public class HelloRest {
    @GetMapping("bigReq")
    public String bigReqList() {
        List<String> result = new ArrayList<>(2048);
        for (int i = 0; i < 2048; i++) {
            result.add(UUID.randomUUID().toString());
        }
        return JSON.toJSONString(result);
    }
}

The following is a comparison of data before and after opening the compressed packets

3. Description

While adding the above configuration, open the gzip compression, but need to pay attention not to say that all of the interfaces will use gzip compression, by default, will only compress more than 2048 bytes of content

If we need to change this value, you can modify the configuration

server:
  compression:
    min-response-size: 1024

II. Other

Project Scope

web series Bowen

项目源码

1. 一灰灰 Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

一灰灰blog

Guess you like

Origin www.cnblogs.com/yihuihui/p/11925743.html