浅谈Spring Boot Web 应用性能优化
这篇文章主要介绍了浅谈Spring Boot Web 应用性能优化,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
默认情况下,Spring Boot Web 应用会装配一些功能组件 Bean。
在大多数 Web 应用场景下,可以选择性地关闭一下自动装配的Spring 组件 Bean,以达到提升性能的目的。
配置项优化
Spring Boot Web 应用加速 完整配置项
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
配置项汇总
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 记录请求的信息(如:请求方法、请求头以及请求路径等),其计算的过程存在一定的开销,使用场景罕见,故可选择关闭。
配置项
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没有存在的必要,故可选择关闭。
配置项
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)功能重叠,故可选择关闭。
配置项
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 方法支持请求参数的获取,如:
public intetfacce ServletRequest { ...... public String getParameter(String name); public Enumeration getParameterNames(); public String[] getParameterValues(String name); public Map getParameterMap(); ...... } 故 以上方法无法支持 HTTP PUT 或 HTTP PATCH 请求方法(请求头Content-Type为application/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 以及更好版本定义(可能存在一定的差异):
相关文章
Java lombok中@Accessors注解三个属性的作用
这篇文章主要介绍了Java lombok的@Accessors注解属性解析,该注解主要作用是:当属性字段在生成 getter 和 setter 方法时,做一些相关的设置,需要的朋友可以参考下
java项目实现统一打印入参出参等日志
这篇文章主要介绍了java项目实现统一打印入参出参等日志方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
基于jdk动态代理和cglib动态代理实现及区别说明
这篇文章主要介绍了基于jdk动态代理和cglib动态代理实现及区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
java如何获得redis所有的key-value
这篇文章主要介绍了java如何获得redis所有的key-value,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
mybatis中的动态sql问题
这篇文章主要介绍了mybatis中的动态sql问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教