服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 浅谈Spring Boot Web 应用性能优化

浅谈Spring Boot Web 应用性能优化

2021-05-20 14:02mercyblitz Java教程

这篇文章主要介绍了浅谈Spring Boot Web 应用性能优化,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

默认情况下,spring boot web 应用会装配一些功能组件 bean。

在大多数 web 应用场景下,可以选择性地关闭一下自动装配的spring 组件 bean,以达到提升性能的目的。

配置项优化

spring boot web 应用加速 完整配置项

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
management.add-application-context-header = false
spring.mvc.formcontent.putfilter.enabled = false
 
spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\
org.springframework.boot.autoconfigure.jmx.jmxautoconfiguration,\
org.springframework.boot.autoconfigure.gson.gsonautoconfiguration,\
org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration,\
org.springframework.boot.autoconfigure.jdbc.xadatasourceautoconfiguration,\
org.springframework.boot.autoconfigure.jdbc.jndidatasourceautoconfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.jtaautoconfiguration,\
org.springframework.boot.autoconfigure.websocket.websocketautoconfiguration,\
org.springframework.boot.autoconfigure.websocket.websocketmessagingautoconfiguration,\
org.springframework.boot.autoconfigure.freemarker.freemarkerautoconfiguration,\
org.springframework.boot.autoconfigure.groovy.template.groovytemplateautoconfiguration,\
org.springframework.boot.autoconfigure.mustache.mustacheautoconfiguration,\
org.springframework.boot.autoconfigure.mail.mailsenderautoconfiguration,\
org.springframework.boot.autoconfigure.mail.mailsendervalidatorautoconfiguration,\
org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration,\
org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration,\
org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration

配置项汇总

?
1
2
3
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration,\
org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration,\
org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration

关闭 web 请求跟踪 自动装配

org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration

顾名思义,该自动装配用跟踪 web 请求,通过servlet filter org.springframework.boot.actuate.trace.webrequesttracefilter 记录请求的信息(如:请求方法、请求头以及请求路径等),其计算的过程存在一定的开销,使用场景罕见,故可选择关闭。

配置项

?
1
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration

org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration

org.springframework.boot.actuate.autoconfigure.tracewebfilterautoconfiguration关闭后,其请求信息存储介质org.springframework.boot.actuate.trace.tracerepository没有存在的必要,故可选择关闭。

配置项

?
1
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.tracerepositoryautoconfiguration

关闭 web 请求结果指标 自动装配

org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration

该组件将自动装配org.springframework.boot.actuate.autoconfigure.metricsfilter,该 filter主要记录web 请求结果指标(如:相应状态码、请求方法执行时间等),该信息一定程度上与反向代理服务器(nginx)功能重叠,故可选择关闭。

配置项

?
1
spring.autoconfigure.exclude = org.springframework.boot.actuate.autoconfigure.metricfilterautoconfiguration

可关闭 servlet web 组件

org.springframework.web.filter.httpputformcontentfilter

引入版本

org.springframework.web.filter.httpputformcontentfilter 由 spring framework 3.1 版本引入,分发在 org.springframework:spring-web 中。

使用场景

通常 web 场景中,浏览器通过 http get 或者 post 请求 提交 form 数据,而非浏览器客户端(如应用程序)可能通过 http put 请求来实现。

当 http 请求头content-type 为 application/x-www-form-urlencoded 时,form 数据被 encoded。而 servlet 规范中, servletrequest.getparameter*()方法仅对 http post 方法支持请求参数的获取,如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public intetfacce servletrequest {
 
 ......
 
 public string getparameter(string name);
 
 public enumeration<string> getparameternames();
 
 public string[] getparametervalues(string name);
 
 public map<string, string[]> getparametermap();
 
 ......
 
}

故 以上方法无法支持 http put 或 http patch 请求方法(请求头content-typeapplication/x-www-form-urlencoded)。

org.springframework.web.filter.httpputformcontentfilter 正是这种场景的解决方案。

spring boot 默认场景下,将org.springframework.web.filter.httpputformcontentfilter org.springframework.boot.autoconfigure.web.webmvcautoconfiguration 自动装配,以下为 spring boot1.4.1.release 以及更好版本定义(可能存在一定的差异):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@configuration
@conditionalonwebapplication
@conditionalonclass({ servlet.class, dispatcherservlet.class,
  webmvcconfigureradapter.class })
@conditionalonmissingbean(webmvcconfigurationsupport.class)
@autoconfigureorder(ordered.highest_precedence + 10)
@autoconfigureafter({ dispatcherservletautoconfiguration.class,
  validationautoconfiguration.class })
public class webmvcautoconfiguration {
 
 ......
 
 @bean
 @conditionalonmissingbean(httpputformcontentfilter.class)
 @conditionalonproperty(prefix = "spring.mvc.formcontent.putfilter", name = "enabled", matchifmissing = true)
 public orderedhttpputformcontentfilter httpputformcontentfilter() {
  return new orderedhttpputformcontentfilter();
 }
 
 ......
 
}

综上所述,org.springframework.web.filter.httpputformcontentfilter 在绝大多数 web 使用场景下为非必须组件。

配置项

如果应用依赖 spring boot 版本 为 1.4.1.release 以及更高的版本,可通过如下配置,进行将 org.springframework.web.filter.httpputformcontentfilter 关闭:

?
1
spring.mvc.formcontent.putfilter.enabled = false

org.springframework.web.filter.hiddenhttpmethodfilter

引入版本

org.springframework.web.filter.hiddenhttpmethodfilter 由 springframework 3.0 版本引入,分发在org.springframework:spring-web 中。

使用场景

当 web 服务端同一资源(url)提供了多请求方法的实现,例如 uri :/update 提供了http post 以及 http put 实现),通常 web 场景中,浏览器仅支持 http get或者 post 请求方法,这样的话,浏览器无法发起 http put 请求。

为了浏览器可以消费 http put 资源, 需要在服务端将 http post 转化成http put 请求,为了解决这类问题,spring 引入org.springframework.web.filter.hiddenhttpmethodfilter web 组件。

当浏览器 发起 http post 请求时,可通过增加请求参数(默认参数名称:"_method")的方式,进行http 请求方法切换,
org.springframework.web.filter.hiddenhttpmethodfilter 获取参数"_method"值后,将参数值作为httpservletrequest#getmethod()的返回值,给后续 servlet实现使用。

出于通用性的考虑,org.springframework.web.filter.hiddenhttpmethodfilter通过调用 #setmethodparam(string) 方法,来修改转换请求方法的参数名称。

spring boot 默认场景下,将org.springframework.web.filter.httpputformcontentfilter org.springframework.boot.autoconfigure.web.webmvcautoconfiguration 自动装配,以下为 spring boot 1.4.1.release 以及更好版本定义(可能存在一定的差异):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@configuration
@conditionalonwebapplication
@conditionalonclass({ servlet.class, dispatcherservlet.class,
  webmvcconfigureradapter.class })
@conditionalonmissingbean(webmvcconfigurationsupport.class)
@autoconfigureorder(ordered.highest_precedence + 10)
@autoconfigureafter({ dispatcherservletautoconfiguration.class,
  validationautoconfiguration.class })
public class webmvcautoconfiguration {
 
 ......
 
 @bean
 @conditionalonmissingbean(hiddenhttpmethodfilter.class)
 public orderedhiddenhttpmethodfilter hiddenhttpmethodfilter() {
  return new orderedhiddenhttpmethodfilter();
 }
 
 ......
}

综上所述,org.springframework.web.filter.hiddenhttpmethodfilter 也是特殊场景下所需,故可以关闭之。

配置项

按目前最新的 spring boot 1.5.2.release 版本中实现,也没有提供类似spring.mvc.formcontent.putfilter.enabled 这样的配置项关闭,无法关闭。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000015742857

延伸 · 阅读

精彩推荐