SpringBoot內置Tomcat的配置和切換
1.基本介紹
-
SpringBoot支持的webServer:Tomcat,Jetty,Undertow
-
因為在spring-boot-starter-web中,默認匯入的是tomcat,因此啟動時使用的web容器就是tomcat,
-
同時 SpringBoot 也支持對Tomcat(或者Jetty、Undertow)的配置和切換,
2.內置Tomcat的配置
2.1通過application.yml完成配置
application.properties配置大全
內置Tomcat的配置和ServerProperties.java關聯,可以通過查看原始碼得知有哪些屬性配置,
例如:
server:
port: 9090 #埠,默認8080
tomcat:
threads:
max: 100 # web容器最大作業執行緒數,默認200
min-spare: 5 # 最小作業執行緒數,默認10
accept-count: 200 # tomcat啟動的執行緒達到最大值后,接受排隊的請求個數,默認100
max-connections: 2000 #最大連接數(并發數),默認8192
connection-timeout: 10000 #建立連接的超時時間,單位為ms
2.2通過類來配置tomcat
組態檔可以配置得更全面,通過類來配置靈活性沒有那么好
package com.li.thymeleaf.config;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
/**
* @author 李
* @version 1.0
* 通過類來配置Tomcat
*/
@Component
public class TomcatInItConfig implements
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9090);
//還可以進行其他設定...
}
}
3.切換WebServer
演示如何將默認配置的webServer切換為Undertow,
(1)修改pom.xml,因為默認引入了spring-boot-starter-tomcat,因此要排除tomcat,再加入Undertow依賴
<!--匯入SpringBoot父工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<dependencies>
<!--匯入web場景啟動器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat starter-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入undertow-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
(2)啟動專案,后臺輸出:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/547897.html
標籤:其他
