欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring Cloud【Finchley】实战-02订单微服务

发布时间:2025/3/21 javascript 62 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring Cloud【Finchley】实战-02订单微服务 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

  • Spring Cloud【Finchley】专栏
  • 概述
  • 数据模型-订单微服务
  • API
  • 业务逻辑分析
  • 搭建订单微服务
    • 依赖及配置文件
      • pom.xml
      • application.yml
    • 将微服务注册到注册中心
    • 实体类
      • Order
      • OrderDetail
    • Dao层
      • OrderRepository
      • OrderDetailRepository
      • 单元测试
    • Service层
      • Order 和OrderDetail 合并为一个DTO对象
      • OrderService接口和实现类
    • Controller层
    • 测试
  • 知识点总结
    • Gson库
      • 将Json转换为对象
        • 解析json数组
      • 将Java对象转换为Json
  • Github

Spring Cloud【Finchley】专栏

如果还没有系统的学过Spring Cloud ,先到我的专栏去逛逛吧

Spring Cloud 【Finchley】手札


概述

这里我们简单的说下业务相关的需求,重点是体会微服务这种理念是如何落地的。


数据模型-订单微服务

通常来讲,微服务都是分数据库的。这里我们新建个数据库给订单微服务 ,数据库实例名 o2o-order

-- ---------------------------- -- Table structure for order -- ---------------------------- -- 订单 create table `artisan_order` (`order_id` varchar(32) not null,`buyer_name` varchar(32) not null comment '买家名字',`buyer_phone` varchar(32) not null comment '买家电话',`buyer_address` varchar(128) not null comment '买家地址',`buyer_openid` varchar(64) not null comment '买家微信openid',`order_amount` decimal(8,2) not null comment '订单总金额',`order_status` tinyint(3) not null default '0' comment '订单状态, 默认为新下单',`pay_status` tinyint(3) not null default '0' comment '支付状态, 默认未支付',`create_time` timestamp not null default current_timestamp comment '创建时间',`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',primary key (`order_id`),key `idx_buyer_openid` (`buyer_openid`) );-- ---------------------------- -- Table structure for order_detail -- ---------------------------- -- 订单详情 create table `order_detail` (`detail_id` varchar(32) not null,`order_id` varchar(32) not null,`product_id` varchar(32) not null,`product_name` varchar(64) not null comment '商品名称',`product_price` decimal(8,2) not null comment '当前价格,单位分',`product_quantity` int not null comment '数量',`product_icon` varchar(512) comment '小图',`create_time` timestamp not null default current_timestamp comment '创建时间',`update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间',primary key (`detail_id`),key `idx_order_id` (`order_id`) );

订单与订单详情是一对多的关系,一个订单中可能包含多个订单详情,比如我下一个订单,这个订单中买了1杯奶茶、2杯可乐等。

order_detail中不仅设计了product_id,同时也冗余了 product_name product_price product_icon等,主要是考虑到有些促销活动这些字段会经常更改这些因素。


API

请求:

POST方式 /order/create

内容:

"name": "xxx","phone": "xxxx","address": "xxxx","openid": "xxxx", //用户的微信openid"items": [{"productId": "xxxxxx","productQuantity": 2 //购买数量}]

后端尽量少依赖前端传递的数据,为了安全起见,产品相关的数据,只传递了一个productId和productQuantity,而没有将价格、描述等等一并传递,不传递就不会被篡改,也减少了交互数据的大小。

返回:

{"code": 0,"msg": "成功","data": {"orderId": "123456" } }

业务逻辑分析

  • 校验前台入参
  • 查询商品信息(调用商品微服务
  • 计算订单总价
  • 扣减库存(调用商品微服务
  • 订单入库

  • 搭建订单微服务

    依赖及配置文件

    pom.xml

    <?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.artisan</groupId><artifactId>artisan_order</artifactId><version>0.0.1-SNAPSHOT</version><name>artisan_order</name><description>Order</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

    application.yml

    这里我们连接到 order微服务的数据库。 我这里本地环境,就新建了个数据库实例。

    server:port: 8081spring:application:name: artisan-order# datasourcedatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/o2o-order?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=falseusername: rootpassword: root#jpajpa:show-sql: true # Eureka eureka:client:service-url:defaultZone: http://localhost:8761/eureka/

    将微服务注册到注册中心

    application.yml中配置了Eureka的信息后,我们在启动类增加@EnableEurekaClient即可

    启动注册中心微服务,启动该服务

    访问 http://localhost:8761/

    注册成功


    实体类

    Order

    package com.artisan.order.domain;import lombok.Data;import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.math.BigDecimal; import java.util.Date;@Data // 必不可少 @Entity @Table(name = "artisan_order") public class Order {/*** 订单id.*/@Idprivate String orderId;/*** 买家名字.*/private String buyerName;/*** 买家手机号.*/private String buyerPhone;/*** 买家地址.*/private String buyerAddress;/*** 买家微信Openid.*/private String buyerOpenid;/*** 订单总金额.*/private BigDecimal orderAmount;/*** 订单状态, 默认为0新下单.*/private Integer orderStatus;/*** 支付状态, 默认为0未支付.*/private Integer payStatus;/*** 创建时间.*/private Date createTime;/*** 更新时间.*/private Date updateTime; }

    OrderDetail

    package com.artisan.order.domain;import lombok.Data;import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.math.BigDecimal;@Data// 必不可少 @Entity // 如果实体类是OrderDetail,表名是order_detail,则这个注解可省略 @Table(name = "order_detail") public class OrderDetail {// 必不可少@Idprivate String detailId;/*** 订单id.*/private String orderId;/*** 商品id.*/private String productId;/*** 商品名称.*/private String productName;/*** 商品单价.*/private BigDecimal productPrice;/*** 商品数量.*/private Integer productQuantity;/*** 商品小图.*/private String productIcon; }

    Dao层

    创建订单无非就是往这两个表里写入数据。直接利用jpa提供的save方法即可。

    OrderRepository

    空实现 ,利用jpa本身提供的save方法

    package com.artisan.order.repository;import com.artisan.order.domain.Order; import org.springframework.data.jpa.repository.JpaRepository;// JpaRepository<Order,String> 第一个是要操作的对象,第二个是实体类中标注的@Id的字段的类型 (主键类型) public interface OrderRepository extends JpaRepository<Order,String> {}

    OrderDetailRepository

    空实现 ,利用jpa本身提供的save方法

    package com.artisan.order.repository;import com.artisan.order.domain.OrderDetail; import org.springframework.data.jpa.repository.JpaRepository;public interface OrderDetailRepository extends JpaRepository<OrderDetail ,String> { }

    单元测试

    OrderRepositoryTest

    package com.artisan.order.repository;import com.artisan.order.ArtisanOrderApplicationTests; import com.artisan.order.domain.Order; import com.artisan.order.enums.OrderStatusEnum; import com.artisan.order.enums.PayStatusEnum; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.math.BigDecimal;@Component public class OrderRepositoryTest extends ArtisanOrderApplicationTests {@Autowiredprivate OrderRepository orderRepository;@Testpublic void testSave(){Order order = new Order();order.setOrderId("1222");order.setBuyerName("artisan");order.setBuyerPhone("123445664");order.setBuyerAddress("Artisan Tech");order.setBuyerOpenid("11112233");order.setOrderAmount(new BigDecimal(3.9));order.setOrderStatus(OrderStatusEnum.NEW.getCode());order.setPayStatus(PayStatusEnum.WAIT.getCode());Order result = orderRepository.save(order);Assert.assertNotNull(result);} }

    数据库记录


    package com.artisan.order.repository;import com.artisan.order.ArtisanOrderApplicationTests; import com.artisan.order.domain.OrderDetail; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.math.BigDecimal;@Component public class OrderDetailRepositoryTest extends ArtisanOrderApplicationTests {@Autowiredprivate OrderDetailRepository orderDetailRepository;@Testpublic void testSave() {OrderDetail orderDetail = new OrderDetail();orderDetail.setDetailId("1111");orderDetail.setOrderId("111111");orderDetail.setProductIcon("http://xxx.com");orderDetail.setProductId("22222");orderDetail.setProductName("拿铁");orderDetail.setProductPrice(new BigDecimal(0.01));orderDetail.setProductQuantity(2);OrderDetail result = orderDetailRepository.save(orderDetail);Assert.assertTrue(result != null);} }

    单元测试 通过。


    Service层

    分析下,我们要往artisan_order 和 order_detail中写入数据,肯定要传入Order和OrderDetail实体类,类似于 createOrder(Order order , OrderDetail orderDetail) ,根据业务规则,一个Order中可能有多个OrderDetail, 所以入参OrderDetail 必须是个集合,并且返回结果也不好定义。 因此我们将这俩合并一下,封装成DTO来使用,作为入参和返回结果。


    Order 和OrderDetail 合并为一个DTO对象

    下图中类上少儿个注解 @Data,注意补上


    OrderService接口和实现类

    package com.artisan.order.service;import com.artisan.order.dto.OrderDTO;public interface OrderService {OrderDTO createOrder(OrderDTO orderDTO); }

    我们来分析下前端的请求

    "name": "xxx","phone": "xxxx","address": "xxxx","openid": "xxxx", //用户的微信openid"items": [{"productId": "xxxxxx","productQuantity": 2 //购买数量}]

    结合业务逻辑

    • 校验前台入参

    • 查询商品信息(调用商品微服务)

    • 计算订单总价

    • 扣减库存(调用商品微服务)

    • 订单入库

    逐一分析下目前的可行性

    • 参数校验,我们放在Controller层校验,所以Service层这里不写

    • 调用微服务的,我们目前还不具备,没法做

    • 计算订单总价,前台入参仅仅传了ProductId, 而Product的数据需要调用商品微服务,目前没法做

    • 订单入库,其实分两部分,第一个是artisan_order表,第二个是Order_detail表。 order_detail表包含了Product的内容,目前也是做不了。

    综合分析,目前在Service层能做的仅仅是 入库artisan_order表

    那在实现类里,我们先实现部分吧

    package com.artisan.order.service.impl;import com.artisan.order.domain.Order; import com.artisan.order.dto.OrderDTO; import com.artisan.order.enums.OrderStatusEnum; import com.artisan.order.enums.PayStatusEnum; import com.artisan.order.repository.OrderDetailRepository; import com.artisan.order.repository.OrderRepository; import com.artisan.order.service.OrderService; import com.artisan.order.utils.KeyUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.math.BigDecimal;@Service public class OrderServiceImpl implements OrderService {@AutowiredOrderRepository orderRepository;@AutowiredOrderDetailRepository orderDetailRepository;@Overridepublic OrderDTO createOrder(OrderDTO orderDTO) {// TODO 查询商品信息(调用商品微服务)// TODO 计算订单总价// TODO 扣减库存(调用商品微服务)//订单入库Order order = new Order();orderDTO.setOrderId(KeyUtil.genUniqueKey());// 复制属性BeanUtils.copyProperties(orderDTO, order);// 设置其他属性order.setOrderAmount(new BigDecimal("100")); // TODO 后需要修改order.setOrderStatus(OrderStatusEnum.NEW.getCode());order.setPayStatus(PayStatusEnum.WAIT.getCode());orderRepository.save(order);return orderDTO;} }

    Controller层

    这里仅列出关键代码,其余请参考github

    package com.artisan.order.form;import lombok.Data; import org.hibernate.validator.constraints.NotEmpty;import java.util.List;@Data public class OrderForm {/*** 对应** {* "name": "xxx",* "phone": "xxxx",* "address": "xxxx",* "openid": "xxxx", //用户的微信openid* "items": [* {* "productId": "xxxxxx",* "productQuantity": 2 //购买数量* }* ]* }****//*** 买家姓名*/@NotEmpty(message = "姓名必填")private String name;/*** 买家手机号*/@NotEmpty(message = "手机号必填")private String phone;/*** 买家地址*/@NotEmpty(message = "地址必填")private String address;/*** 买家微信openid*/@NotEmpty(message = "openid必填")private String openid;/*** 购物车*/@NotEmpty(message = "购物车不能为空")private String items;} package com.artisan.order.converter;import com.artisan.order.domain.OrderDetail; import com.artisan.order.dto.OrderDTO; import com.artisan.order.enums.ResultEnum; import com.artisan.order.exception.OrderException; import com.artisan.order.form.OrderForm; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import lombok.extern.slf4j.Slf4j;import java.util.ArrayList; import java.util.List;@Slf4j public class OrderForm2OrderDTOConverter {public static OrderDTO convert(OrderForm orderForm) {Gson gson = new Gson();OrderDTO orderDTO = new OrderDTO();orderDTO.setBuyerName(orderForm.getName());orderDTO.setBuyerPhone(orderForm.getPhone());orderDTO.setBuyerAddress(orderForm.getAddress());orderDTO.setBuyerOpenid(orderForm.getOpenid());List<OrderDetail> orderDetailList = new ArrayList<>();try {// fromJson 从Json相关对象到Java实体的方法 ,转换成列表类型orderDetailList = gson.fromJson(orderForm.getItems(),new TypeToken<List<OrderDetail>>() {}.getType());}catch(Exception e){log.error("【json转换】错误, string={}", orderForm.getItems());throw new OrderException(ResultEnum.PARAM_ERROR);}orderDTO.setOrderDetailList(orderDetailList);return orderDTO;} } package com.artisan.order.controller;import com.artisan.order.converter.OrderForm2OrderDTOConverter; import com.artisan.order.dto.OrderDTO; import com.artisan.order.enums.ResultEnum; import com.artisan.order.exception.OrderException; import com.artisan.order.form.OrderForm; import com.artisan.order.service.OrderService; import com.artisan.order.vo.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid; import java.util.HashMap; import java.util.Map;@RestController @RequestMapping("/order") @Slf4j public class OrderController {@AutowiredOrderService orderService;@PostMapping("/create")public Result create(@Valid OrderForm orderForm,BindingResult bindingResult) {if (bindingResult.hasErrors()){log.error("【Create Order】参数不正确, orderForm={}", orderForm);throw new OrderException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());}// orderForm -> orderDTOOrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {log.error("【Create Order】购物车信息为空");throw new OrderException(ResultEnum.CART_EMPTY);}OrderDTO result = orderService.createOrder(orderDTO);Map<String, String> map = new HashMap<>();map.put("orderId", result.getOrderId());return Result.success(map);}}

    测试

    使用PostMan

    查看数据库

    OK

    说明下: x-www-form-urlencoded 这种格式 就是application/x-www-from-urlencoded,会将表单内的数据转换为键值对,比如:name=artisan&phone=123


    知识点总结

    Gson库

    谷歌提供的 JSON – Java Object 相互转换的 Java序列化/反序列化库。

    将Json转换为对象

    解析json数组

    orderDetailList = gson.fromJson(orderForm.getItems(),new TypeToken<List<OrderDetail>>() {}.getType());

    更多示例参考


    将Java对象转换为Json


    Github

    https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan-product

    总结

    以上是生活随笔为你收集整理的Spring Cloud【Finchley】实战-02订单微服务的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。