作者:劉軍
Spring Cloud 是在 Spring Boot 之上構(gòu)建的一套微服務(wù)生態(tài)體系,包括服務(wù)發(fā)現(xiàn)、配置中心、限流降級、分布式事務(wù)、異步消息等,因此通過增加依賴、注解等簡單的四步即可完成 Spring Boot 應(yīng)用到 Spring Cloud 升級。
*Spring Cloud Alibaba (SCA) 官網(wǎng)正式上線:sca.aliyun.com
Spring Boot 應(yīng)用升級為 Spring Cloud
以下是應(yīng)用升級 Spring Cloud 的完整步驟。
第一步:添加 Spring Cloud 依賴
首先,為應(yīng)用添加 Spring Cloud 與 Spring Cloud Alibaba 依賴。注意根據(jù)當(dāng)前應(yīng)用 Spring Boot 版本選擇合適的 Spring Cloud 版本,具體參見版本映射表 [ 1] 。
<properties>
<spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
<spring-cloud.version>2022.0.0</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Nacos 服務(wù)發(fā)現(xiàn) -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 服務(wù)發(fā)現(xiàn):OpenFeign服務(wù)調(diào)用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 服務(wù)發(fā)現(xiàn):OpenFeign服務(wù)調(diào)用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
以上我們添加了服務(wù)注冊發(fā)現(xiàn)、OpenFeign 等依賴。
第二步:添加配置
在應(yīng)用 application.yml 或者 application.properties 文件中增加以下配置項,設(shè)置應(yīng)用名、注冊中心地址。
application.yml:
spring:
application:
#項目名稱必填,在注冊中心唯一
#最好和之前域名規(guī)范、kubernetes service名等保持一致(會作為調(diào)用與負(fù)載均衡依據(jù))
name: service-provider
cloud:
nacos:
discovery: #啟用 spring cloud nacos discovery
server-addr: 127.0.0.1:8848
application.properties:
#項目名稱必填,在注冊中心唯一
#最好和之前域名規(guī)范、kubernetes service名等保持一致(會作為調(diào)用與負(fù)載均衡依據(jù))
spring.application.name=service-provider
#啟用 spring cloud nacos discovery
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
第三步:啟動類增加注解
啟動類增加 EnableDiscoveryClient EnableFeignClients 注解,啟動服務(wù)地址自動注冊與發(fā)現(xiàn)。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
第四步:調(diào)整服務(wù)調(diào)用方式
?? 注意!
-
為了保證平滑升級,請確保下游應(yīng)用完成 Spring Cloud 改造并在注冊中心注冊服務(wù)后再進行調(diào)用方式改造。
-
RestTemplate/FeignClient 默認(rèn)發(fā)起調(diào)用的 hostname (示例中的 service-provider)是對端 Spring Cloud 應(yīng)用名。因此,為了保證盡可能少的改造量,改造過程中設(shè)置的應(yīng)用名 spring.name=service-provider 最好和之前的命名規(guī)范保持一致。比如:
-
- 如果之前有自定義域名,則和域名定義保持一致
- 如果之前用的 Kubernetes Service,則和 Service Name 保持一致
1. RestTemplate 模式
為之前的 RestTemplate Bean 添加 @LoadBlanced 注解,使得 RestTemplate 接入服務(wù)發(fā)現(xiàn)與負(fù)載均衡:
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
其它原有 RestTemplate 發(fā)起調(diào)用的代碼保持不變,只需調(diào)整 hostname 即可,如下所示。
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
}
2. FeignClient 模式
使用 @FeignClient 注解將 EchoService 這個接口包裝成一個 FeignClient,屬性 name 對應(yīng)對端應(yīng)用名 spring.name=service-provider。
//@FeignClient(name = "service-provider", url="http://service.example.com/")
@FeignClient(name = "service-provider")
public interface EchoService {
@GetMapping(value = "/echo/{str}")
String echo(@PathVariable("str") String str);
}
將 EchoService 作為標(biāo)準(zhǔn) bean 注入,即可對遠(yuǎn)端服務(wù)發(fā)起請求了。
@RestController
public class TestController {
@Autowired
private EchoService echoService;
@GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
}
3. HtClient、自定義 HTTP 訪問工具等
對于使用 HttpClient 或者自行封裝 http 調(diào)用工具的用戶,建議統(tǒng)一改造為以上 1、2 兩種調(diào)用模式之一。
參考資料
完整示例源碼
基于 Spring Boot 構(gòu)建的應(yīng)用架構(gòu)變化多樣,比如可能是以下一些常用架構(gòu)的任意一種,但不論哪種架構(gòu),升級 Spring Cloud 的大致改造方式都是類似的(都可以轉(zhuǎn)為基于 Nacos 注冊中心的地址發(fā)現(xiàn)與負(fù)載均衡)。
- 基于 DNS 自定義域名,服務(wù)間的通過域名調(diào)用實現(xiàn)負(fù)載均衡
- 基于 SLB 的中心化流量轉(zhuǎn)發(fā),調(diào)用直接轉(zhuǎn)發(fā)到 SLB,由 SLB 實現(xiàn)在服務(wù)間實現(xiàn)流量轉(zhuǎn)發(fā)
- 基于 Kubernetes Service 微服務(wù)體系,依賴 Kubernetes ClusterIP 等實現(xiàn)負(fù)載均衡與調(diào)用轉(zhuǎn)發(fā)
在此,我們以 DNS 自定義域名架構(gòu)為例,提供了一個 Spring Boot 到 Spring Cloud 升級改造的完整示例,升級前后的應(yīng)用架構(gòu)圖如下。具體可參見 Github 源碼鏈接 [ 2] 。
升級前 SpringBoot 架構(gòu) ??
升級后 SpringCloud 架構(gòu) ??
Spring Boot 與 Spring Cloud Alibaba 版本對應(yīng)關(guān)系
請根據(jù)您使用的 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-starter-alibaba-nacos-discovery [ 3]
- spring-cloud-starter-alibaba-nacos-config [ 4]
- spring-cloud-starter-alibaba-nacos-sentinel [ 5]
- spring-cloud-starter-alibaba-nacos-rocketmq [ 6]
- spring-cloud-starter-alibaba-nacos-seata [ 7]
Spring Cloud Alibaba 集成的組件版本
每個 Spring Cloud Alibaba 版本及其自身所適配的各組件對應(yīng)版本如下表所示:
Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
---|---|---|---|---|---|
2022.0.0.0 | 1.8.6 | 2.2.1 | 4.9.4 | ~ | 1.7.0 |
2022.0.0.0-RC2 | 1.8.6 | 2.2.1 | 4.9.4 | ~ | 1.7.0-native-rc2 |
2021.0.5.0 | 1.8.6 | 2.2.0 | 4.9.4 | ~ | 1.6.1 |
2.2.10-RC1 | 1.8.6 | 2.2.0 | 4.9.4 | ~ | 1.6.1 |
2022.0.0.0-RC1 | 1.8.6 | 2.2.1-RC | 4.9.4 | ~ | 1.6.1 |
2.2.9.RELEASE | 1.8.5 | 2.1.0 | 4.9.4 | ~ | 1.5.2 |
2021.0.4.0 | 1.8.5 | 2.0.4 | 4.9.4 | ~ | 1.5.2 |
2.2.8.RELEASE | 1.8.4 | 2.1.0 | 4.9.3 | ~ | 1.5.1 |
2021.0.1.0 | 1.8.3 | 1.4.2 | 4.9.2 | ~ | 1.4.2 |
2.2.7.RELEASE | 1.8.1 | 2.0.3 | 4.6.1 | 2.7.13 | 1.3.0 |
2.2.6.RELEASE | 1.8.1 | 1.4.2 | 4.4.0 | 2.7.8 | 1.3.0 |
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE | 1.8.0 | 1.4.1 | 4.4.0 | 2.7.8 | 1.3.0 |
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE | 1.8.0 | 1.3.3 | 4.4.0 | 2.7.8 | 1.3.0 |
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE | 1.7.1 | 1.2.1 | 4.4.0 | 2.7.6 | 1.2.0 |
2.2.0.RELEASE | 1.7.1 | 1.1.4 | 4.4.0 | 2.7.4.1 | 1.0.0 |
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE | 1.7.0 | 1.1.4 | 4.4.0 | 2.7.3 | 0.9.0 |
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE | 1.6.3 | 1.1.1 | 4.4.0 | 2.7.3 | 0.7.1 |
相關(guān)鏈接:
[1]?版本映射表
https://sca.aliyun.com/zh-cn/docs/next/best-practice/spring-boot-to-spring-cloud/
[2]?Github 源碼鏈接
https://github.com/spring-cloud-alibaba-group/springboot-transfer-to-springcloud
[3]?spring-cloud-starter-alibaba-nacos-discovery
https://sca.aliyun.com/zh-cn/docs/next/user-guide/nacos/quick-start/#%E6%8E%A5%E5%85%A5-nacos-%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0
[4]?spring-cloud-starter-alibaba-nacos-config
https://sca.aliyun.com/zh-cn/docs/next/user-guide/nacos/quick-start/#%E6%8E%A5%E5%85%A5-nacos-%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83
[5]?spring-cloud-starter-alibaba-nacos-sentinel
https://sca.aliyun.com/zh-cn/docs/next/user-guide/sentinel/quick-start/
[6]?spring-cloud-starter-alibaba-nacos-rocketmq
https://sca.aliyun.com/zh-cn/docs/next/user-guide/rocketmq/quick-start/
[7]?spring-cloud-starter-alibaba-nacos-seata文章來源:http://www.zghlxwxcb.cn/news/detail-803882.html
https://sca.aliyun.com/zh-cn/docs/next/user-guide/seata/quick-start/文章來源地址http://www.zghlxwxcb.cn/news/detail-803882.html
到了這里,關(guān)于Spring Boot 單體應(yīng)用升級 Spring Cloud 微服務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!