网站成品超市,成都小程序开发一个多少钱啊,苏州网站关键词推广,龙岗建设网站文章目录一、整合版本说明1. 毕业版本依赖关系(推荐使用)2. 组件版本关系3. 演示版本二、整合实战2.1. 聚合模块设计2.2. 创建聚合parent2.3. 依次创建子项目三、子模块配置3.1. 订单模块3.2. 产品模块3.3. 用户模块3.4. 扣库存模块3.5. 购物车模块四、测试案例4.1. 订单模块4.…
文章目录一、整合版本说明1. 毕业版本依赖关系(推荐使用)2. 组件版本关系3. 演示版本二、整合实战2.1. 聚合模块设计2.2. 创建聚合parent2.3. 依次创建子项目三、子模块配置3.1. 订单模块3.2. 产品模块3.3. 用户模块3.4. 扣库存模块3.5. 购物车模块四、测试案例4.1. 订单模块4.2. 产品模块4.3. 用户模块4.4. 扣库存模块4.5. 购物车模块五、连通性测试5.1. 请求地址5.2. nacos服务端5.3. 效果图六、负载均衡测试6.1. 请求地址6.2. 测试设计6.3. 登陆nacos6.4. 连续请求10次观察命中概率6.5. nacos 将服务下线6.6. 重新上线6.7. 码云开源地址一、整合版本说明
1. 毕业版本依赖关系(推荐使用)
Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot VersionSpring Cloud 2020.0.02021.12.4.2Spring Cloud Hoxton.SR92.2.6.RELEASE2.3.2.RELEASESpring Cloud Greenwich.SR62.1.4.RELEASE2.1.13.RELEASESpring Cloud Hoxton.SR32.2.1.RELEASE2.2.5.RELEASESpring Cloud Hoxton.RELEASE2.2.0.RELEASE2.2.X.RELEASESpring Cloud Greenwich2.1.2.RELEASE2.1.X.RELEASE
2. 组件版本关系
Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version2.2.6.RELEASE1.8.11.4.24.4.02.7.81.3.02021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE1.8.01.4.14.4.02.7.81.3.02.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE1.8.01.3.34.4.02.7.81.3.02.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.02.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.0
3. 演示版本
Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot VersionNacos VersionjdkSpring Cloud Hoxton.SR92.2.6.RELEASE2.3.2.RELEASE1.4.21.8.202
官网地址 https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
二、整合实战
2.1. 聚合模块设计
模块划分微服务划分端口订单模块order-serv8000产品模块product-serv9000用户模块user-serv15000扣库存模块stock-serv11000购物车模块shopcart-serv12000
2.2. 创建聚合parent
创建maven父工程名称为EShopParent
父工程依赖添加
bash!--服务注册发现--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependencydependencyManagementdependencies!--spring-cloud-alibaba 版本控制--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2.2.6.RELEASE/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement2.3. 依次创建子项目
依次创建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 {BeanLoadBalanced//负载均衡动态路路由public RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderApplication.class);}
}
3.2. 产品模块
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?productId11userId11222GetMapping(/order/create)public String createOrder(Integer productId, Integer userId) {// 调用商品服务通过商品ID获取商品名称String productNmae restTemplate.getForObject(http://product-serv/product/ productId, String.class);// 调用用户服务通过用户ID获取用户名称String userNmae restTemplate.getForObject(http://user-serv/user/ userId, String.class);// 调用扣库存服务通过商品ID将已购买的商品从库存中删除String result restTemplate.getForObject(http://stock-serv/stock/reduce/ productId, String.class);// 调用个购物车服务通过商品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. 产品模块
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/ productIdGetMapping(/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?productId11userId112225.2. nacos服务端
Nacos 官网 https://nacos.io/zh-cn/docs/quick-start.html
5.3. 效果图 以上5个微服务集成nacos完毕并测试连通性测试通过
六、负载均衡测试
6.1. 请求地址
http://localhost:9000/order/create?productId11userId112226.2. 测试设计
分别启动3个订单模块端口为9000、9001、9002 分别启动3个扣库存模块端口为8000、8001、8002
6.3. 登陆nacos 6.4. 连续请求10次观察命中概率 6.5. nacos 将服务下线 应用不停止 nacos观察服务状态已下线
再次测试 请求地址
http://localhost:9000/order/create?productId11userId112226.6. 重新上线
将下线的项目服务重新上线 负载均衡测试应该和正常请求一样这里就不演示了。
6.7. 码云开源地址
https://gitee.com/gb_90/eshop-parent