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
- 191018-SpringBoot chapter of the series of tutorials web filter Filter Guide extension piece
- 191016-SpringBoot chapter of the series of tutorials web filter Filter Guide
- 191012-SpringBoot series of tutorials custom web articles of exception handling HandlerExceptionResolver
- 191010-SpringBoot global web tutorial series of articles Exception Handling
- 404, 500 exception page 190930-SpringBoot chapter of the series of tutorials web configuration
- 190929-SpringBoot series of articles tutorials web redirection
- 190913-SpringBoot chapter of the return series of tutorials web text, web pages, images operation posture
- 190905-SpringBoot 系列教程 web 篇之中文乱码问题解决
- 190831-SpringBoot 系列教程 web 篇之如何自定义参数解析器
- 190828-SpringBoot 系列教程 web 篇之 Post 请求参数解析姿势汇总
- 190824-SpringBoot 系列教程 web 篇之 Get 请求参数解析姿势汇总
- 190822-SpringBoot 系列教程 web 篇之 Beetl 环境搭建
- 190820-SpringBoot 系列教程 web 篇之 Thymeleaf 环境搭建
- 190816-SpringBoot 系列教程 web 篇之 Freemaker 环境搭建
- 190421-SpringBoot 高级篇 WEB 之 websocket 的使用说明
- 190327-Spring-RestTemplate 之 urlencode 参数解析异常全程分析
- 190317-Spring MVC 之基于 java config 无 xml 配置的 web 应用构建
- 190316-Spring MVC 之基于 xml 配置的 web 应用构建
- 190213-SpringBoot 文件上传异常之提示 The temporary upload location xxx is not valid
项目源码
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 项目:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/210-web-filter
1. 一灰灰 Blog
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
- 一灰灰 Blog 个人博客 https://blog.hhui.top
- 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top