欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > [Java]微服务拆分2

[Java]微服务拆分2

2025/6/23 23:40:36 来源:https://blog.csdn.net/CSDN20221005/article/details/143844438  浏览:    关键词:[Java]微服务拆分2

用户服务拆分

创建数据库: 执行资料中sql文件即可

在hmall下新建一个module,命名为user-service

导入用户模块需要使用的包

 <dependencies><!--common--><dependency><groupId>com.heima</groupId><artifactId>hm-common</artifactId><version>1.0.0</version></dependency><!--hm-api--><dependency><groupId>com.heima</groupId><artifactId>hm-api</artifactId><version>1.0.0</version></dependency><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--数据库--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!--nacos 服务注册发现--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

添加启动类和配置文件

package com.hmall.user;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@MapperScan("com.hmall.user.mapper")
@EnableFeignClients(basePackages = "com.hmall.api.client")
@SpringBootApplication
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class, args);}
}
server:port: 8084 #每个微服务运行在不同端口
spring:application:name: user-service #每个微服务对应一个名称cloud:nacos:server-addr: 192.168.1.97:8848 #nacos地址profiles:active: dev #读取dev的配置datasource: #数据库配置url: jdbc:mysql://${hm.db.host}:3306/hm-user?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: ${hm.db.pw}
mybatis-plus:configuration:default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandlerglobal-config:db-config:update-strategy: not_nullid-type: auto
logging: #日志配置level: #日志记录级别com.hmall: debugpattern: #日志日期格式dateformat: HH:mm:ss:SSSfile:  #日志保存目录path: "logs/${spring.application.name}"
knife4j: #swagger配置enable: trueopenapi:title: 黑马商城用户管理接口文档description: "黑马商城用户管理接口文档"email: 123456@qq.comconcat: 王url: https://www.itcast.cnversion: v1.0.0group:default:group-name: defaultapi-rule: packageapi-rule-resources: # 指定扫描的包- com.hmall.user.controller

登录注册代码迁移

  1. 项目中登录注册采用的是JWT模式
  2. hmall.jsk是JWT加密的密钥文件
  3. JwtProperties文件是读取密钥时用于封装密钥信息
  4. SecurityConfig文件封装了加密用户密码的方法 和 读取密钥的方法
  5. JwtTool文件用于生成Token和解析Token

  1. 在配置文件中配置密钥文件的位置信息
server:
... ...
hm:jwt:location: classpath:hmall.jksalias: hmallpassword: hmall123tokenTTL: 30m

业务代码迁移

  1. 迁移工具类

  1. 迁移mapper

  • 由于项目中存在多个同名类, 所以需要手动导入类文件
  1. 迁移service

  1. 迁移controller

配置启动项, 启动服务

交易服务拆分

在hmall下新建一个module,命名为trade-service

导入订单模块需要使用的包

 <dependencies><!--common--><dependency><groupId>com.heima</groupId><artifactId>hm-common</artifactId><version>1.0.0</version></dependency><!--hm-api--><dependency><groupId>com.heima</groupId><artifactId>hm-api</artifactId><version>1.0.0</version></dependency><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--数据库--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!--nacos 服务注册发现--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

添加启动类和配置文件

package com.hmall.trade;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@MapperScan("com.hmall.trade.mapper")
@EnableFeignClients(basePackages = "com.hmall.api.client")
@SpringBootApplication
public class TradeApplication {public static void main(String[] args) {SpringApplication.run(TradeApplication.class, args);}
}
server:port: 8085 #每个微服务运行在不同端口
spring:application:name: trade-service #每个微服务对应一个名称cloud:nacos:server-addr: 192.168.1.97:8848 #nacos地址profiles:active: dev #读取dev的配置datasource: #数据库配置url: jdbc:mysql://${hm.db.host}:3306/hm-trade?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: ${hm.db.pw}
mybatis-plus:configuration:default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandlerglobal-config:db-config:update-strategy: not_nullid-type: auto
logging: #日志配置level: #日志记录级别com.hmall: debugpattern: #日志日期格式dateformat: HH:mm:ss:SSSfile:  #日志保存目录path: "logs/${spring.application.name}"
knife4j: #swagger配置enable: trueopenapi:title: 黑马商城交易管理接口文档description: "黑马商城交易管理接口文档"email: 123456@qq.comconcat: 王url: https://www.itcast.cnversion: v1.0.0group:default:group-name: defaultapi-rule: packageapi-rule-resources: # 指定扫描的包- com.hmall.trade.controller

业务代码迁移

  1. 迁移工具类

  1. 迁移mapper

  1. 迁移service

  1. 迁移controller

解决OrderServiceImpl报错

  1. 在交易服务中,用户下单时需要做下列事情:
  • 根据id查询商品列表
  • 计算商品总价
  • 保存订单
  • 扣减库存
  • 清理购物车商品
  1. 标黑的业务需要远程调用其他服务
  • 查询商品、扣减库存都是与商品有关的业务,在item-service中有相关功能;
  • 清理购物车商品是购物车业务,在cart-service中有相关功能。
  1. 抽取ItemClient接口
@FeignClient(value = "item-service",configuration = DefaultFeignConfig.class) // 指定拉取的服务名称
public interface ItemClient {@PutMapping("/items/stock/deduct")void deductStock(@RequestBody List<OrderDetailDTO> items);
}
  • hm-api中导入OrderDetailDTO实体类
  • trade-service中删除OrderDetailDTO实体类
  • trade-service中的OrderFormDTO重新导包
  1. 抽取CartClient接口
@FeignClient(value = "cart-service",configuration = DefaultFeignConfig.class) // 指定拉取的服务名称
public interface CartClient {@DeleteMapping("/carts")void deleteCartItemByIds(@RequestParam("ids") Collection<Long> ids);
}
  1. 改造OrderServiceImpl
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {private final IOrderDetailService detailService;private final ItemClient itemClient;private final CartClient cartClient;@Override@Transactionalpublic Long createOrder(OrderFormDTO orderFormDTO) {// 1.订单数据Order order = new Order();// 1.1.查询商品List<OrderDetailDTO> detailDTOS = orderFormDTO.getDetails();// 1.2.获取商品id和数量的MapMap<Long, Integer> itemNumMap = detailDTOS.stream().collect(Collectors.toMap(OrderDetailDTO::getItemId, OrderDetailDTO::getNum));Set<Long> itemIds = itemNumMap.keySet();// 1.3.查询商品// 改为远程调用List<ItemDTO> items = itemClient.queryItemByIds(itemIds);if (items == null || items.size() < itemIds.size()) {throw new BadRequestException("商品不存在");}// 1.4.基于商品价格、购买数量计算商品总价:totalFeeint total = 0;for (ItemDTO item : items) {total += item.getPrice() * itemNumMap.get(item.getId());}order.setTotalFee(total);// 1.5.其它属性order.setPaymentType(orderFormDTO.getPaymentType());order.setUserId(UserContext.getUser());order.setStatus(1);// 1.6.将Order写入数据库order表中save(order);// 2.保存订单详情List<OrderDetail> details = buildDetails(order.getId(), items, itemNumMap);detailService.saveBatch(details);// 3.清理购物车商品// 改为远程调用cartClient.deleteCartItemByIds(itemIds);// 4.扣减库存try {// 改为远程调用itemClient.deductStock(detailDTOS);} catch (Exception e) {throw new RuntimeException("库存不足!");}return order.getId();}}
  1. 清除重复DTO, 引入hm-api模块, 重新导包

配置启动项, 启动服务, 测试id: 1654779387523936258

支付服务拆分

新建UserCllient客户端 和 TradeClient客户端

@FeignClient("user-service")
public interface UserClient {@PutMapping("/users/money/deduct")void deductMoney(@RequestParam("pw") String pw, @RequestParam("amount") Integer amount);
}
@FeignClient("trade-service")
public interface TradeClient {@PutMapping("/orders/{orderId}")void markOrderPaySuccess(@PathVariable("orderId") Long orderId);
}

在hmall下新建一个module,命名为pay-service

导入订单模块需要使用的包

 <dependencies><!--common--><dependency><groupId>com.heima</groupId><artifactId>hm-common</artifactId><version>1.0.0</version></dependency><!--hm-api--><dependency><groupId>com.heima</groupId><artifactId>hm-api</artifactId><version>1.0.0</version></dependency><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--数据库--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!--nacos 服务注册发现--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

添加启动类和配置文件

package com.hmall.pay;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@MapperScan("com.hmall.pay.mapper")
@EnableFeignClients(basePackages = "com.hmall.api.client")
@SpringBootApplication
public class PayApplication {public static void main(String[] args) {SpringApplication.run(PayApplication.class, args);}
}
server:port: 8086 #每个微服务运行在不同端口
spring:application:name: pay-service #每个微服务对应一个名称cloud:nacos:server-addr: 192.168.1.97:8848 #nacos地址profiles:active: dev #读取dev的配置datasource: #数据库配置url: jdbc:mysql://${hm.db.host}:3306/hm-pay?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: ${hm.db.pw}
mybatis-plus:configuration:default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandlerglobal-config:db-config:update-strategy: not_nullid-type: auto
logging: #日志配置level: #日志记录级别com.hmall: debugpattern: #日志日期格式dateformat: HH:mm:ss:SSSfile:  #日志保存目录path: "logs/${spring.application.name}"
knife4j: #swagger配置enable: trueopenapi:title: 黑马商城支付管理接口文档description: "黑马商城支付管理接口文档"email: 123456@qq.comconcat: 王url: https://www.itcast.cnversion: v1.0.0group:default:group-name: defaultapi-rule: packageapi-rule-resources: # 指定扫描的包- com.hmall.pay.controller

业务代码迁移

  1. 迁移工具类

  1. 迁移mapper

  1. 迁移service

  1. 迁移枚举

  1. 迁移controller

解决PayOrderServiceImpl报错

@Service
@RequiredArgsConstructor
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {private final UserClient userClient;private final TradeClient tradeClient;... ...@Override@Transactionalpublic void tryPayOrderByBalance(PayOrderFormDTO payOrderFormDTO) {// 1.查询支付单PayOrder po = getById(payOrderFormDTO.getId());// 2.判断状态if(!PayStatus.WAIT_BUYER_PAY.equalsValue(po.getStatus())){// 订单不是未支付,状态异常throw new BizIllegalException("交易已支付或关闭!");}// 3.尝试扣减余额// 远程调用userClient.deductMoney(payOrderFormDTO.getPw(), po.getAmount());// 4.修改支付单状态boolean success = markPayOrderSuccess(payOrderFormDTO.getId(), LocalDateTime.now());if (!success) {throw new BizIllegalException("交易已支付或关闭!");}// 5.修改订单状态// 远程调用tradeClient.markOrderPaySuccess(po.getBizOrderNo());}... ...
}

在支付服务的PayController中添加一个接口方便测试

    @ApiOperation("查询支付单")@GetMappingpublic List<PayOrderVO> queryPayOrders(){return BeanUtils.copyList(payOrderService.list(), PayOrderVO.class);}

配置启动项, 启动服务

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词