Springboot 需要独立的容器运行吗

939 阅读3分钟

这是我参与11月更文挑战的第2天,活动详情查看:2021最后一次更文挑战

废话少说,先上答案,不需要,因为springboot已经内置三大容器,Tomcat、Jetty 和 Undertow。

当我们添加了 spring-boot-starter-web 依赖之后,默认会使用 Tomcat 作为 Web 容器。

每个Spring Boot web 应用都有一个内置服务器。这个功能导致了一系列的 ”how to“ 的问题。比如如何更改内置服务器,如何配置内置服务器等,下面对这些问题进行回答。

配置文件

配置更改

更改配置的方式有很多,如:

  • 注册个 spring bean,实现 WebServerFactoryCustomizer接口。 WebServerFactoryCustomizer 提供了对 ConfigurableServletWebServerFactory 的访问功能,其中包含了许多自定义设置方法,比如下面的例子演示了通过编程式设置端口:
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

	@Override
	public void customize(ConfigurableServletWebServerFactory server) {
		server.setPort(9000);
	}

}
  • 直接注册 TomcatServletWebServerFactory
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
	TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
	factory.setPort(9000);
	factory.setSessionTimeout(10, TimeUnit.MINUTES);
	factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
	return factory;
}
  • 在 application.yml 文件中配置。

我们常用的方式是通过 application.yml 来定义配置,下面的例子来演示如何更改服务器配置。

默认配置

有关于 Tomcat 的默认配置是在 org.springframework.boot:spring-boot-autoconfigure:2.3.0.RELEASE.jar jar包中定义的。

我们以最常见的 port 参数为例,看看 Spring Boot 中的默认配置是如何定义的:

    {
      "name": "server.port",
      "type": "java.lang.Integer",
      "description": "Server HTTP port.",
      "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties",
      "defaultValue": 8080
    },

从上面配置中,我们可以看到关于 server.port 的完整定义。如果我们不配置端口号的话,那么默认端口号就是 8080,其他参数也是如此。

其他配置也是如此

常用配置

虽然 Tomcat 的配置有很多,但是通常情况下,我们只关注一些参数信息,下面列出一些常用配置。

server:
  port: 8081 #默认8080
  servlet:
    context-path: /search # 配置项目名称(默认为 /),如果配置了项目名称,那么在访问路径中要加上配置的路径
  address: 127.0.0.1 # 服务器绑定的地址
  error:
    path: /error  # 配置当前项目出错时跳转去的页面。
  tomcat:
    connection-timeout: 5s # 服务器连接超时时间
    max-swallow-size: 2MB # 请求正文的最大大小
    uri-encoding: UTF-8 # 配置 Tomcat 请求编码
    basedir: /home/work/search 配置 Tomcat 运行日志和临时文件的目录。若不配置,则默认使用系统的临时目录。
  max-http-header-size: 8KB # 请求头的最大大小
  ssl:
    enabled: true # 启用SSL支持
    protocol: TLS # SSL使用的协议

其他配置如有需要,可自行修改。

替换内置服务器

当我们想要替换我们的内置服务器时,首先需要去掉默认依赖,然后再引入我们需要的依赖。

举例说明,如何使用 Jetty 替换 Tomcat(Maven)。

<properties>
	<servlet-api.version>3.1.0</servlet-api.version>
</properties>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<!-- Exclude the Tomcat dependency -->
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

参考文档