文章目錄
一、版本選取、需求和項目簡述
1. 版本選取
框架
| 版本
| 說明
|
spring-cloud-alibaba
| 2.2.6.RELEASE
| 版本要對應
|
spring-boot
| 2.3.2.RELEASE
| 版本要對應
|
nacos
| 1.4.2
| 版本要對應
|
org.apache.dubbo
| 2.7.8
| 版本要對應
|
2. 項目模塊說明
模塊
| 說明
|
EShopParent
| 父工程
|
DubboApi
| 接口子模塊
|
Order-serv
| 訂單模塊
|
Stock-serv
| 扣庫存模塊
|
product-serv
| 產(chǎn)品模塊
|
2. 需求說明
訂單模塊調(diào)用扣庫存模塊完成庫庫存的業(yè)務
二、需求實戰(zhàn)-依賴初始化
2.1. 創(chuàng)建maven父工程EShopParent
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gblfy</groupId>
<artifactId>EShopParent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Order-serv</module>
<module>Stock-serv</module>
<module>DubboApi</module>
<module>product-serv</module>
</modules>
<description>父工程 所有子工程需要依賴此工程</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<!--springMVC啟動器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos服務發(fā)現(xiàn)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--dubbo組件-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring-cloud-alibaba依賴版本控制-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.2. 創(chuàng)建子模塊DubboApi
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EShopParent</artifactId>
<groupId>com.gblfy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>DubboApi</artifactId>
</project>
2.3. 創(chuàng)建服務端Stock-serv
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EShopParent</artifactId>
<groupId>com.gblfy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Stock-serv</artifactId>
<dependencies>
<dependency>
<groupId>com.gblfy</groupId>
<artifactId>DubboApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.4. 創(chuàng)建服務端product-serv
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EShopParent</artifactId>
<groupId>com.gblfy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product-serv</artifactId>
<dependencies>
<dependency>
<groupId>com.gblfy</groupId>
<artifactId>DubboApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.5. 創(chuàng)建消費端端Order-serv
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>EShopParent</artifactId>
<groupId>com.gblfy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.gblfy</groupId>
<artifactId>Order-serv</artifactId>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>com.gblfy</groupId>
<artifactId>DubboApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
三、需求實戰(zhàn)-代碼編寫
3.1. 創(chuàng)建公共接口
在DubboApi子模塊中創(chuàng)建調(diào)用扣庫存模塊(Stock-serv)的接口IStockService
package com.gblfy.stock.api;
public interface IStockService {
public String reduce(Integer productId, Integer userId);
}
在DubboApi子模塊中創(chuàng)建調(diào)用產(chǎn)品(product-serv)模塊的接口
package com.gblfy.product.api;
public interface IProductService {
public String buyProduct(Integer productId, Integer userId);
}
3.2. 扣庫存服務端編寫
在Stock-serv子模塊中實現(xiàn)接口實現(xiàn)類StockServiceimpl
package com.gblfy.service.impl;
import com.gblfy.stock.api.IStockService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class StockServiceimpl implements IStockService {
@Override
public String reduce(Integer productId, Integer userId) {
return "用戶編號: " + userId + "產(chǎn)品編碼: " + productId + "減庫存1個";
}
}
啟動類上添加@EnableDiscoveryClient注解
package com.gblfy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class StockApplication {
public static void main(String[] args) {
SpringApplication.run(StockApplication.class);
}
}
3.3. 產(chǎn)品服務端編寫
在product-serv子模塊中實現(xiàn)接口實現(xiàn)類ProductServiceImpl
package com.gblfy.service.impl;
import com.gblfy.product.api.IProductService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class ProductServiceImpl implements IProductService {
@Override
public String buyProduct(Integer productId, Integer userId) {
return "用戶編號: " + userId + "產(chǎn)品編碼: " + productId + "購買PHONE SUCCESS";
}
}
啟動類添加@EnableDiscoveryClient注解
package com.gblfy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class);
}
}
3.5. 消費端編寫
在Order-serv子模塊的啟動類上添加@EnableDiscoveryClient開啟服務發(fā)現(xiàn)掃描
package com.gblfy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderAppliaction {
public static void main(String[] args) {
SpringApplication.run(OrderAppliaction.class);
}
}
創(chuàng)建一個客戶端類發(fā)起請求OrderController分別向扣庫存模塊和產(chǎn)品模塊發(fā)起請求
package com.gblfy.controller;
import com.gblfy.product.api.IProductService;
import com.gblfy.stock.api.IStockService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@DubboReference
private IStockService stockService;
@DubboReference
private IProductService productService;
@GetMapping("/order/create")
public String createOrder(Integer productId, Integer userId) {
//調(diào)用扣庫存模塊服務
return stockService.reduce(productId, userId);
}
//http://127.0.0.1:8080/order/create?productId=2&userId=8
@GetMapping("/buyProduct")
public String buyProduct(Integer productId, Integer userId) {
//調(diào)用產(chǎn)品模塊服務
return productService.buyProduct(productId, userId);
}
// http://127.0.0.1:8080/buyProduct?productId=2&userId=8
}
四、需求實戰(zhàn)-配置編寫
4.1. 扣庫存服務端配置
# 應用端口
server:
port: 8082
# nacos服務發(fā)現(xiàn)配置
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
service: stock-serv
application:
name: stock-serv
# Dubbo服務配置
4.2. 產(chǎn)品服務端配置
server:
port: 8081
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
service: product-serv
application:
name: product-serv
# Dubbo服務配置
4.3.消費端配置
server:
port: 8080
spring:
application:
name: order-serv
cloud:
nacos:
discovery:
server-addr: http://127.0.0.1:8848
config:
server-addr: http://127.0.0.1:8848
# Dubbo服務配置
五、需求測試實戰(zhàn)
5.1. 啟動nacos
5.2. 啟動服務端
分別依次啟動扣庫存服務端和產(chǎn)品服務端
5.3. 啟消費端
5.4. 查看nacos
5.5. 項目模塊分布結(jié)構(gòu)
六、測試實戰(zhàn)
6.1. 請求扣庫存鏈路
??http://127.0.0.1:8080/order/create?productId=2&userId=8??
6.2. 請求產(chǎn)品鏈路
??http://127.0.0.1:8080/buyProduct?productId=2&userId=8??
6.3. 常見的異常
沒有服務的提供者,啟動客戶端