Spring Boot tomcat

定制内嵌 Tomcat

设置内嵌Tomcat的端口

Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口。若是,咱们须要修改Tomcat的端口,咱们能够在 src/main/resources/application.properties 中配置Tomcat信息。web

server.port=8089

设置内嵌Tomcat的最大线程数

咱们还能够修改内嵌的 Tomcat 服务器的最大线程数。spring

server.tomcat.max-threads=1000

设置内嵌Tomcat的编码

在一些场景下,咱们可能须要改动 Tomcat 的编码,例如是 GBK 仍是 UTF-8。express

server.tomcat.uri-encoding = UTF-8

官方提供的经常使用配置参数

除了上面讲到的3个场景外,咱们还能够自定义设置路径地址、 SSL等参数。apache

这里列举一些官方提供的经常使用配置参数,若是有特定需求,能够进行内嵌 Tomcat 的定制。api

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 也是支持的,不过须要一些额外的配置。tomcat

首先,要将最终的打包形式改成 war 包,因此须要将 packaging 的值修改成 war。springboot

<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 的依赖,所以添加以下的配置。app

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

设置,打包后的项目访问名称,在<build>节点里添加<finalName>内容。ide

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

相关文章
相关标签/搜索