引言
Spring Boot 和 Spring Cloud 是 Java 微服务开发的基石框架。2025 年 5 月,Spring Boot 3.5 和 Spring Cloud 2025.0(代号 Northfields)发布,带来了依赖升级、配置优化、模块重命名等重要变化。本文将详细介绍这些更新的技术亮点、弃用内容、使用注意事项,并提供简单示例,帮助开发者快速上手和迁移。
Spring Boot 3.5 技术更新
Spring Boot 3.5 引入了一系列改进,优化了性能、安全性和开发体验。以下是主要更新点:
1. 依赖升级
- Apache Pulsar:从 3.3.x 升级至 4.0.x,适配最新 LTS 版本,提升消息处理能力。
- Liquibase:升级至 4.31.0,支持更复杂的数据库变更管理。
- 其他依赖(如 Jackson、Spring Security)也同步更新,确保兼容性和安全性。
2. 新配置属性
新增属性增强了配置灵活性,例如:
spring.liquibase.analytics-enabled
:控制 Liquibase 分析功能。spring.data.redis.lettuce.read-from
:指定 Redis 读取来源(如主节点或副本)。spring.kafka.listener.auth-exception-retry-interval
:设置 Kafka 监听器重试间隔。spring.test.print-condition-evaluation-report
:打印条件评估报告,便于调试。
3. 构建与容器化
- 默认构建器切换为
paketobuildpacks/builder-noble-java-tiny
,基于 Ubuntu Noble,优化容器化构建。 - Docker Compose 支持更完善的 SSL 配置,提升容器部署安全性。
4. 测试与日志
- Testcontainers:增强 SSL 配置支持,便于容器化测试。
- 结构化日志:支持自定义堆栈跟踪,优化日志分析。
更多详情请参考:Spring Boot 3.5 Release Notes。
Spring Boot 3.5 弃用内容
以下功能在 Spring Boot 3.5 中被弃用或移除,需特别注意:
spring-boot-parent
模块:停止发布,建议直接使用 starter 依赖。HttpOption.ENABLE_REDIRECTS
:已弃用,推荐使用现代 HTTP 客户端配置。- Groovy 模板属性:
spring.groovy.template.configuration.*
替换为spring.groovy.template.*
。 - 其他移除:
service.group
资源属性支持将在未来版本移除。- Spring Boot 3.3 中标记为弃用的类和方法(如
ConditionalOutcome.inverse
)已移除。 - Micrometer 观察注解属性
micrometer.observations.annotations.enabled
不再支持。
Spring Cloud 2025.0 技术更新
Spring Cloud 2025.0(Northfields)与 Spring Boot 3.5 兼容,重点改进了 Kubernetes 集成和网关模块。以下是关键更新:
1. Kubernetes 集成
- 升级至 Fabric8 7.3.1,适配 Jackson 2.19.x,提升与 Spring Boot 3.5 的兼容性。
2. Gateway 模块重命名
为提高模块清晰度,Spring Cloud Gateway 的模块和 starter 名称已更新:
旧模块/启动器 | 新模块/启动器 |
---|---|
spring-cloud-gateway-server | spring-cloud-gateway-server-webflux |
spring-cloud-gateway-server-mvc | spring-cloud-gateway-server-webmvc |
spring-cloud-starter-gateway-server | spring-cloud-starter-gateway-server-webflux |
spring-cloud-starter-gateway-server-mvc | spring-cloud-starter-gateway-server-webmvc |
spring-cloud-gateway-mvc | spring-cloud-gateway-proxyexchange-webmvc |
spring-cloud-gateway-webflux | spring-cloud-gateway-proxyexchange-webflux |
3. 信任代理配置
- 默认禁用 X-Forwarded-* 和 Forwarded 头,需显式配置信任代理:
- WebFlux:
spring.cloud.gateway.server.webflux.trusted-proxies
- WebMVC(4.1.x 起):
spring.cloud.gateway.server.webmvc.trusted-proxies
- WebFlux:
更多详情请参考:Spring Cloud 2025.0 Release Notes。
Spring Cloud 2025.0 弃用内容
- 旧模块名称:如上表所示,旧 Gateway 模块和启动器名称已弃用。
- 属性前缀变更:
spring.cloud.gateway.*
迁移至spring.cloud.gateway.server.webflux.*
(WebFlux)。spring.cloud.gateway.mvc.*
迁移至spring.cloud.gateway.server.webmvc.*
(WebMVC)。spring.cloud.gateway.proxy.*
迁移至spring.cloud.gateway.proxy-exchange.*
。
建议使用 spring-boot-properties-migrator
工具辅助迁移。
使用注意事项
Spring Boot 3.5
- 配置检查:更新
application.properties
或application.yml
,确保属性与 3.5 兼容。 - 依赖管理:升级 Pulsar、Liquibase 等依赖至最新版本,避免兼容性问题。
- 测试调整:适配 Testcontainers 的新 SSL 配置,优化日志输出。
Spring Cloud 2025.0
- 模块迁移:在
pom.xml
或build.gradle
中更新 Gateway 依赖。 - 信任代理:若需 X-Forwarded-* 功能,必须配置信任代理,否则功能不可用。
- 属性迁移:检查并更新旧属性前缀,使用新前缀。
简单示例
以下是基于 Spring Boot 3.5 和 Spring Cloud 2025.0 的简单配置和代码示例。
1. Spring Boot 3.5 配置
在 application.properties
中启用新属性:
# 启用 Liquibase 分析
spring.liquibase.analytics-enabled=true
# 配置 Redis 读取优先级
spring.data.redis.lettuce.read-from=REPLICA_PREFERRED
# Kafka 重试间隔
spring.kafka.listener.auth-exception-retry-interval=5000ms
2. Spring Boot 3.5 测试示例
使用 Testcontainers 进行 PostgreSQL 测试:
import org.testcontainers.containers.PostgreSQLContainer;
public class DatabaseTest {
@Container
private static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13")
.withDatabaseName("testdb")
.withUsername("user")
.withPassword("password")
.withExposedPorts(5432);
}
3. Spring Cloud 2025.0 Gateway 配置
启用 WebFlux 信任代理:
spring.cloud.gateway.server.webflux.trusted-proxies=10.0.0..*
WebMVC 信任代理(4.1.x 起):
spring.cloud.gateway.server.webmvc.trusted-proxies=10.0.0..*
4. Spring Cloud Gateway 示例
一个简单的 Gateway 路由配置(application.yml
):
spring:
cloud:
gateway:
server:
webflux:
trusted-proxies: 10.0.0..*
routes:
- id: example_route
uri: http://example.com
predicates:
- Path=/example/**
结论
Spring Boot 3.5 和 Spring Cloud 2025.0 通过依赖升级、配置优化和模块重构,为开发者提供了更高效、安全的开发体验。升级时需关注弃用内容和配置变更,使用 spring-boot-properties-migrator
等工具可简化迁移。上述示例展示了如何快速应用新功能,建议开发者参考官方文档,确保项目顺利过渡。
参考资料: