配置 Spring Boot 的服务器、GZip 压缩、HTTP/2、缓存等等

x33g5p2x  于2021-10-20 转载在 Spring  
字(3.9k)|赞(0)|评价(0)|浏览(618)

Spring Boot 强大而灵活。它尝试为您自动配置大部分内容,以便您可以快速启动并运行您的应用程序。

它在自动配置期间使用合理的默认值,但也让您可以灵活地通过调整一些东西来更改这些默认值。

在本文中,您将了解可能需要在您的应用程序中进行的最常见的配置调整。

在 Spring Boot 中更改嵌入式服务器

Spring Boot 使用 Tomcat 作为默认的嵌入式服务器。如果你想使用其他一些流行的服务器,比如 Jetty 或 Undertow,那么你只需要排除 tomcat 依赖项并添加其他服务器依赖项。

1. 在 Spring Boot 中使用 Jetty 作为嵌入式服务器

<!-- Exclude tomcat dependency -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!-- Include jetty dependency -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

2. 在 Spring Boot 中使用 undertow 作为嵌入式服务器

<!-- Exclude tomcat dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Include undertow dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

在 Spring Boot 中更改默认服务器端口和上下文路径

默认情况下,Spring Boot 在端口 8080 上使用上下文路径 / 运行您的应用程序。

如果你想改变默认端口和上下文路径,那么只需在 application.properties 文件中指定相应的值 -

# HTTP Server port
server.port=8080

# Make the application accessible on the given context path (http://localhost:8080/myapp)
server.servlet.context-path=/myapp

在 Spring Boot 中启用 GZip 压缩

GZip 压缩是节省带宽和提高网站速度的一种非常简单有效的方法。

它通过压缩资源然后将其发送给客户端来减少您网站的响应时间。它节省了至少 50% 的带宽。

Spring Boot 中默认禁用 GZip 压缩。要启用它,请将以下属性添加到您的 application.properties 文件中 -

# Enable response compression
server.compression.enabled=true

# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json

# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024

请注意,GZip 压缩的开销很小。因此,我添加了一个 min-response-size 属性来告诉 Spring Boot 服务器仅在大小大于给定值时才压缩响应。

在 Spring Boot 中启用 HTTP/2 支持

HTTP/2 是对 HTTP1 协议的改进。它通过采用多种机制来提高您网站的页面加载速度,例如数据压缩、服务器推送、单个 TCP 连接上的多个请求的多路复用等。

如果服务器支持,您可以使用以下属性在 Spring Boot 中启用 HTTP2 -

# Enable HTTP/2 support, if the current environment supports it
server.http2.enabled=true

在 Spring Boot 中启用浏览器缓存静态资源

浏览器缓存是另一种提高网站页面加载速度的方法。您可以设置 cache-control 标头来告诉浏览器缓存静态资源直到某个时间段。

Spring Boot 中默认禁用浏览器缓存。您可以通过在 application.properties 文件中设置以下属性来启用缓存。

# Maximum time the response should be cached (in seconds) 
spring.resources.cache.cachecontrol.max-age=120

# The cache must re-validate stale resources with the server. Any expired resources must not be used without re-validating.
spring.resources.cache.cachecontrol.must-revalidate=true

以下是您应该注意的其他一些与缓存相关的属性 -

# The resources are private and intended for a single user. They must not be stored by a shared cache (e.g CDN).
spring.resources.cache.cachecontrol.cache-private= # set a boolean value true/false

# The resources are public and any cache may store the response.
spring.resources.cache.cachecontrol.cache-public= # set a boolean value true/false

在 Spring Boot 中配置多部分文件上传

Spring Boot 中默认启用分段文件上传,具有以下属性 -

spring.servlet.multipart.enabled=true

但是您可能需要更改其他一些默认的多部分属性。

默认情况下,Spring Boot 允许您上传最大大小为 1MB 的文件。您可能需要根据您的要求将其更改为所需的值。

以下是完整的属性集 -

# Write files to disk if the file size is more than 2KB.
spring.servlet.multipart.file-size-threshold=2KB

# The intermediate disk location where the uploaded files are written
spring.servlet.multipart.location=/tmp

# Maximum file size that can be uploaded
spring.servlet.multipart.max-file-size=50MB

# Maximum allowed multipart request size
spring.servlet.multipart.max-request-size=75MB

结论

在这篇简短的文章中,您学习了如何根据需要调整 Spring Boot 的默认配置。您可以在以下官方 Spring Boot 页面上找到常见应用程序属性的完整索引 -

Spring Boot Common application properties

每当您需要在 Spring Boot 应用程序中配置任何内容时,您应该做的第一件事是 - 检查上面的通用应用程序属性页面。很可能,您会找到一个用于配置您正在寻找的东西的属性。

相关文章