SpringCloud微服务项目搭建入门

  • Post author:
  • Post category:其他




介绍

本篇将以springboot+mybatis-plus搭建一个简单的微服务项目,包括几个简单表用户表,商品表,订单表,库存表。以resttemplate进行模块之间的相互调用。



建表sql和数据



表脚本

-- test.`user` definition

CREATE TABLE `user` (
  `user_id` int NOT NULL AUTO_INCREMENT COMMENT '用户Id',
  `user_name` varchar(100) NOT NULL COMMENT '用户名字',
  `user_age` int DEFAULT NULL COMMENT '用户年龄',
  `user_phone` varchar(11) DEFAULT NULL COMMENT '联系电话',
  `user_mail` varchar(100) DEFAULT NULL COMMENT '邮箱',
  `user_address` varchar(100) DEFAULT NULL COMMENT '用户地址',
  `user_status` varchar(1) DEFAULT NULL COMMENT '用户状态',
  `update_by` varchar(100) DEFAULT NULL COMMENT '更新人',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(100) DEFAULT NULL COMMENT '创建人',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- test.product definition

CREATE TABLE `product` (
  `product_id` int NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `product_name` varchar(100) DEFAULT NULL COMMENT '商品名称',
  `product_type` varchar(10) DEFAULT NULL,
  `product_price` decimal(10,0) DEFAULT NULL COMMENT '商品价格',
  `create_by` varchar(100) DEFAULT NULL COMMENT '创建人',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  `update_by` varchar(100) DEFAULT NULL COMMENT '更新人',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`product_id`),
  KEY `product_product_type_IDX` (`product_type`,`product_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='商品表';
-- test.stock definition

CREATE TABLE `stock` (
  `stock_id` int NOT NULL AUTO_INCREMENT COMMENT '库存主键',
  `product_id` int NOT NULL COMMENT '商品ID',
  `stock_num` int DEFAULT NULL COMMENT '库存数量',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(100) DEFAULT NULL COMMENT '创建人',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  `update_by` varchar(100) DEFAULT NULL COMMENT '更新人',
  PRIMARY KEY (`stock_id`),
  KEY `stock_product_id_IDX` (`product_id`) USING BTREE,
  CONSTRAINT `stock_FK` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='库存表';
-- test.`order` definition

CREATE TABLE `order` (
  `order_id` int NOT NULL AUTO_INCREMENT COMMENT '订单ID',
  `order_no` int DEFAULT NULL COMMENT '订单号',
  `product_id` int DEFAULT NULL COMMENT '产品id',
  `user_id` int DEFAULT NULL COMMENT '用户ID',
  `order_num` int DEFAULT NULL COMMENT '订单产品数量',
  `order_amt` decimal(10,2) DEFAULT NULL COMMENT '订单金额',
  `order_status` varchar(10) DEFAULT NULL COMMENT '订单状态',
  `pay_status` varchar(10) DEFAULT NULL COMMENT '支付状态',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_user` varchar(10) DEFAULT NULL COMMENT '创建人',
  `update_user` varchar(10) DEFAULT NULL COMMENT '更新人',
  PRIMARY KEY (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='订单表';



表数据

INSERT INTO test.`user`
(user_id, user_name, user_age, user_phone, user_mail, user_address, user_status, update_by, create_time, create_by, update_time)
VALUES(1, 'elite', 24, '18388888888', '18388888888@163.com', 'xx省xx市xx区', '1', NULL, '2022-09-06 13:17:24', NULL, '2022-09-06 13:17:30');
INSERT INTO test.`user`
(user_id, user_name, user_age, user_phone, user_mail, user_address, user_status, update_by, create_time, create_by, update_time)
VALUES(3, 'test', 22, '18366666666', '18366666666@163.com', 'xx省xx市xx区', '1', NULL, '2022-09-06 15:10:59', NULL, '2022-09-06 15:10:59');
INSERT INTO test.product
(product_id, product_name, product_type, product_price, create_by, update_time, update_by, create_time)
VALUES(1, '华为', '手机', 5999, NULL, '2022-09-06 15:12:29', NULL, '2022-09-06 15:12:29');
INSERT INTO test.product
(product_id, product_name, product_type, product_price, create_by, update_time, update_by, create_time)
VALUES(2, '小米', '手机', 4999, NULL, '2022-09-06 15:12:29', NULL, '2022-09-06 15:12:29');
INSERT INTO test.product
(product_id, product_name, product_type, product_price, create_by, update_time, update_by, create_time)
VALUES(3, 'OPPO', '手机', 3999, NULL, '2022-09-06 15:12:29', NULL, '2022-09-06 15:12:29');
INSERT INTO test.product
(product_id, product_name, product_type, product_price, create_by, update_time, update_by, create_time)
VALUES(4, '联想拯救者', '电脑', 8999, NULL, '2022-09-06 15:13:43', NULL, '2022-09-06 15:13:43');
INSERT INTO test.product
(product_id, product_name, product_type, product_price, create_by, update_time, update_by, create_time)
VALUES(5, 'Dell', '电脑', 6999, NULL, '2022-09-06 15:13:43', NULL, '2022-09-06 15:13:43');
INSERT INTO test.product
(product_id, product_name, product_type, product_price, create_by, update_time, update_by, create_time)
VALUES(6, '华为mate', '电脑', 7999, NULL, '2022-09-06 15:13:43', NULL, '2022-09-06 15:13:43');
INSERT INTO test.stock
(stock_id, product_id, stock_num, create_time, create_by, update_time, update_by)
VALUES(1, 1, 100, '2022-09-06 15:19:18', 'elite', '2022-09-06 15:19:18', 'elite');
INSERT INTO test.stock
(stock_id, product_id, stock_num, create_time, create_by, update_time, update_by)
VALUES(6, 6, 100, '2022-09-06 15:19:18', 'elite', '2022-09-06 15:19:18', 'elite');
INSERT INTO test.stock
(stock_id, product_id, stock_num, create_time, create_by, update_time, update_by)
VALUES(5, 5, 100, '2022-09-06 15:19:18', 'elite', '2022-09-06 15:19:18', 'elite');
INSERT INTO test.stock
(stock_id, product_id, stock_num, create_time, create_by, update_time, update_by)
VALUES(4, 4, 100, '2022-09-06 15:19:18', 'elite', '2022-09-06 15:19:18', 'elite');
INSERT INTO test.stock
(stock_id, product_id, stock_num, create_time, create_by, update_time, update_by)
VALUES(3, 3, 100, '2022-09-06 15:19:18', 'elite', '2022-09-06 15:19:18', 'elite');
INSERT INTO test.stock
(stock_id, product_id, stock_num, create_time, create_by, update_time, update_by)
VALUES(2, 2, 100, '2022-09-06 15:19:18', 'elite', '2022-09-06 15:19:18', 'elite');
INSERT INTO test.`order`
(order_id, order_no, product_id, user_id, order_num, order_amt, order_status, pay_status, create_time, update_time, create_user, update_user)
VALUES(1, 1, 1, 1, 2, 0.00, '已发货', '支付完成', '2022-08-21 08:32:40', '2022-08-21 10:48:44', 'user1', 'user2');
INSERT INTO test.`order`
(order_id, order_no, product_id, user_id, order_num, order_amt, order_status, pay_status, create_time, update_time, create_user, update_user)
VALUES(3, 2, 2, 1, 2, 20.00, '取消下单', '未支付', '2022-08-21 11:20:29', '2022-08-21 11:20:29', 'annotation', 'annotation');
INSERT INTO test.`order`
(order_id, order_no, product_id, user_id, order_num, order_amt, order_status, pay_status, create_time, update_time, create_user, update_user)
VALUES(4, 4, 1, 1, 2, 20.00, '下单', '支付', '2022-08-21 11:25:09', '2022-08-21 11:25:09', 'annotation', 'annotation');
INSERT INTO test.`order`
(order_id, order_no, product_id, user_id, order_num, order_amt, order_status, pay_status, create_time, update_time, create_user, update_user)
VALUES(6, 3, 3, 3, 2, 30.00, '发货', '支付', '2022-08-22 00:38:30', '2022-08-22 00:38:30', 'plus', 'plus');
INSERT INTO test.`order`
(order_id, order_no, product_id, user_id, order_num, order_amt, order_status, pay_status, create_time, update_time, create_user, update_user)
VALUES(7, 3, 3, 3, 2, 30.00, '下单', '未支付', '2022-08-22 00:47:29', '2022-08-22 00:47:29', 'plus', 'plus');



项目结构

项目结构



common模块

common存放公共的实体,这里只贴一个实体,其他的后续会上传。


/**
 * 商品表
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("`product`")
public class Product {

    /**
     * 商品ID
     */
    @TableId(value = "product_id", type = IdType.AUTO)
    private Integer productId;

    /**
     * 商品名
     */
    @TableField(value = "product_name")
    private String productName;

    /**
     * 商品分类
     */
    @TableField(value = "product_type")
    private String productType;

    /**
     * 商品分类
     */
    @TableField(value = "product_price")
    private BigDecimal productPrice;

    //创建人
    @TableField(value = "create_by")
    private String createBy;
    //创建时间
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    private String createTime;
    //更新人
    @TableField(value = "update_by")
    private String updateBy;
    //更新时间
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
    private String updateTime;
}



user服务模块

mybtias的部分就不多说了,不清楚可以看我前边的博客。

Springboot整合MybatisPlus


controller代码:

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author elite
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/springcloud/user")
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 获取用户列表
     * @return
     */
    @GetMapping("/getUserList")
    public List<User> getUserList(){
        return userService.list();
    }
    /**
     * 获取用户
     * @param user_id
     * @return
     */

    @GetMapping("/getUserByUseId/{user_id}")
    public User getUserByUseId(@PathVariable("user_id")Integer  user_id){
        return userService.getById(user_id);
    }
}



商品服务模块

同理,这里只贴controller层代码.

/**
 * /springcloud/product/getProductList
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author elite
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/springcloud/product")
public class ProductController {

    @Autowired
    IProductService productService;

    /**
     * 获取商品列表
     * @return
     */
    @GetMapping("/getProductList")
    public List<Product> getProductList(){
        return productService.list();
    }

    /**
     * 获取商品
     * @param product_id
     * @return
     */
    @GetMapping("/getProductById/{product_id}")
    public Product getProductById(@PathVariable("product_id")Integer product_id){
        return productService.getById(product_id);
    }
}



库存模块


/**
 * <p>
 * 库存表 前端控制器
 * </p>
 *
 * @author elite
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/springcloud/stock")
public class StockController {

    @Autowired
    IStockService stockService;

    /**
     * 获取库存列表
     * @return
     */
    @GetMapping("/getStockList")
    public List<Stock> getStockList(){
        return stockService.list();
    }

    /**
     * 获取库存ID
     * @param stock_id
     * @return
     */
    @GetMapping("/getStockById/{stock_id}")
    public Stock getStockById(@PathVariable("stock_id")Integer stock_id){
        return stockService.getById(stock_id);
    }
}



订单模块

订单模块,获取订单信息,获取对应的用户信息与商品信息。以resttemplate调用用户模块服务以及商品服务模块。

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author elite
 * @since 2022-09-06
 */
@RestController
@RequestMapping("/springcloud/order")
public class OrderController {
    @Autowired
    IOrderService orderService;

    @Autowired
    RestTemplate restTemplate;
    /**
     * 获取订单列表
     * @return
     */
    @GetMapping("/getOrderList")
    public List<Order> getOrderList(){
        return orderService.list();
    }
    /**
     * 获取订单列表
     * @return
     */
    @GetMapping("/getOrderById/{order_id}")
    public HashMap<String,Object> getOrderById(@PathVariable("order_id")Integer order_id){
        HashMap<String,Object> res = new HashMap<>();
        //获取订单
        Order order = orderService.getById(order_id);
        res.put("order",order);
        //获取用户
        User user = restTemplate.getForObject("http://localhost:8081/springcloud/user/getUserByUseId/"+order.getUserId(), User.class);
        res.put("user",user);
        //获取商品信息
        Product product = restTemplate.getForObject("http://localhost:8082/springcloud/product/getProductById/"+order.getProductId(), Product.class);
        res.put("product",product);
        return res;
    }
}



测试

完整的订单信息,包括订单信息,用户信息,商品信息。

测试



版权声明:本文为qq_37400096原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。