1. Overview
1.概述
Spring Boot web applications include a pre-configured, embedded web server by default. In some situations, though, we’d like to modify the default configuration to meet custom requirements.
Spring Boot Web应用程序默认包含一个预配置的嵌入式Web服务器。但在某些情况下,我们希望修改默认配置,以满足自定义需求。
In this tutorial, we’ll look at a few common use cases for configuring the Tomcat embedded server through the application.properties file.
在本教程中,我们将看看通过application.properties文件配置Tomcat嵌入式服务器的几个常见用例。
2. Common Embedded Tomcat Configurations
2.常见的嵌入式Tomcat配置
2.1. Server Address and Port
2.1.服务器地址和端口
The most common configuration we may wish to change is the port number:
我们可能希望改变的最常见配置是端口号。
server.port=80
If we don’t provide the server.port parameter it’s set to 8080 by default.
如果我们不提供server.port参数,它就会被默认设置为8080。。
In some cases, we may wish to set a network address to which the server should bind. In other words, we define an IP address where our server will listen:
在某些情况下,我们可能希望设置一个n网络地址,服务器应该与之绑定。换句话说,我们定义一个IP地址,我们的服务器将在那里收听:
server.address=my_custom_ip
By default, the value is set to 0.0.0.0, which allows connection via all IPv4 addresses. Setting another value, for example, localhost – 127.0.0.1 – will make the server more selective.
默认情况下,该值被设置为0.0.0.0,这允许通过所有IPv4地址进行连接。设置另一个值,例如,localhost – 127.0.0.1 – 将使服务器更具选择性。
2.2. Error Handling
2.2 错误处理
By default, Spring Boot provides a standard error web page. This page is called the Whitelabel. It’s enabled by default, but if we don’t want to display any error information, we can disable it:
默认情况下,Spring Boot提供一个标准错误网页。这个页面被称为Whitelabel。它默认是启用的,但如果我们不想显示任何错误信息,我们可以禁用它。
server.error.whitelabel.enabled=false
The default path to a Whitelabel is /error. We can customize it by setting the server.error.path parameter:
Whitelabel的默认路径是/error。我们可以通过设置server.error.path参数来定制它。
server.error.path=/user-error
We can also set properties that will determine which information about the error is presented. For example, we can include the error message and the stack trace:
我们还可以设置一些属性,以决定呈现哪些关于错误的信息。例如,我们可以包括错误信息和堆栈跟踪。
server.error.include-exception=true
server.error.include-stacktrace=always
Our tutorials Exception Message Handling for REST and Customize Whitelabel Error Page explain more about handling errors in Spring Boot.
我们的教程REST的异常消息处理和自定义白标签错误页面解释了有关Spring Boot中错误处理的更多内容。
2.3. Server Connections
2.3.服务器连接
When running on a low resource container, we might like to decrease the CPU and memory load. One way of doing that is to limit the number of simultaneous requests that can be handled by our application. Conversely, we can increase this value to use more available resources to get better performance.
当在低资源容器上运行时,我们可能希望减少CPU和内存负载。一种方法是限制我们的应用程序可以处理的同时请求的数量。反之,我们可以增加这个值,以使用更多的可用资源来获得更好的性能。
In Spring Boot, we can define the maximum amount of Tomcat worker threads:
在Spring Boot中,我们可以定义Tomcat工作线程的最大数量。
server.tomcat.threads.max=200
When configuring a web server, it also might be useful to set the server connection timeout. This represents the maximum amount of time the server will wait for the client to make their request after connecting before the connection is closed:
在配置Web服务器时,设置服务器连接超时也可能是有用的。这代表了服务器在连接后等待客户端发出请求后关闭连接的最长时间。
server.connection-timeout=5s
We can also define the maximum size of a request header:
我们还可以定义一个请求头的最大尺寸。
server.max-http-header-size=8KB
The maximum size of a request body:
一个请求主体的最大尺寸。
server.tomcat.max-swallow-size=2MB
Or a maximum size of the whole post request:
或整个帖子请求的最大尺寸。
server.tomcat.max-http-post-size=2MB
2.4. SSL
2.4. SSL
To enable SSL support in our Spring Boot application, we need to set the server.ssl.enabled property to true and define an SSL protocol:
为了在Spring Boot应用程序中启用SSL支持,我们需要将server.ssl.enabled属性设置为true并定义一个SSL协议。
server.ssl.enabled=true
server.ssl.protocol=TLS
We should also configure the password, type, and path to the key store that holds the certificate:
我们还应该配置持有证书的密码、类型和钥匙库的路径。
server.ssl.key-store-password=my_password
server.ssl.key-store-type=keystore_type
server.ssl.key-store=keystore-path
And we must also define the alias that identifies our key in the key store:
而且,我们还必须定义别名,以识别我们在钥匙库中的钥匙。
server.ssl.key-alias=tomcat
For more information about SSL configuration, visit our HTTPS using a self-signed certificate in the Spring Boot article.
有关SSL配置的更多信息,请访问我们的Spring Boot中使用自签名证书的HTTPS文章。
2.5. Tomcat Server Access Logs
2.5.Tomcat服务器访问日志
Tomcat access logs are beneficial when measuring page hit counts, user session activity, and so on.
Tomcat访问日志在测量页面点击率、用户会话活动等方面很有益处。
To enable access logs, simply set:
要启用访问日志,只需设置。
server.tomcat.accesslog.enabled=true
We should also configure other parameters such as directory name, prefix, suffix, and date format appended to log files:
我们还应该配置其他参数,如目录名称、前缀、后缀和附加到日志文件的日期格式。
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
3. Embedded Tomcat’s Version
3.嵌入式Tomcat的版本
We can’t change Tomcat’s version being used by configuring our application.properties file. It’s a bit more complicated and mainly depends on whether we use spring-boot-starter-parent or not.
我们不能通过配置application.properties文件来改变正在使用的Tomcat的版本。这有点复杂,主要取决于我们是否使用spring-boot-starter-parent。
Before we proceed, however, we must be aware that each Spring Boot release is designed and tested against the specific Tomcat version. If we change it, we may face some unexpected compatibility issues.
然而,在我们继续之前,我们必须意识到,每个Spring Boot版本都是针对特定的Tomcat版本设计和测试的。如果我们改变它,我们可能会面临一些意想不到的兼容性问题。
3.1. Using spring-boot-starter-parent
3.1.使用spring-boot-starter-parent
If we use Maven and configure our project to inherit from the spring-boot-starter-parent, we can override individual dependencies by overwriting a specific property in our pom.xml.
如果我们使用Maven,并将项目配置为继承自spring-boot-starter-parent,我们可以通过覆盖pom.xml中的特定属性来覆盖各个依赖项。
With that in mind, to update the Tomcat version, we must use the tomcat.version property:
考虑到这一点,要更新Tomcat版本,我们必须使用tomcat.version属性。
<properties>
<tomcat.version>9.0.44</tomcat.version>
</properties>
3.2. Using spring-boot-dependencies
3.2.使用spring-boot-dependencies
There are situations when we don’t want to or can’t use the spring-boot-starter-parent. For example, if we use a custom parent in our Spring Boot project. In such cases, there’s a great chance we use spring-boot-dependency to still benefit from the dependency management.
有些情况下,我们不想或不能使用spring-boot-starter-parent。例如,如果我们在Spring Boot项目中使用custom parent。在这种情况下,我们很有可能使用spring-boot-dependency,以便仍然从依赖性管理中获益。
This setup, however, doesn’t let us override individual dependencies by using Maven properties, as shown in the preceding section.
然而,这种设置并不能让我们通过使用Maven属性来覆盖单个依赖项,如上节所示。
To achieve the same goal and still use a different Tomcat version, we need to add an entry in the dependencyManagement section of our pom file. The crucial thing to remember is that we must place it before the spring-boot-dependencies:
为了实现同样的目标,并且仍然使用不同的Tomcat版本,我们需要在pom文件的dependencyManagement部分添加一个条目。需要记住的是,我们必须把它放在spring-boot-dependencies之前。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.44</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
4. Conclusion
4.总结
In this tutorial, we’ve learned a few common Tomcat embedded server configurations. To view more possible configurations, please visit the official Spring Boot application properties docs page.
在本教程中,我们已经了解了一些常见的Tomcat嵌入式服务器配置。要查看更多可能的配置,请访问官方的Spring Boot应用程序属性文档页面。
As always, the source code for these examples is available over on GitHub.
一如既往,这些例子的源代码可以在GitHub上获得。