文章目錄
一、整合版本說明
1. 畢業(yè)版本依賴關(guān)系(推薦使用)
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. 組件版本關(guān)系
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
|
官網(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ì)
模塊劃分
| 微服務(wù)劃分
| 端口
|
訂單模塊
| order-serv
| 8000
|
產(chǎn)品模塊
| product-serv
| 9000
|
用戶模塊
| user-serv
| 15000
|
扣庫(kù)存模塊
| stock-serv
| 11000
|
購(gòu)物車模塊
| shopcart-serv
| 12000
|
2.2. 創(chuàng)建聚合parent
創(chuàng)建maven父工程名稱為EShopParent
父工程依賴添加
```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-serv
server-addr: localhost:8848
啟動(dòng)類
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-serv
server-addr: localhost:8848
3.3. 用戶模塊
server:
port: 15000
spring:
cloud:
nacos:
discovery:
service: user-serv
server-addr: localhost:8848
3.4. 扣庫(kù)存模塊
server:
port: 11000
spring:
cloud:
nacos:
discovery:
service: stock-serv
server-addr: localhost:8848
3.5. 購(gòu)物車模塊
server:
port: 12000
spring:
cloud:
nacos:
discovery:
service: shop-cart-serv
server-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 {
@Autowired
private 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)用扣庫(kù)存服務(wù),通過商品ID將已購(gòu)買的商品從庫(kù)存中刪除
String result = restTemplate.getForObject("http://stock-serv/stock/reduce/" + productId, String.class);
// 調(diào)用個(gè)購(gòu)物車服務(wù),通過商品ID和用戶ID將已購(gòu)買的商品從購(gòu)物車中移除
String shopCartResult = restTemplate.getForObject("http://shop-cart-serv/shopcart/remove?productId=" + productId + "&userId=" + userId, String.class);
return "[用戶]: " + userNmae + " 購(gòu)買商品 " + 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. 扣庫(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)物車模塊
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)物車成功!";
}
}
五、連通性測(cè)試
5.1. 請(qǐng)求地址
http://localhost:9000/order/create?productId=11&userId=11222
5.2. nacos服務(wù)端
Nacos 官網(wǎng):
??https://nacos.io/zh-cn/docs/quick-start.html??
5.3. 效果圖
以上5個(gè)微服務(wù)集成nacos完畢!并測(cè)試連通性測(cè)試通過!
六、負(fù)載均衡測(cè)試
6.1. 請(qǐng)求地址
http://localhost:9000/order/create?productId=11&userId=11222
6.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)求地址:
http://localhost:9000/order/create?productId=11&userId=11222
6.6. 重新上線
將下線的項(xiàng)目服務(wù)重新上線
負(fù)載均衡測(cè)試,應(yīng)該和正常請(qǐng)求一樣這里就不演示了。
6.7. 碼云開源地址
??https://gitee.com/gb_90/eshop-parent??