《sca-trade :简易交易系统》系列文章
《sca-trade :简易交易系统》开篇
《sca-trade :简易交易系统》微服务项目搭建
《sca-trade :简易交易系统》代码实现
引入 Spring-Cloud-Alibaba
父工程 pom 文件直接加入如下依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
引入注册中心
这里我们采用 Nacos
Nacos 下载安装
进入官网,找到合适的版本即可下载,我这里选了最新版本。
启动 Nacos
## 单机启动 Nacos
sh startup.sh -m standalone
## 停止 Nacos
sh shutdown.sh
引入依赖
以用户微服务为例,pom文件中引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
yml配置文件中添加配置
spring:
application:
name: server-trade-user
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
启动类开启注册中心,改造后代码如下
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
log.info("----------=OrderApplication Start=----------");
}
}
启动服务后,通过网址即可查看 当前服务是否已经注册上 Nacos
注意:如果你把 Nacos 安装在 Linux 服务器上,一定要记得开启 9848
端口,否则微服务会找不到 Nacos 而无法启动
相同的操作把其他微服务注册到 Nacos 上,这里不做赘述。
引入 RPC远程调用框架
这里我们采用 openFeign
引入依赖
直接引入订单微服务,pom文件中引入依赖,其他微服务不需要引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类开启RPC,改造后代码如下
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
log.info("----------=OrderApplication Start=----------");
}
}
订单服务代码改造
在 io.shijiev.trade.order.fegin
路径添加 ProductService
和 UserService
@FeignClient("server-trade-product") // 微服务的服务名称
public interface ProductService {
// 查询商品信息
@RequestMapping("/trade-product/getProductById/{productId}")
Product getProductById(@PathVariable("productId") Long productId);
// 更新库存信息
@RequestMapping("/trade-product/updateProductById")
Integer updateProductById(@RequestBody Product product);
}
@FeignClient("server-trade-user") // 微服务的服务名称
public interface UserService {
@RequestMapping("/trade-user/getUserById/{userId}")
User getUserById(@PathVariable("userId") Long userId);
}
在orderService 里面添加处理逻辑,
@Override
public Integer saveOrder(OrderParams orderParams) {
// 参数判空
if (orderParams.isEmpty()){
throw new RuntimeException("参数异常: " + JSONObject.toJSONString(orderParams));
}
// 查询用户信息
User user = userService.getUserById(orderParams.getUserId());
if (user == null){
throw new RuntimeException("未获取到用户信息: " + JSONObject.toJSONString(orderParams));
}
// 查询商品信息
Product product = productService.getProductById(orderParams.getProductId());
if (product == null){
throw new RuntimeException("未获取到商品信息: " + JSONObject.toJSONString(orderParams));
}
if (product.getProductStock() < orderParams.getCount()){
throw new RuntimeException("商品库存不足: " + JSONObject.toJSONString(orderParams));
}
Order order = new Order();
order.setAddress(user.getAddress());
order.setPhone(user.getPhone());
order.setUserId(user.getId());
order.setUserName(user.getUserName());
order.setTotalPrice(product.getProductPrice().multiply(BigDecimal.valueOf(orderParams.getCount())));
orderMapper.addOrder(order);
log.info("-----------=订单添加成功=---------");
OrderItem orderItem = new OrderItem();
orderItem.setNumber(orderParams.getCount());
orderItem.setOrderId(order.getId());
orderItem.setProductId(product.getId());
orderItem.setProductName(product.getProductName());
orderItem.setProductPrice(product.getProductPrice());
orderItemMapper.addOrderItem(orderItem);
log.info("-----------=订单明细添加成功=---------");
Integer stock = product.getProductStock() - orderParams.getCount();
product.setProductStock(stock);
Integer i = productService.updateProductById(product);
log.info("-----------=库存扣减成功=---------");
return i;
}
使用测试工具进行调用,
响应成功,查看数据库表,对比数据,数据正确,整个交易完成,系统搭建成功 ~~