1. 程式人生 > >Spring Boot tomcat

Spring Boot tomcat

定製內嵌 Tomcat

設定內嵌Tomcat的埠

Spring Boot 內嵌的 Tomcat 伺服器預設執行在 8080 埠。如果,我們需要修改Tomcat的埠,我們可以在 src/main/resources/application.properties 中配置Tomcat資訊。

server.port=8089

設定內嵌Tomcat的最大執行緒數

我們還可以修改內嵌的 Tomcat 伺服器的最大執行緒數。

server.tomcat.max-threads=1000

設定內嵌Tomcat的編碼

在一些場景下,我們可能需要改動 Tomcat 的編碼,例如是 GBK 還是 UTF-8。

server.tomcat.uri-encoding = UTF-8

官方提供的常用配置引數

除了上面講到的3個場景外,我們還可以自定義設定路徑地址、 SSL等引數。

這裡列舉一些官方提供的常用配置引數,如果有特定需求,可以進行內嵌 Tomcat 的定製。

server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix
=access_log # Log file name prefix. server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time. server.tomcat.accesslog.suffix=.log # Log file name suffix. server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods. server.tomcat.basedir
= # Tomcat base directory. If not specified a temporary directory will be used. server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\ 192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\ 169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\ 127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses. server.tomcat.max-threads=0 # Maximum amount of worker threads. server.tomcat.min-spare-threads=0 # Minimum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL. server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path. server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR` server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.

使用war包部署

如果我們希望通過 war 包的方式,部署到外部的 Tomcat 伺服器上, Spring Boot 也是支援的,不過需要一些額外的配置。

首先,要將最終的打包形式改為 war 包,所以需要將 packaging 的值修改為 war。

<packaging>war</packaging>

接著,對依賴進行適當的配置,值得注意的是,在這裡需要移除對嵌入的 Tomcat 的依賴,這樣打出的 WAR 包中,在 lib 目錄下才不會包含 Tomcat 相關的JAR包。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!--移除對嵌入的Tomcat的依賴-->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--額外新增tomcat的依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

另外,為了保證編譯正確,還需要新增對 servlet-api 的依賴,因此新增如下的配置。

<dependency>  
    <groupId>org.apache.tomcat</groupId>  
    <artifactId>tomcat-servlet-api</artifactId>  
    <version>7.0.42</version>  
    <scope>provided</scope>  
</dependency>

設定,打包後的專案訪問名稱,在<build>節點裡新增<finalName>內容。

<build>
  <finalName>springboot-web-demo</finalName>
</bulid>

修改專案啟動類,繼承SpringBootServletInitializer,如下:

package com.winner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * @author winner_0715
 * @date 2018/12/07
 */
@SpringBootApplication
public class SpringBootServerApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootServerApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootServerApplication.class);
    }
}

大功告成,此時,我們可以通過 Maven 的 "mvn clean package" 打包出一個 war 包,並部署到外部的 Tomcat 伺服器執行。

總結

Spring Boot 預設使用的是 Tomcat 作為內嵌的伺服器。所以,我們搭建一個 Web 工程將會變得非常的簡單,只需要一個 jar 包即可執行。此外,我們還可以對內嵌的 Tomcat 進行一些定製,例如埠、最大執行緒數、編碼、 SSL 等。如果,我們還是希望通過 war 包的方式,部署到外部的 Tomcat 伺服器上, Spring Boot 也是支援的,不過需要一些額外的配置,這個配置過程也只需要幾個簡單的步驟即可實現。