當(dāng)前位置:
首頁(yè) >
SpringBoot 整合 Spring Cloud Alibaba Nacos 连通性+负载均衡
發(fā)布時(shí)間:2024/9/27
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 整合 Spring Cloud Alibaba Nacos 连通性+负载均衡
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、整合版本說(shuō)明
- 1. 畢業(yè)版本依賴(lài)關(guān)系(推薦使用)
- 2. 組件版本關(guān)系
- 3. 演示版本
- 二、整合實(shí)戰(zhàn)
- 2.1. 聚合模塊設(shè)計(jì)
- 2.2. 創(chuàng)建聚合parent
- 2.3. 依次創(chuàng)建子項(xiàng)目
- 三、子模塊配置
- 3.1. 訂單模塊
- 3.2. 產(chǎn)品模塊
- 3.3. 用戶模塊
- 3.4. 扣庫(kù)存模塊
- 3.5. 購(gòu)物車(chē)模塊
- 四、測(cè)試案例
- 4.1. 訂單模塊
- 4.2. 產(chǎn)品模塊
- 4.3. 用戶模塊
- 4.4. 扣庫(kù)存模塊
- 4.5. 購(gòu)物車(chē)模塊
- 五、連通性測(cè)試
- 5.1. 請(qǐng)求地址
- 5.2. nacos服務(wù)端
- 5.3. 效果圖
- 六、負(fù)載均衡測(cè)試
- 6.1. 請(qǐng)求地址
- 6.2. 測(cè)試設(shè)計(jì)
- 6.3. 登陸nacos
- 6.4. 連續(xù)請(qǐng)求10次,觀察命中概率
- 6.5. nacos 將服務(wù)下線
- 6.6. 重新上線
- 6.7. 碼云開(kāi)源地址
一、整合版本說(shuō)明
1. 畢業(yè)版本依賴(lài)關(guān)系(推薦使用)
| 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. 組件版本關(guān)系
| 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 Hoxton.SR9 | 2.2.6.RELEASE | 2.3.2.RELEASE | 1.4.2 | 1.8.202 |
官網(wǎng)地址:
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
二、整合實(shí)戰(zhàn)
2.1. 聚合模塊設(shè)計(jì)
| 訂單模塊 | order-serv | 8000 |
| 產(chǎn)品模塊 | product-serv | 9000 |
| 用戶模塊 | user-serv | 15000 |
| 扣庫(kù)存模塊 | stock-serv | 11000 |
| 購(gòu)物車(chē)模塊 | shopcart-serv | 12000 |
2.2. 創(chuàng)建聚合parent
創(chuàng)建maven父工程名稱(chēng)為EShopParent
父工程依賴(lài)添加 ```bash<!--服務(wù)注冊(cè)發(fā)現(xiàn)--><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. 依次創(chuàng)建子項(xiàng)目
依次創(chuàng)建5個(gè)子模塊
三、子模塊配置
3.1. 訂單模塊
server:port: 8000 spring:cloud:nacos:discovery:service: order-servserver-addr: localhost:8848啟動(dòng)類(lèi)
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//負(fù)載均衡+動(dòng)態(tài)路路由public RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderApplication.class);} }3.2. 產(chǎn)品模塊
server:port: 9000 spring:cloud:nacos:discovery:service: product-servserver-addr: localhost:88483.3. 用戶模塊
server:port: 15000 spring:cloud:nacos:discovery:service: user-servserver-addr: localhost:88483.4. 扣庫(kù)存模塊
server:port: 11000 spring:cloud:nacos:discovery:service: stock-servserver-addr: localhost:88483.5. 購(gòu)物車(chē)模塊
server:port: 12000 spring:cloud:nacos:discovery:service: shop-cart-servserver-addr: localhost:8848四、測(cè)試案例
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 {@Autowiredprivate RestTemplate restTemplate;//http://localhost:8000/order/create?productId=11&userId=11222@GetMapping("/order/create")public String createOrder(Integer productId, Integer userId) {// 調(diào)用商品服務(wù),通過(guò)商品ID獲取商品名稱(chēng)String productNmae = restTemplate.getForObject("http://product-serv/product/" + productId, String.class);// 調(diào)用用戶服務(wù),通過(guò)用戶ID獲取用戶名稱(chēng)String userNmae = restTemplate.getForObject("http://user-serv/user/" + userId, String.class);// 調(diào)用扣庫(kù)存服務(wù),通過(guò)商品ID將已購(gòu)買(mǎi)的商品從庫(kù)存中刪除String result = restTemplate.getForObject("http://stock-serv/stock/reduce/" + productId, String.class);// 調(diào)用個(gè)購(gòu)物車(chē)服務(wù),通過(guò)商品ID和用戶ID將已購(gòu)買(mǎi)的商品從購(gòu)物車(chē)中移除String shopCartResult = restTemplate.getForObject("http://shop-cart-serv/shopcart/remove?productId=" + productId + "&userId=" + userId, String.class);return "[用戶]: " + userNmae + " 購(gòu)買(mǎi)商品 " + productNmae + " " + result + " " + shopCartResult;} }4.2. 產(chǎn)品模塊
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專(zhuān)家";} }4.4. 扣庫(kù)存模塊
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("減庫(kù)存一個(gè)成功");return "減庫(kù)存一個(gè)成功!";} }4.5. 購(gòu)物車(chē)模塊
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 "移除購(gòu)物車(chē)成功!";} }五、連通性測(cè)試
5.1. 請(qǐng)求地址
http://localhost:9000/order/create?productId=11&userId=112225.2. nacos服務(wù)端
Nacos 官網(wǎng):
https://nacos.io/zh-cn/docs/quick-start.html
5.3. 效果圖
以上5個(gè)微服務(wù)集成nacos完畢!并測(cè)試連通性測(cè)試通過(guò)!
六、負(fù)載均衡測(cè)試
6.1. 請(qǐng)求地址
http://localhost:9000/order/create?productId=11&userId=112226.2. 測(cè)試設(shè)計(jì)
分別啟動(dòng)3個(gè)訂單模塊端口為9000、9001、9002
分別啟動(dòng)3個(gè)扣庫(kù)存模塊端口為8000、8001、8002
6.3. 登陸nacos
6.4. 連續(xù)請(qǐng)求10次,觀察命中概率
6.5. nacos 將服務(wù)下線
應(yīng)用不停止
nacos觀察服務(wù)狀態(tài)已下線
再次測(cè)試
請(qǐng)求地址:
6.6. 重新上線
將下線的項(xiàng)目服務(wù)重新上線
負(fù)載均衡測(cè)試,應(yīng)該和正常請(qǐng)求一樣這里就不演示了。
6.7. 碼云開(kāi)源地址
https://gitee.com/gb_90/eshop-parent
總結(jié)
以上是生活随笔為你收集整理的SpringBoot 整合 Spring Cloud Alibaba Nacos 连通性+负载均衡的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: uni-app获取腾讯地图计算两经纬度的
- 下一篇: Nacos 集群集成SpringBoot