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

Spring Boot tomcat的更多相关文章

  1. spring boot tomcat 打本地包成war,通过Tomcat启动时出现问题: ZipException: error in opening zip file

    一个第三方公司提供spring boot 项目,直接启动是ok的, 但是打包成war,通过Tomcat启动,就出现 ZipException: error in opening zip file: 2 ...

  2. Spring Boot Tomcat配置详解

    参数配置容器 server.xx开头的是所有servlet容器通用的配置,server.tomcat.xx开头的是tomcat特有的参数,其它类似. 所有参数绑定配置类:org.springframe ...

  3. 如何配置Spring Boot Tomcat

    1.概述 Spring Boot Web应用程序默认包含预配置的嵌入式Web服务器.但在某些情况下,我们要修改默认配置以满足自定义要求. 在本教程中,我们将介绍通过application.proper ...

  4. spring boot tomcat 部署

    前几天springboot项目部署到linux中,整个过程就是个坑啊.踩坑的过程中也学到了许多.spring boot 项目部署时由于其内置了tomcat和jdk,而且还都是8. 所以部署的话就分为两 ...

  5. spring boot Tomcat文件上传找不到零时文件夹

    springboot项目上传文件是找不到零时文件夹 1.本身启动jar包时内置Tomcat没有创建零时文件夹 2.在zuul网关级别没有创建零时文件夹 处理方案: -Djava.io.tmpdir=/ ...

  6. spring boot Tomcat访问日志

    1.Tomcat设置访问日志 <Host name="localhost" appBase="webapps" unpackWARs="true ...

  7. spring boot tomcat 线程数 修改初始线程数 统计性能 每百次请求耗时

    [root@f java]# tail -30 nohup.outsearchES-TimeMillisSpent:448P->1602@fT->http-nio-8080-exec-3t ...

  8. Spring Boot tomcat参数

    主题 初学SpringBoot,想要配置一下tomcat的端口,以前tomcat直接在它的XML里配置就好了.现在SpringBoot直接继承了,不知道哪里配置.后来找到解决方法,记录一下. 具体方法 ...

  9. Spring boot Tomcat配置

    来自: https://www.cnblogs.com/a8457013/p/7687764.html

随机推荐

  1. utf-8和utf8的区别

    utf-8 和 utf8 的区别与使用: "UTF-8" 是标准写法,php 在 Windows 系统里的英文不区分大小写,所以也可以写成 "utf-8".&q ...

  2. linux(manjaro)磁盘迁移/opt /home

    目录 1. 创建临时挂载点/opt, 并将分区挂载到临时挂载点上: 2. 切换单用户,将除了root用户之外的用户踢出 3.  将/opt目录下的所有内容拷贝到临时挂载点中,等待结束 4. 进入/et ...

  3. P2661 信息传递

    P2661 信息传递dfs求最小环,要加时间戳,记录这个点是哪一次被dfs到的.] #include<iostream> #include<cstdio> #include&l ...

  4. Windows 自动启动 bat

    创建文件,然后把这个文件放到window开机自动执行的目录中,之后的每次开机都会重新启动这个脚本 cd /d %~dp0 %1 start "" mshta vbscript:cr ...

  5. Xamarin Essentials教程剪贴板Clipboard

    Xamarin Essentials教程剪贴板Clipboard   现在手机设备操作以触屏为主,不便于文本输入.虽然可以通过复制/粘贴的方式,借助系统剪贴板简化操作,但仍然不够方便.如果通过代码操作 ...

  6. NOIP2017 题解(给自己看的) --有坑要填

    目录 D1T1精妙证明: D1T3 D2T2 几道水题就不写了.... D1T1精妙证明: 把ax+by = z 的z按照模a剩余系分类 由于\((a,b)=1\)所以对于每个\(k\in[0, a) ...

  7. Apache Atlas

    atlas英 [ˈætləs] 阿特拉斯. 美 [ˈætləs] n.地图集;〈比喻〉身负重担的人 == Apache Atlas Version: 1.1.0 Last Published: 201 ...

  8. BZOJ.3218.a + b Problem(最小割ISAP 可持久化线段树优化建图)

    BZOJ UOJ 首先不考虑奇怪方格的限制,就是类似最大权闭合子图一样建图. 对于奇怪方格的影响,显然可以建一条边\((i\to x,p_i)\),然后由\(x\)向\(1\sim i-1\)中权值在 ...

  9. React Native小白入门学习路径——五

    React Native小白入门学习路径--五 序 "哦天呐!" 这句话成了我在实验室的口头禅, 老师可能觉得我们都是大神吧,都还在看着基础就给布置了那么多任务:写一个RN的TDD ...

  10. error :expected initializer before

    很可能头文件或者前面的某个定义少了个: