文章目錄
一、版本選取、需求和項(xiàng)目簡(jiǎn)述
1. 版本選取
框架
| 版本
| 說(shuō)明
|
spring-cloud-alibaba
| 2.2.6.RELEASE
| 版本要對(duì)應(yīng)
|
spring-boot
| 2.3.2.RELEASE
| 版本要對(duì)應(yīng)
|
nacos
| 1.4.2
| 版本要對(duì)應(yīng)
|
org.apache.dubbo
| 2.7.8
| 版本要對(duì)應(yīng)
|
2. 項(xiàng)目模塊說(shuō)明
模塊
| 說(shuō)明
|
EShopParent
| 父工程
|
DubboApi
| 接口子模塊
|
Order-serv
| 訂單模塊
|
Stock-serv
| 扣庫(kù)存模塊
|
product-serv
| 產(chǎn)品模塊
|
2. 需求說(shuō)明
訂單模塊調(diào)用扣庫(kù)存模塊完成庫(kù)庫(kù)存的業(yè)務(wù)
二、需求實(shí)戰(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啟動(dòng)器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos服務(wù)發(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)建服務(wù)端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)建服務(wù)端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)建消費(fèi)端端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>
三、需求實(shí)戰(zhàn)-代碼編寫
3.1. 創(chuàng)建公共接口
在DubboApi子模塊中創(chuàng)建調(diào)用扣庫(kù)存模塊(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. 扣庫(kù)存服務(wù)端編寫
在Stock-serv子模塊中實(shí)現(xiàn)接口實(shí)現(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 "用戶編號(hào): " + userId + "產(chǎn)品編碼: " + productId + "減庫(kù)存1個(gè)";
}
}
啟動(dòng)類上添加@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)品服務(wù)端編寫
在product-serv子模塊中實(shí)現(xiàn)接口實(shí)現(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 "用戶編號(hào): " + userId + "產(chǎn)品編碼: " + productId + "購(gòu)買PHONE SUCCESS";
}
}
啟動(dòng)類添加@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. 消費(fèi)端編寫
在Order-serv子模塊的啟動(dòng)類上添加@EnableDiscoveryClient開啟服務(wù)發(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)建一個(gè)客戶端類發(fā)起請(qǐng)求OrderController分別向扣庫(kù)存模塊和產(chǎn)品模塊發(fā)起請(qǐng)求
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)用扣庫(kù)存模塊服務(wù)
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)品模塊服務(wù)
return productService.buyProduct(productId, userId);
}
// http://127.0.0.1:8080/buyProduct?productId=2&userId=8
}
四、需求實(shí)戰(zhàn)-配置編寫
4.1. 扣庫(kù)存服務(wù)端配置
# 應(yīng)用端口
server:
port: 8082
# nacos服務(wù)發(fā)現(xiàn)配置
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
service: stock-serv
application:
name: stock-serv
# Dubbo服務(wù)配置
4.2. 產(chǎn)品服務(wù)端配置
server:
port: 8081
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
service: product-serv
application:
name: product-serv
# Dubbo服務(wù)配置
4.3.消費(fèi)端配置
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服務(wù)配置
五、需求測(cè)試實(shí)戰(zhàn)
5.1. 啟動(dòng)nacos
5.2. 啟動(dòng)服務(wù)端
分別依次啟動(dòng)扣庫(kù)存服務(wù)端和產(chǎn)品服務(wù)端
5.3. 啟消費(fèi)端
5.4. 查看nacos
5.5. 項(xiàng)目模塊分布結(jié)構(gòu)
六、測(cè)試實(shí)戰(zhàn)
6.1. 請(qǐng)求扣庫(kù)存鏈路
??http://127.0.0.1:8080/order/create?productId=2&userId=8??
6.2. 請(qǐng)求產(chǎn)品鏈路
??http://127.0.0.1:8080/buyProduct?productId=2&userId=8??
6.3. 常見(jiàn)的異常
沒(méi)有服務(wù)的提供者,啟動(dòng)客戶端