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 也是支持的,不过需要一些额外的配置,这个配置过程也只需要几个简单的步骤即可实现。

猜你喜欢

转载自www.cnblogs.com/winner-0715/p/10085293.html