国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot 單體應(yīng)用升級 Spring Cloud 微服務(wù)

這篇具有很好參考價值的文章主要介紹了Spring Boot 單體應(yīng)用升級 Spring Cloud 微服務(wù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

作者:劉軍

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)用方式

?? 注意!

  1. 為了保證平滑升級,請確保下游應(yīng)用完成 Spring Cloud 改造并在注冊中心注冊服務(wù)后再進行調(diào)用方式改造。

  2. 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] 。

Spring Boot 單體應(yīng)用升級 Spring Cloud 微服務(wù),spring boot,spring cloud,微服務(wù)

升級前 SpringBoot 架構(gòu) ??

Spring Boot 單體應(yīng)用升級 Spring Cloud 微服務(wù),spring boot,spring cloud,微服務(wù)

升級后 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

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Spring Boot 3.x微服務(wù)升級經(jīng)歷

    Spring Boot 3.x微服務(wù)升級經(jīng)歷

    Spring Boot 3.0.0 GA版已經(jīng)發(fā)布,好多人也開始嘗試升級,有人測試升級后,啟動速度確實快了不少,如下為網(wǎng)絡(luò)截圖,于是我也按捺不住的想嘗試下。 首先就是要把Spring Boot、Spring Cloud 相關(guān)的依賴升一下 Spring Boot:3.0.0 Spring Cloud:2022.0.0-RC2 統(tǒng)一依賴版本管理: 現(xiàn)在還不能下載

    2024年02月02日
    瀏覽(19)
  • Spring boot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證

    Spring boot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證

    ??作者主頁:青花鎖 ??簡介:Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者??、Java微服務(wù)架構(gòu)公號作者?? ??簡歷模板、學(xué)習(xí)資料、面試題庫、技術(shù)互助 ??文末獲取聯(lián)系方式 ?? 專欄 描述 Java項目實戰(zhàn) 介紹Java組件安裝、使用;手寫框架等 Aws服務(wù)器實戰(zhàn) Aws Linux服務(wù)器上操作nginx、git、JDK、Vue Jav

    2024年04月14日
    瀏覽(17)
  • Java版分布式微服務(wù)云開發(fā)架構(gòu) Spring Cloud+Spring Boot+Mybatis 電子招標(biāo)采購系統(tǒng)功能清單

    Java版分布式微服務(wù)云開發(fā)架構(gòu) Spring Cloud+Spring Boot+Mybatis 電子招標(biāo)采購系統(tǒng)功能清單

    ???一、立項管理 1、招標(biāo)立項申請 功能點:招標(biāo)類項目立項申請入口,用戶可以保存為草稿,提交。 2、非招標(biāo)立項申請 功能點:非招標(biāo)立項申請入口、用戶可以保存為草稿、提交。 3、采購立項列表 功能點:對草稿進行編輯,駁回的立項編輯,在途流程查看。 二、項目管

    2024年02月17日
    瀏覽(22)
  • SkyWalking鏈路追蹤-搭建-spring-boot-cloud-單機環(huán)境 之《10 分鐘快速搭建 SkyWalking 服務(wù)》

    SkyWalking鏈路追蹤-搭建-spring-boot-cloud-單機環(huán)境 之《10 分鐘快速搭建 SkyWalking 服務(wù)》

    首先了解一下單機環(huán)境 第一步,搭建一個 Elasticsearch 服務(wù)。 第二步,下載 SkyWalking 軟件包。 第三步,搭建一個 SkyWalking OAP 服務(wù)。 第四步,啟動一個 Spring Boot 應(yīng)用,并配置 SkyWalking Agent。 第五步,搭建一個 SkyWalking UI 服務(wù)。 準(zhǔn)備工作,準(zhǔn)備一個docker網(wǎng)絡(luò)組,網(wǎng)絡(luò)組的名字為

    2024年02月15日
    瀏覽(24)
  • Dubbo Spring Boot Starter 開發(fā)微服務(wù)應(yīng)用

    Dubbo Spring Boot Starter 開發(fā)微服務(wù)應(yīng)用

    系統(tǒng):Windows、Linux、MacOS JDK 8 及以上(推薦使用 JDK17) Git IntelliJ IDEA(可選) Docker (可選) 在本任務(wù)中,將分為 3 個子模塊進行獨立開發(fā),模擬生產(chǎn)環(huán)境下的部署架構(gòu)。 如上所示,共有 3 個模塊,其中? interface ?模塊被? consumer ?和? provider ?兩個模塊共同依賴,存儲 RPC

    2024年02月12日
    瀏覽(27)
  • Spring Boot、Spring Cloud與Spring Cloud Alibaba版本對應(yīng)關(guān)系

    Spring Boot、Spring Cloud與Spring Cloud Alibaba版本對應(yīng)關(guān)系

    一、前言 在搭建SpringCloud項目環(huán)境架構(gòu)的時候,經(jīng)常需要選擇SpringBoot和SpringCloud進行兼容的版本號。因此,對于選擇SpringBoot版本與SpringCloud版本的對應(yīng)關(guān)系很重要,如果版本關(guān)系不對應(yīng),常見的會遇見項目啟動不起來,怪異的則會是你的項目出現(xiàn)一些詭異的問題,查資料也不

    2024年02月07日
    瀏覽(93)
  • Spring Boot 中的 Spring Cloud Gateway

    Spring Boot 中的 Spring Cloud Gateway

    Spring Cloud Gateway 是一個基于 Spring Boot 的網(wǎng)關(guān)框架,它提供了一種統(tǒng)一的入口,將所有的請求路由到不同的后端服務(wù)中。Spring Cloud Gateway 采用了 Reactive 編程模型,可以處理大量并發(fā)請求,同時還具備負(fù)載均衡、熔斷、限流等功能。本文將介紹 Spring Cloud Gateway 的原理和使用方法

    2024年02月12日
    瀏覽(20)
  • Spring Boot 項目應(yīng)用消息服務(wù)器RabbitMQ(簡單介紹)

    Spring Boot 項目應(yīng)用消息服務(wù)器RabbitMQ(簡單介紹)

    本章講述的是在用戶下單環(huán)節(jié),消息服務(wù)器RabbitMQ 的應(yīng)用 在寫一個電商項目的小demo,在電商項目中,消息服務(wù)器的應(yīng)用: 1、訂單狀態(tài)通知:當(dāng)用戶下單、支付成功、訂單發(fā)貨、訂單完成等關(guān)鍵節(jié)點時,可以通過消息服務(wù)器向用戶發(fā)送相應(yīng)的訂單狀態(tài)通知。 2、消息推送:通

    2024年02月13日
    瀏覽(97)
  • Spring boot與Spring cloud 之間的關(guān)系

    Spring boot與Spring cloud 之間的關(guān)系 Spring boot 是 Spring 的一套快速配置腳手架,可以基于spring boot 快速開發(fā)單個微服務(wù),Spring Boot,看名字就知道是Spring的引導(dǎo),就是用于啟動Spring的,使得Spring的學(xué)習(xí)和使用變得快速無痛。不僅適合替換原有的工程結(jié)構(gòu),更適合微服務(wù)開發(fā)。 Spr

    2024年02月09日
    瀏覽(26)
  • Spring Boot、Spring MVC 和 Spring Cloud 深度解析

    Spring Boot、Spring MVC 和 Spring Cloud是三個在Java企業(yè)級開發(fā)中非常重要的框架。他們各自具有不同的功能,但是也可以一起使用來創(chuàng)建強大且可擴展的應(yīng)用程序。 Spring Boot是為了簡化Spring應(yīng)用開發(fā)而創(chuàng)建的。它使用了一種\\\"約定優(yōu)于配置\\\"的方法,使得開發(fā)者可以更加專注于編寫業(yè)

    2024年02月07日
    瀏覽(37)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包