1 嵌入式的Servlet 容器
1.1 嵌入式Web服务器
SpringBoot 的web模块默认 使用 Tomcat 作为嵌入式的 Servlet 容器
如果是外部的Tomcat 服务器,我们想要修改Tomcat的配置,可以直接修改 /conf
目录下的server.xml
配置文件。
既然是内嵌的Tomcat 服务器,我们就无法直接修改 /conf
目录下的server.xml
配置文件了。
要想修改Servlet容器相关的配置,在SpringBoot中有两种方式可供选择:
- 一种是在配置文件中修改
- 还有一种是通过配置类的方式去修改
2 定制和修改 嵌入式Servlet容器 相关配置
2.1 配置文件中修改 Server 相关的配置
Springboot内嵌服务器的相关配置,都在ServerProperties 类中;
配置文件中以server开头的项表示服务器的配置参数,这些参数,包括端口,路径设置,SSL配置参数等等。
application.properties
只需要在application.roperties或者application.yml/yaml中像上面那样就可以轻松的修改掉相关的配置
SpringBoot官方——ServerProperties
server.port=8081
server.servlet.context-path=/crud
server.tomcat.uri-encoding=UTF-8
// 通用的Servlet容器设置
server.servlet.xxx
// Tomcat的设置
Server.tomcat.xxx
ServerProperties 源码具体修改的参数可以查看ServerProperties类,如下源码
// org.springframework.boot.autoconfigure.web
// Springboot配置文件中以server开头的项表示服务器的配置参数
@ConfigurationProperties(
prefix = "server",
ignoreUnknownFields = true
)
public class ServerProperties {
// 服务器端口号
private Integer port;
// 虚拟地址
private InetAddress address;
// 错误参数配置类
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
//
private ServerProperties.ForwardHeadersStrategy forwardHeadersStrategy;
private String serverHeader;
private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8L);
private Shutdown shutdown;
@NestedConfigurationProperty
private Ssl ssl;
@NestedConfigurationProperty
private final Compression compression;
@NestedConfigurationProperty
private final Http2 http2;
//
private final ServerProperties.Servlet servlet;
private final ServerProperties.Tomcat tomcat;
// jetty(比较适合做长链接的项目,比如聊天等这种一直要连接的)
private final ServerProperties.<