个人网站内容,目前网站类型主要包括哪几种,wordpress模板框架,大连市平台网站文章目录一、版本选取、需求和项目简述1. 版本选取2. 项目模块说明2. 需求说明二、需求实战-依赖初始化2.1. 创建maven父工程EShopParent2.2. 创建子模块DubboApi2.3. 创建服务端Stock-serv2.4. 创建服务端product-serv2.5. 创建消费端端Order-serv三、需求实战-代码编写3.1. 创…
文章目录一、版本选取、需求和项目简述1. 版本选取2. 项目模块说明2. 需求说明二、需求实战-依赖初始化2.1. 创建maven父工程EShopParent2.2. 创建子模块DubboApi2.3. 创建服务端Stock-serv2.4. 创建服务端product-serv2.5. 创建消费端端Order-serv三、需求实战-代码编写3.1. 创建公共接口3.2. 扣库存服务端编写3.3. 产品服务端编写3.5. 消费端编写四、需求实战-配置编写4.1. 扣库存服务端配置4.2. 产品服务端配置4.3.消费端配置五、需求测试实战5.1. 启动nacos5.2. 启动服务端5.3. 启消费端5.4. 查看nacos5.5. 项目模块分布结构六、测试实战6.1. 请求扣库存链路6.2. 请求产品链路6.3. 常见的异常一、版本选取、需求和项目简述
1. 版本选取
框架版本说明spring-cloud-alibaba2.2.6.RELEASE版本要对应spring-boot2.3.2.RELEASE版本要对应nacos1.4.2版本要对应org.apache.dubbo2.7.8版本要对应
2. 项目模块说明
模块说明EShopParent父工程DubboApi接口子模块Order-serv订单模块Stock-serv扣库存模块product-serv产品模块
2. 需求说明
订单模块调用扣库存模块完成库库存的业务
二、需求实战-依赖初始化
2.1. 创建maven父工程EShopParent
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.gblfy/groupIdartifactIdEShopParent/artifactIdpackagingpom/packagingversion1.0-SNAPSHOT/versionmodulesmoduleOrder-serv/modulemoduleStock-serv/modulemoduleDubboApi/modulemoduleproduct-serv/module/modulesdescription父工程 所有子工程需要依赖此工程/descriptionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.2.RELEASE/version/parentdependencies!--springMVC启动器--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--nacos服务发现--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!--dubbo组件--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-dubbo/artifactId/dependency/dependenciesdependencyManagementdependencies!--spring-cloud-alibaba依赖版本控制--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2.2.6.RELEASE/versionscopeimport/scopetypepom/type/dependency/dependencies/dependencyManagement
/project2.2. 创建子模块DubboApi
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdEShopParent/artifactIdgroupIdcom.gblfy/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdDubboApi/artifactId/project2.3. 创建服务端Stock-serv
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdEShopParent/artifactIdgroupIdcom.gblfy/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdStock-serv/artifactIddependenciesdependencygroupIdcom.gblfy/groupIdartifactIdDubboApi/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies/project2.4. 创建服务端product-serv
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdEShopParent/artifactIdgroupIdcom.gblfy/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdproduct-serv/artifactIddependenciesdependencygroupIdcom.gblfy/groupIdartifactIdDubboApi/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies
/project2.5. 创建消费端端Order-serv
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdEShopParent/artifactIdgroupIdcom.gblfy/groupIdversion1.0-SNAPSHOT/version/parentgroupIdcom.gblfy/groupIdartifactIdOrder-serv/artifactIdmodelVersion4.0.0/modelVersiondependenciesdependencygroupIdcom.gblfy/groupIdartifactIdDubboApi/artifactIdversion1.0-SNAPSHOT/version/dependency/dependencies/project三、需求实战-代码编写
3.1. 创建公共接口
在DubboApi子模块中创建调用扣库存模块Stock-serv的接口IStockService
package com.gblfy.stock.api;public interface IStockService {public String reduce(Integer productId, Integer userId);
}在DubboApi子模块中创建调用产品product-serv模块的接口
package com.gblfy.product.api;public interface IProductService {public String buyProduct(Integer productId, Integer userId);
}3.2. 扣库存服务端编写
在Stock-serv子模块中实现接口实现类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 {Overridepublic String reduce(Integer productId, Integer userId) {return 用户编号: userId 产品编码: 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. 产品服务端编写
在product-serv子模块中实现接口实现类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 {Overridepublic String buyProduct(Integer productId, Integer userId) {return 用户编号: userId 产品编码: 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开启服务发现扫描
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);}
}
创建一个客户端类发起请求OrderController分别向扣库存模块和产品模块发起请求
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 {DubboReferenceprivate IStockService stockService;DubboReferenceprivate IProductService productService;GetMapping(/order/create)public String createOrder(Integer productId, Integer userId) {//调用扣库存模块服务return stockService.reduce(productId, userId);}//http://127.0.0.1:8080/order/create?productId2userId8GetMapping(/buyProduct)public String buyProduct(Integer productId, Integer userId) {//调用产品模块服务return productService.buyProduct(productId, userId);}// http://127.0.0.1:8080/buyProduct?productId2userId8
}四、需求实战-配置编写
4.1. 扣库存服务端配置
# 应用端口
server:port: 8082# nacos服务发现配置
spring:cloud:nacos:server-addr: 127.0.0.1:8848discovery:service: stock-servapplication:name: stock-serv# Dubbo服务配置
dubbo:scan:base-packages: com.gblfy.service.implprotocol:name: dubboport: -1registry:address: spring-cloud://127.0.0.14.2. 产品服务端配置
server:port: 8081
spring:cloud:nacos:server-addr: 127.0.0.1:8848discovery:service: product-servapplication:name: product-serv# Dubbo服务配置
dubbo:scan:base-packages: com.gblfy.service.implprotocol:name: dubboport: -1registry:address: spring-cloud://127.0.0.14.3.消费端配置
server:port: 8080
spring:application:name: order-servcloud:nacos:discovery:server-addr: http://127.0.0.1:8848config:server-addr: http://127.0.0.1:8848# Dubbo服务配置
dubbo:protocol:name: dubboport: -1registry:address: spring-cloud://127.0.0.1cloud:subscribed-services:- stock-serv- product-serv
五、需求测试实战
5.1. 启动nacos 5.2. 启动服务端
分别依次启动扣库存服务端和产品服务端
5.3. 启消费端 5.4. 查看nacos 5.5. 项目模块分布结构 六、测试实战
6.1. 请求扣库存链路
http://127.0.0.1:8080/order/create?productId2userId8
6.2. 请求产品链路
http://127.0.0.1:8080/buyProduct?productId2userId8 6.3. 常见的异常
没有服务的提供者启动客户端