• 當前位置:首頁 > IT技術 > 編程語言 > 正文

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡
    2022-09-06 22:55:26


    文章目錄

    一、整合版本說明
    1. 畢業版本依賴關系(推薦使用)

    Spring Cloud Version

    Spring Cloud Alibaba Version

    Spring Boot Version

    Spring Cloud 2020.0.0

    2021.1

    2.4.2

    Spring Cloud Hoxton.SR9

    2.2.6.RELEASE

    2.3.2.RELEASE

    Spring Cloud Greenwich.SR6

    2.1.4.RELEASE

    2.1.13.RELEASE

    Spring Cloud Hoxton.SR3

    2.2.1.RELEASE

    2.2.5.RELEASE

    Spring Cloud Hoxton.RELEASE

    2.2.0.RELEASE

    2.2.X.RELEASE

    Spring Cloud Greenwich

    2.1.2.RELEASE

    2.1.X.RELEASE

    2. 組件版本關系

    Spring Cloud Alibaba Version

    Sentinel Version

    Nacos Version

    RocketMQ Version

    Dubbo Version

    Seata Version

    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

    3. 演示版本

    Spring Cloud Version

    Spring Cloud Alibaba Version

    Spring Boot Version

    Nacos Version

    jdk

    Spring Cloud Hoxton.SR9

    2.2.6.RELEASE

    2.3.2.RELEASE

    1.4.2

    1.8.202

    官網地址:
    ???https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E??

    二、整合實戰
    2.1. 聚合模塊設計

    模塊劃分

    微服務劃分

    端口

    訂單模塊

    order-serv

    8000

    產品模塊

    product-serv

    9000

    用戶模塊

    user-serv

    15000

    扣庫存模塊

    stock-serv

    11000

    購物車模塊

    shopcart-serv

    12000

    2.2. 創建聚合parent

    創建maven父工程名稱為EShopParent

    父工程依賴添加
    ```bash
    <!--服務注冊發現-->
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <dependencyManagement>
    <dependencies>
    <!--spring-cloud-alibaba 版本控制-->
    <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.6.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>
    2.3. 依次創建子項目

    依次創建5個子模塊

    三、子模塊配置
    3.1. 訂單模塊
    server:
    port: 8000
    spring:
    cloud:
    nacos:
    discovery:
    service: order-serv
    server-addr: localhost:8848

    啟動類

    package com.gblfy;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;

    @SpringBootApplication
    @EnableDiscoveryClient
    public class OrderApplication {

    @Bean
    @LoadBalanced//負載均衡+動態路路由
    public RestTemplate restTemplate() {
    return new RestTemplate();
    }

    public static void main(String[] args) {
    SpringApplication.run(OrderApplication.class);
    }
    }
    3.2. 產品模塊
    server:
    port: 9000
    spring:
    cloud:
    nacos:
    discovery:
    service: product-serv
    server-addr: localhost:8848
    3.3. 用戶模塊
    server:
    port: 15000
    spring:
    cloud:
    nacos:
    discovery:
    service: user-serv
    server-addr: localhost:8848
    3.4. 扣庫存模塊
    server:
    port: 11000
    spring:
    cloud:
    nacos:
    discovery:
    service: stock-serv
    server-addr: localhost:8848
    3.5. 購物車模塊
    server:
    port: 12000
    spring:
    cloud:
    nacos:
    discovery:
    service: shop-cart-serv
    server-addr: localhost:8848
    四、測試案例
    4.1. 訂單模塊
    package com.gblfy.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;

    @RestController
    public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    //http://localhost:8000/order/create?productId=11&userId=11222
    @GetMapping("/order/create")
    public String createOrder(Integer productId, Integer userId) {

    // 調用商品服務,通過商品ID獲取商品名稱
    String productNmae = restTemplate.getForObject("http://product-serv/product/" + productId, String.class);

    // 調用用戶服務,通過用戶ID獲取用戶名稱
    String userNmae = restTemplate.getForObject("http://user-serv/user/" + userId, String.class);

    // 調用扣庫存服務,通過商品ID將已購買的商品從庫存中刪除
    String result = restTemplate.getForObject("http://stock-serv/stock/reduce/" + productId, String.class);

    // 調用個購物車服務,通過商品ID和用戶ID將已購買的商品從購物車中移除
    String shopCartResult = restTemplate.getForObject("http://shop-cart-serv/shopcart/remove?productId=" + productId + "&userId=" + userId, String.class);
    return "[用戶]: " + userNmae + " 購買商品 " + productNmae + " " + result + " " + shopCartResult;
    }
    }
    4.2. 產品模塊
    package com.gblfy.controller;

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class ProductController {

    //http://localhost:9000/product/" + productId
    @GetMapping("/product/{productId}")
    public String getProductName(@PathVariable Integer productId) {
    return "IPhone 12";
    }
    }
    4.3. 用戶模塊
    package com.gblfy.controller;

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class UserController {

    @GetMapping("/user/{userId}")
    public String getUserName(@PathVariable Integer userId) {
    return "gblfy專家";
    }
    }
    4.4. 扣庫存模塊
    package com.gblfy.controller;

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class StockController {

    @GetMapping("/stock/reduce/{productId}")
    public String reduce(@PathVariable Integer productId) {
    System.out.println("減庫存一個成功");
    return "減庫存一個成功!";
    }
    }
    4.5. 購物車模塊
    package com.gblfy.controller;

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class ShopCartController {


    @GetMapping("/shopcart/remove")
    public String remove(Integer productId, Integer userId) {
    return "移除購物車成功!";
    }
    }
    五、連通性測試
    5.1. 請求地址
    http://localhost:9000/order/create?productId=11&userId=11222
    5.2. nacos服務端

    Nacos 官網:

    ??https://nacos.io/zh-cn/docs/quick-start.html??

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_負載均衡

    5.3. 效果圖

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_02


    以上5個微服務集成nacos完畢!并測試連通性測試通過!

    六、負載均衡測試
    6.1. 請求地址
    http://localhost:9000/order/create?productId=11&userId=11222
    6.2. 測試設計

    分別啟動3個訂單模塊端口為9000、9001、9002

    分別啟動3個扣庫存模塊端口為8000、8001、8002

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_微服務_03

    6.3. 登陸nacos

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_微服務_04

    6.4. 連續請求10次,觀察命中概率

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_spring_05


    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_06


    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_07

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_08

    6.5. nacos 將服務下線

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_09


    應用不停止

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_負載均衡_10


    nacos觀察服務狀態已下線

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_11

    再次測試
    請求地址:

    http://localhost:9000/order/create?productId=11&userId=11222

    SpringBoot 整合 Spring Cloud Alibaba Nacos 連通性+負載均衡_連通性_12

    6.6. 重新上線

    將下線的項目服務重新上線
    負載均衡測試,應該和正常請求一樣這里就不演示了。

    6.7. 碼云開源地址

    ??https://gitee.com/gb_90/eshop-parent??


    本文摘自 :https://blog.51cto.com/g

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码