Spring Boot 默认使用的是 Tomcat 作为内嵌的服务器。所以,我们搭建一个 Web 工程将会变得非常的简单。

内嵌的 Tomcat,一个Jar包运行

还记得,《Spring Boot 揭秘与实战(一) 快速上手》讲到的例子么?我们来回顾下。
首先,修改 POM 文件,添加依赖。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

创建Java代码

  1. @RestController
  2. @EnableAutoConfiguration
  3. public class RestfulApiWebDemo {
  4. @RequestMapping("/")
  5. String home() {
  6. return "Hello World!";
  7. }
  8. public static void main(String[] args) throws Exception {
  9. SpringApplication.run(RestfulApiWebDemo.class, args);
  10. }
  11. }

直接运行 Java 类,或者可以通过 “mvn spring-boot:run” 在命令行启动该应用。会启动一个内嵌的 Tomcat 服务器运行在 8080 端口。访问 “http://localhost:8080” 可以看到页面上显示“Hello World!”。

此外,在 POM 文件添加插件。

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. </plugin>

在添加了插件后,当运行 “mvn package” 进行打包时,会打包成一个可以直接运行的 JAR 文件,使用 “java -jar” 命令就可以直接运行。

通过 Spring Boot 内嵌的 Tomcat 服务器,我们通过一个 jar 包就可以运行一个 web 工程,这个也很符合微服务的场景,我们在正式环境只要部署一个 jar 即可,当然,我们还可以把这个 jar 部署在 docker 容器中运行,是不是非常方便呢?

如何定制内嵌 Tomcat

设置内嵌Tomcat的端口

Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口。如果,我们需要修改Tomcat的端口,我们可以在 src/main/resources/application.properties 中配置Tomcat信息。

  1. server.port=8089

现在,你可以重新运行上面的例子,看下是不是 Tomcat 的端口变成 8089 了。

设置内嵌Tomcat的最大线程数

我们还可以修改内嵌的 Tomcat 服务器的最大线程数。

  1. server.tomcat.max-threads=1000

设置内嵌Tomcat的编码

在一些场景下,我们可能需要改动 Tomcat 的编码,例如是 GBK 还是 UTF-8。

  1. server.tomcat.uri-encoding = UTF-8

官方提供的常用配置参数

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

这里列举一些官方提供的常用配置参数,如果有特定需求,可以进行内嵌 Tomcat 的定制。

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

War 包部署的使用细节

如果,我们还是希望通过 war 包的方式,部署到外部的 Tomcat 服务器上, Spring Boot 也是支持的,不过需要一些额外的配置。

首先,要将最终的打包形式改为 war 包,所以需要将 packaging 的值修改为 war。

  1. <packaging>war</packaging>

接着,对依赖进行适当的配置,值得注意的是,在这里需要移除对嵌入的 Tomcat 的依赖,这样打出的 WAR 包中,在 lib 目录下才不会包含 Tomcat 相关的JAR包。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-tomcat</artifactId>
  14. <scope>provided</scope>
  15. </dependency>

另外,为了保证编译正确,还需要添加对 servlet-api 的依赖,因此添加如下的配置。

  1. <dependency>
  2. <groupId>org.apache.tomcat</groupId>
  3. <artifactId>tomcat-servlet-api</artifactId>
  4. <version>7.0.42</version>
  5. <scope>provided</scope>
  6. </dependency>

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

  1. <build>
  2. <finalName>springboot-web-demo</finalName>
  3. </bulid>

如果我们想要将在外部的 Tomcat 服务器部署的 WAR 包,就不能依赖于 RestfulApiWebDemo 的 main 函数,要以类似于 web.xml 文件配置的方式来启动 Spring 应用上下文,此时我们需要声明一个类,这个类的作用与在 web.xml 中配置负责初始化 Spring 应用上下文的监听器作用类似,只不过在这里不需要编写额外的 XML 文件了。

  1. public class ServletInitializer extends SpringBootServletInitializer {
  2. @Override
  3. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  4. return application.sources(RestfulApiWebDemo.class);
  5. }
  6. }

大功告成,此时,我们可以通过 Maven 的 “mvn clean package” 打包出一个 war 包,并部署到外部的 Tomcat 服务器运行。访问 “http://localhost:8080/springboot-web-demo/” 可以看到页面上显示“Hello World!”。

总结

Spring Boot 默认使用的是 Tomcat 作为内嵌的服务器。所以,我们搭建一个 Web 工程将会变得非常的简单,只需要一个 jar 包即可运行。此外,我们还可以对内嵌的 Tomcat 进行一些定制,例如端口、最大线程数、编码、 SSL 等。如果,我们还是希望通过 war 包的方式,部署到外部的 Tomcat 服务器上, Spring Boot 也是支持的,不过需要一些额外的配置,这个配置过程也只需要几个简单的步骤即可实现。

源代码

相关示例完整代码: springboot-action

(完)

如果觉得我的文章对你有帮助,请随意打赏。

Spring Boot 揭秘与实战(五) 服务器篇 - 内嵌的服务器 Tomcat剖析的更多相关文章

  1. Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri

    文章目录 1. Jetty 的切换 2. Undertow的使用 Spring Boot 可选择内嵌 Tomcat.Jetty 和 Undertow,因此我们不需要以 war 包形式部署项目.< ...

  2. Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 启用 HTTPS

    文章目录 1. 生成证书 2. 配置 HTTPS 支持 3. 启动与测试 4. 源代码 Spring Boot 内嵌的 Tomcat 服务器可以启用 HTTPS 支持. 生成证书 使用第三方 CA 证 ...

  3. Spring Boot 揭秘与实战(五) 服务器篇 - Tomcat 代码配置

    Spring Boot 内嵌的 Tomcat 服务器默认运行在 8080 端口.如果,我们需要修改Tomcat的端口,我们可以在 src/main/resources/application.prop ...

  4. Spring Boot 揭秘与实战(九) 应用监控篇 - 自定义监控端点

    文章目录 1. 继承 AbstractEndpoint 抽象类 2. 创建端点配置类 3. 运行 4. 源代码 Spring Boot 提供的端点不能满足我们的业务需求时,我们可以自定义一个端点. 本 ...

  5. Spring Boot 揭秘与实战(六) 消息队列篇 - RabbitMQ

    文章目录 1. 什么是 RabitMQ 2. Spring Boot 整合 RabbitMQ 3. 实战演练4. 源代码 3.1. 一个简单的实战开始 3.1.1. Configuration 3.1 ...

  6. Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache

    文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ...

  7. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控

    文章目录 1. 内置 HealthIndicator 监控检测 2. 自定义 HealthIndicator 监控检测 3. 源代码 Health 信息是从 ApplicationContext 中所 ...

  8. Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 应用监控

    文章目录 1. 快速开始 2. 监控和管理端点3. 定制端点 2.1. health 应用健康指标 2.2. info 查看应用信息 2.3. metrics 应用基本指标 2.4. trace 基本 ...

  9. Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性

    文章目录 1. 使用属性文件2. YAML文件 1.1. 自定义属性 1.2. 参数引用 1.3. 随机数属性 1.4. application-{profile}.properties参数加载 3. ...

随机推荐

  1. nyoj-1250-exgcd

    机器人 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远 ...

  2. 51Nod 1001 数组中和等于K的数对

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1001一开始的想法是排序后二分搜索,发现会进行非常多不必要的遍历,十分耗时 ...

  3. git status 查看当前修改文件

    可以查看当前已经修改的文件.

  4. tls 流量画像——直接使用图像处理的思路探索,待进一步观察

    代码,示意了一个tls的数据内容: import numpy as np import matplotlib.pyplot as pyplot # !!! If on the server, use ...

  5. 这些你都了解么------程序员"跳槽"法则

    篇头语: “跳槽”这个词是从我报了"软件工程"这个专业后就已经开始听说的词了, 在大学中老师上课也会常说:“等你们参加工作以后,工资低不怕,没事就跳槽,之后工资就高了”: 我相信听 ...

  6. PowerShell使用教程

    一.说明 1.1 背景说明 个人对PowerShell也不是很熟悉,开始的时候就突然看到开始菜单中多了个叫PowerShell的文件夹,后来一点就看到某个教程视频说PowerShell很厉害但也没怎么 ...

  7. Elasticsearch安装部署(CentOS)

    1.安装JDK,http://www.cnblogs.com/zhi-leaf/p/5996287.html. 2.下载ES:https://www.elastic.co/downloads/elas ...

  8. DMA-总结

    概念DMA “Direct Memory Access(存储器直接访问).这是指一种高速的数据传输操作,允许在外部设备和存储器之间直接读写数据.整个数据传输操作在一个称为"DMA控制器&qu ...

  9. 4.3 if-else语句使用

    Q:对输入的成绩进行登记划分. #include<iostream> #include<cstdio> using namespace std; int main() { in ...

  10. shutil 模块

    import shutil #用于简化文件操作的模块 # f1 = open(r"D:\上海python全栈4期\day20\7.shutil模块.py","rb&quo ...