當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot 整合 Spring Cloud Alibaba Nacos 连通性+负载均衡
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 整合 Spring Cloud Alibaba Nacos 连通性+负载均衡
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 一、整合版本說明
- 1. 畢業(yè)版本依賴關(guān)系(推薦使用)
- 2. 組件版本關(guān)系
- 3. 演示版本
- 二、整合實戰(zhàn)
- 2.1. 聚合模塊設(shè)計
- 2.2. 創(chuàng)建聚合parent
- 2.3. 依次創(chuàng)建子項目
- 三、子模塊配置
- 3.1. 訂單模塊
- 3.2. 產(chǎn)品模塊
- 3.3. 用戶模塊
- 3.4. 扣庫存模塊
- 3.5. 購物車模塊
- 四、測試案例
- 4.1. 訂單模塊
- 4.2. 產(chǎn)品模塊
- 4.3. 用戶模塊
- 4.4. 扣庫存模塊
- 4.5. 購物車模塊
- 五、連通性測試
- 5.1. 請求地址
- 5.2. nacos服務(wù)端
- 5.3. 效果圖
- 六、負載均衡測試
- 6.1. 請求地址
- 6.2. 測試設(shè)計
- 6.3. 登陸nacos
- 6.4. 連續(xù)請求10次,觀察命中概率
- 6.5. nacos 將服務(wù)下線
- 6.6. 重新上線
- 6.7. 碼云開源地址
一、整合版本說明
1. 畢業(yè)版本依賴關(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
二、整合實戰(zhàn)
2.1. 聚合模塊設(shè)計
| 訂單模塊 | order-serv | 8000 |
| 產(chǎn)品模塊 | product-serv | 9000 |
| 用戶模塊 | user-serv | 15000 |
| 扣庫存模塊 | stock-serv | 11000 |
| 購物車模塊 | shopcart-serv | 12000 |
2.2. 創(chuàng)建聚合parent
創(chuàng)建maven父工程名稱為EShopParent
父工程依賴添加 ```bash<!--服務(wù)注冊發(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)建子項目
依次創(chuàng)建5個子模塊
三、子模塊配置
3.1. 訂單模塊
server:port: 8000 spring:cloud:nacos:discovery:service: order-servserver-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//負載均衡+動態(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. 扣庫存模塊
server:port: 11000 spring:cloud:nacos:discovery:service: stock-servserver-addr: localhost:88483.5. 購物車模塊
server:port: 12000 spring:cloud:nacos:discovery:service: shop-cart-servserver-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 {@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ù),通過商品ID獲取商品名稱String productNmae = restTemplate.getForObject("http://product-serv/product/" + productId, String.class);// 調(diào)用用戶服務(wù),通過用戶ID獲取用戶名稱String userNmae = restTemplate.getForObject("http://user-serv/user/" + userId, String.class);// 調(diào)用扣庫存服務(wù),通過商品ID將已購買的商品從庫存中刪除String result = restTemplate.getForObject("http://stock-serv/stock/reduce/" + productId, String.class);// 調(diào)用個購物車服務(wù),通過商品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. 產(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專家";} }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=112225.2. nacos服務(wù)端
Nacos 官網(wǎng):
https://nacos.io/zh-cn/docs/quick-start.html
5.3. 效果圖
以上5個微服務(wù)集成nacos完畢!并測試連通性測試通過!
六、負載均衡測試
6.1. 請求地址
http://localhost:9000/order/create?productId=11&userId=112226.2. 測試設(shè)計
分別啟動3個訂單模塊端口為9000、9001、9002
分別啟動3個扣庫存模塊端口為8000、8001、8002
6.3. 登陸nacos
6.4. 連續(xù)請求10次,觀察命中概率
6.5. nacos 將服務(wù)下線
應(yīng)用不停止
nacos觀察服務(wù)狀態(tài)已下線
再次測試
請求地址:
6.6. 重新上線
將下線的項目服務(wù)重新上線
負載均衡測試,應(yīng)該和正常請求一樣這里就不演示了。
6.7. 碼云開源地址
https://gitee.com/gb_90/eshop-parent
總結(jié)
以上是生活随笔為你收集整理的SpringBoot 整合 Spring Cloud Alibaba Nacos 连通性+负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uni-app获取腾讯地图计算两经纬度的
- 下一篇: (进阶篇)Redis6.2.0 集群 哨