SpringCloud微服务项目搭建入门

简介: SpringCloud微服务项目搭建入门

文章目录

介绍

本篇将以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');

项目结构

38df92298a4e4cb0a33c4df1a722e018.png

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;

   }

}

测试

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

dd9a6750b27344848545163bb553249d.png

相关文章
|
9月前
|
算法 Java 微服务
【SpringCloud(1)】初识微服务架构:创建一个简单的微服务;java与Spring与微服务;初入RestTemplate
微服务架构是What?? 微服务架构是一种架构模式,它提出将单一应用程序划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。 每个服务允许在其独立的进程中,服务于服务间采用轻量级的通信机制互相协作(通常是Http协议的RESTful API或RPC协议)。 每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据上下文,选择合适的语言、工具对其进行构建
718 126
|
9月前
|
负载均衡 算法 Java
【SpringCloud(2)】微服务注册中心:Eureka、Zookeeper;CAP分析;服务注册与服务发现;单机/集群部署Eureka;连接注册中心
1. 什么是服务治理? SpringCloud封装了Netfix开发的Eureka模块来实现服务治理 在传统pc的远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册
482 0
|
9月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
10月前
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
589 1
|
10月前
|
jenkins Java 持续交付
使用 Jenkins 和 Spring Cloud 自动化微服务部署
随着单体应用逐渐被微服务架构取代,企业对快速发布、可扩展性和高可用性的需求日益增长。Jenkins 作为领先的持续集成与部署工具,结合 Spring Cloud 提供的云原生解决方案,能够有效简化微服务的开发、测试与部署流程。本文介绍了如何通过 Jenkins 实现微服务的自动化构建与部署,并结合 Spring Cloud 的配置管理、服务发现等功能,打造高效、稳定的微服务交付流程。
1102 0
使用 Jenkins 和 Spring Cloud 自动化微服务部署
|
设计模式 Java API
微服务架构演变与架构设计深度解析
【11月更文挑战第14天】在当今的IT行业中,微服务架构已经成为构建大型、复杂系统的重要范式。本文将从微服务架构的背景、业务场景、功能点、底层原理、实战、设计模式等多个方面进行深度解析,并结合京东电商的案例,探讨微服务架构在实际应用中的实施与效果。
932 6
|
设计模式 Java API
微服务架构演变与架构设计深度解析
【11月更文挑战第14天】在当今的IT行业中,微服务架构已经成为构建大型、复杂系统的重要范式。本文将从微服务架构的背景、业务场景、功能点、底层原理、实战、设计模式等多个方面进行深度解析,并结合京东电商的案例,探讨微服务架构在实际应用中的实施与效果。
491 1
|
安全 应用服务中间件 API
微服务分布式系统架构之zookeeper与dubbo-2
微服务分布式系统架构之zookeeper与dubbo-2
|
负载均衡 Java 应用服务中间件
微服务分布式系统架构之zookeeper与dubbor-1
微服务分布式系统架构之zookeeper与dubbor-1
|
Java 开发者 微服务
从单体到微服务:如何借助 Spring Cloud 实现架构转型
**Spring Cloud** 是一套基于 Spring 框架的**微服务架构解决方案**,它提供了一系列的工具和组件,帮助开发者快速构建分布式系统,尤其是微服务架构。
2713 70
从单体到微服务:如何借助 Spring Cloud 实现架构转型

热门文章

最新文章