一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人免费在线视频 | a级特黄毛片免费观看 | 久久免费资源福利资源站 | 91果冻制片厂天美传媒 | 男人机机桶女人机机 | 美女漫画网 | aⅴ导航站 | 国产亚洲精aa在线观看香蕉 | sese在线| 99久久免费看精品国产一区 | 秘书喂奶好爽一边 | 日本国产成人精品视频 | 国产精品酒店视频免费看 | 国产性色视频 | 欧美va免费大片 | 人人干97 | 91高跟丝袜 | 91动漫在线观看 | 国内精品视频九九九九 | 女海盗斯蒂内塔的复仇2免费观看 | 狠狠的撞进去嗯啊h女强男视频 | 麻生希在线 | 日本中文字幕一区二区三区不卡 | 93版高校教师 | 俄罗斯大白屁股 | 添逼逼视频 | 久久精品视频免费 | 精品一区二区三区在线播放 | 精品国产免费观看一区高清 | 91亚洲专区 | 日本68xxxxxxxxx59 日本 视频 在线 | 逼123| 欧美男同videos | 成人在线免费观看 | 青草香蕉精品视频在线观看 | 日韩在线观看网址 | 成人夜视频寂寞在线观看 | 四虎最新免费网址 | 男人天堂网页 | 四虎影库紧急大通知 | 扒开腚眼子视频大全 |