Spring Boot 单体应用升级 Spring Cloud 微服务最佳实践
Spring Cloud 是在 Spring Boot 之上构建的一套微服务生态体系,包括服务发现、配置中心、限流降级、分布式事务、异步消息等,因此通过增加依赖、注解等简单的四步即可完成 Spring Boot 应用到 Spring Cloud 升级。
Spring Boot 应用升级为 Spring Cloud
以下是应用升级 Spring Cloud 的完整步骤。
第一步:添加 Spring Cloud 依赖
首先,为应用添加
Spring Cloud
与
Spring Cloud Alibaba
依赖。注意根据当前应用 Spring Boot 版本选择合适的 Spring Cloud 版本,具体参见
版本映射表
。
以上我们添加了
服务注册发现
、
OpenFeign
等依赖。
第二步:添加配置
在应用
application.yml
或者
application.properties
文件中增加以下配置项,设置应用名、注册中心地址。
application.yml
application.properties
第三步:启动类增加注解
启动类增加
EnableDiscoveryClient
EnableFeignClients
注解,启动服务地址自动注册与发现。
第四步:调整服务调用方式
hostname (示例中的service-provider)
是对端 Spring Cloud 应用名。因此,为了保证尽可能少的改造量,改造过程中设置的应用名
spring.name=service-provider
最好和之前的命名规范保持一致。比如:
为之前的 RestTemplate Bean 添加
@LoadBlanced
注解,使得 RestTemplate 接入服务发现与负载均衡:
其它原有
RestTemplate
发起调用的代码保持不变,只需调整 hostname 即可,如下所示。
使用
@FeignClient
注解将 EchoService 这个接口包装成一个 FeignClient,属性 name 对应对端应用名
spring.name=service-provider
。
将 EchoService 作为标准 bean 注入,即可对远端服务发起请求了。
对于使用 HttpClient 或者自行封装 http 调用工具的用户,建议统一改造为以上 1、2 两种调用模式之一。
完整示例源码
基于 Spring Boot 构建的应用架构变化多样,比如可能是以下一些常用架构的任意一种,但不论哪种架构,升级 Spring Cloud 的大致改造方式都是类似的(都可以转为基于 Nacos 注册中心的地址发现与负载均衡)。
在此,我们以 DNS 自定义域名架构为例,提供了一个 Spring Boot 到 Spring Cloud 升级改造的完整示例,升级前后的应用架构图如下。具体可参见 Github 源码链接 。
升级前 SpringBoot 架构 👆_升级后 SpringCloud 架构 _👆
Spring Boot 与 Spring Cloud Alibaba 版本对应关系
请根据您使用的 Spring Boot 版本,选择兼容的 Spring Cloud Alibaba 版本
Spring Boot Version | Spring Cloud Alibaba Version | Spring Cloud Version |
---|---|---|
3.0.2 | 2022.0.0.0 | Spring Cloud 2022.0.0 |
3.0.2 | 2022.0.0.0-RC2 | Spring Cloud 2022.0.0 |
3.0.0 | 2022.0.0.0-RC1 | Spring Cloud 2022.0.0 |
2.6.13 | 2021.0.5.0 | Spring Cloud 2021.0.5 |
2.6.11 | 2021.0.4.0 | Spring Cloud 2021.0.4 |
2.6.3 | 2021.0.1.0 | Spring Cloud 2021.0.1 |
2.4.2 | 2021.1 | Spring Cloud 2020.0.1 |
2.3.12.RELEASE | 2.2.10-RC1 | Spring Cloud Hoxton.SR12 |
2.3.12.RELEASE | 2.2.9.RELEASE | Spring Cloud Hoxton.SR12 |
2.3.12.RELEASE | 2.2.8.RELEASE | Spring Cloud Hoxton.SR12 |
2.3.12.RELEASE | 2.2.7.RELEASE | Spring Cloud Hoxton.SR12 |
2.3.2.RELEASE | 2.2.6.RELEASE | Spring Cloud Hoxton.SR9 |
2.2.5.RELEASE | 2.2.1.RELEASE | Spring Cloud Hoxton.SR3 |
2.2.X.RELEASE | 2.2.0.RELEASE | Spring Cloud Hoxton.RELEASE |
2.1.13.RELEASE | 2.1.4.RELEASE | Spring Cloud Greenwich.SR6 |
2.1.X.RELEASE | 2.1.2.RELEASE | Spring Cloud Greenwich |
2.0.X.RELEASE | 2.0.4.RELEASE(停止维护,建议升级) | Spring Cloud Finchley |
1.5.X.RELEASE | 1.5.1.RELEASE(停止维护,建议升级) | Spring Cloud Edgware |
Spring Cloud Alibaba Starters 列表与使用手册
Spring Cloud Alibaba 集成的组件版本
每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本如下表所示