• 快速搭建springcloud项目


    本文讲述springcloud项目从0开始的搭建过程以及其中需要注意的问题,不涉及具体的理论内容以及微服务项目中的五大问题和具体解决办法的插件或手段.五大问题的具体解决办法在本合集中分别进行逐一讲解

    本文模拟了电商项目中的订单微服务模块和商品微服务模块.商品微服务模块只实现一个功能:"通过商品id获取商品信息的功能,返回一个商品对象(product)",订单微服务模块也只实现一个功能:"生成订单",实现该功能需要调用商品微服务模块中的功能.

    一. 项目搭建详细步骤

    1.技术选型

    (1) maven:3.5.0+

    (2) MySQL:5.7+

    (3) Mybatis-plus

    (4) SpringCloud Alibaba

    (5)lombok

    2.需要准备的内容

    (1)数据库

    数据库名:springcloud

    商品表:shop_product

    1. DROP TABLE IF EXISTS `shop_product`;
    2. CREATE TABLE `shop_product` (
    3. `pid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品id',
    4. `pname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品名',
    5. `pprice` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
    6. `stock` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品库存',
    7. PRIMARY KEY (`pid`) USING BTREE
    8. ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

    订单表:shop_order

    1. DROP TABLE IF EXISTS `shop_order`;
    2. CREATE TABLE `shop_order` (
    3. `oid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单id',
    4. `uid` int(11) NULL DEFAULT NULL COMMENT '用户id',
    5. `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名',
    6. `pid` bigint(20) NULL DEFAULT NULL COMMENT '商品id',
    7. `pname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品名称',
    8. `pprice` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
    9. `number` int(11) NULL DEFAULT NULL COMMENT '购买数量',
    10. PRIMARY KEY (`oid`) USING BTREE
    11. ) ENGINE = InnoDB AUTO_INCREMENT = 239 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

    两张表里的数据随意发挥,不重要.

    (2)jdk1.8+idea开发工具

    3.模块设计

    springcloud-parent                父工程

    springcloud-common            公共模块(存放实体类,工具类等)

    springcloud-product              商品微服务(端口号:8080~8090)

    springcloud-order                  订单微服务(端口号:8090~8099)

    4.具体步骤:

    (1)创建一个springboot项目  起名为springcloud-parent

    ①删掉工程中给的src文件

    最终该父工程的结构图如下图所示:

    注:可能你创建的工程会有很多用不到的文件,比如.ieda,mvn等等.我们可以在设置中隐藏掉这些文件.具体方法可以参考一下连接:idea中隐藏不想看的文件和文件夹_Love_云宝儿的博客-CSDN博客_idea隐藏不想看到的文件1.file--settings2.editor--file types-- ignored files and folder --+3.输入你想要忽略的文件或文件夹名 可以用*通配符比如我想忽略mvn等等 可以这样写完事点击ok 或者点击apply+ok即可非常清爽https://blog.csdn.net/m0_38084879/article/details/123753512②编辑pom.xml文件

    • 降低springboot的版本到2.3.12
    • 设置父工程打包方式为pom
    • 设置具体版本号
    • 设置dependencyManagement管理jar的版本号
    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.3.12.RELEASEversion>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.csdngroupId>
    12. <artifactId>springcloudartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <packaging>pompackaging>
    15. <properties>
    16. <java.version>1.8java.version>
    17. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    18. <project.reporting.outputEncoding>UTF- 8project.reporting.outputEncoding>
    19. <spring-cloud.version>Hoxton.SR8spring-cloud.version>
    20. <spring-cloud-alibaba.version>2.2.3.RELEASEspring-cloud-alibaba.version>
    21. properties>
    22. <dependencyManagement>
    23. <dependencies>
    24. <dependency>
    25. <groupId>org.springframework.cloudgroupId>
    26. <artifactId>spring-cloud-dependenciesartifactId>
    27. <version>${spring-cloud.version}version>
    28. <type>pomtype>
    29. <scope>importscope>
    30. dependency>
    31. <dependency>
    32. <groupId>com.alibaba.cloudgroupId>
    33. <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    34. <version>${spring-cloud-alibaba.version}version>
    35. <type>pomtype>
    36. <scope>importscope>
    37. dependency>
    38. dependencies>
    39. dependencyManagement>
    40. <build>
    41. <plugins>
    42. <plugin>
    43. <groupId>org.springframework.bootgroupId>
    44. <artifactId>spring-boot-maven-pluginartifactId>
    45. plugin>
    46. plugins>
    47. build>
    48. project>

     注意,springcloud和alibabaversion以及springboot之间的版本号要对应.具体对应的关系见下表:

    (2) 建立公共模块微服务

    ①点击父工程文件右键新建一个Module.

     ②建立一个maven工程 起名为springcloud-common

     

    ③引入依赖(注意是springcloud-common下的pom.xml中的依赖,不是父工程中的pom.xml)

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.projectlombokgroupId>
    4. <artifactId>lombokartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>mysqlgroupId>
    8. <artifactId>mysql-connector-javaartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>com.baomidougroupId>
    12. <artifactId>mybatis-plus-boot-starterartifactId>
    13. <version>3.5.2version>
    14. dependency>
    15. dependencies>

    ④在src下建立实体类包和工具包,将两张表对应的实体类放入对应包下

    Product:

    1. package com.csdn.common.entity;
    2. import com.baomidou.mybatisplus.annotation.IdType;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import lombok.Data;
    6. @Data
    7. @TableName(value="shop_product")
    8. public class Product {
    9. @TableId(type= IdType.AUTO)
    10. private Integer pid;
    11. private String pname;//商品名称
    12. private Double pprice;//商品价格
    13. private Integer stock;//库存
    14. }

    Order:

    1. package com.csdn.common.entity;
    2. import com.baomidou.mybatisplus.annotation.IdType;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import lombok.Data;
    6. @Data
    7. @TableName("shop_order")
    8. public class Order {
    9. @TableId(type = IdType.AUTO)
    10. private Long oid; //订单id
    11. private Integer uid;//用户id
    12. private String username;//用户名
    13. private Integer pid;//商品id
    14. private String pname;//商品名称
    15. private Double pprice;//商品价格
    16. private Integer number;//购买数量
    17. }

     CommonResult:

    1. package com.csdn.common.utils;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. /**
    6. * @作者:刘壬杉
    7. * @创建时间 2022/8/19 19:05
    8. **/
    9. @Data
    10. @AllArgsConstructor
    11. @NoArgsConstructor
    12. public class CommonResult {
    13. private Integer code;
    14. private String msg;
    15. private Object data;
    16. }

     最终工程结构图如图所示:

    (3)建立商品微服务模块

    ①点击父工程文件右键新建一个Module.

    ②新建一个maven项目,命名为springcloud-product

    ③引入依赖,(这里需要引入公共模块服务,从而可以获取其中的jar和实体类工具类)

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.csdngroupId>
    4. <artifactId>springcloud-commonartifactId>
    5. <version>0.0.1-SNAPSHOTversion>
    6. dependency>
    7. <dependency>
    8. <groupId>org.springframework.bootgroupId>
    9. <artifactId>spring-boot-starter-webartifactId>
    10. dependency>
    11. dependencies>

    ④application.properties配置文件的内容

    1. #端口号
    2. server.port=8081
    3. #数据源
    4. spring.datasource.username=root
    5. spring.datasource.password=lrs998563
    6. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    7. spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai
    8. #sql日志
    9. mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    ⑤dao,service,controller业务的实现

    ProductMapper

    1. package com.lrs.product.dao;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.csdn.common.entity.Product;
    4. /**
    5. * @作者:刘壬杉
    6. * @创建时间 2022/8/19 19:17
    7. **/
    8. @Mapper
    9. public interface ProductMapper extends BaseMapper {
    10. }

    ProductServiceImpl:

    1. package com.lrs.product.service.impl;
    2. import com.csdn.common.entity.Product;
    3. import com.lrs.product.dao.ProductMapper;
    4. import com.lrs.product.service.ProductService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @作者:刘壬杉
    9. * @创建时间 2022/8/19 19:21
    10. **/
    11. @Service
    12. public class ProductServiceImpl implements ProductService {
    13. @Autowired
    14. private ProductMapper productMapper;
    15. @Override
    16. public Product getById(Integer id) {
    17. return productMapper.selectById(id);
    18. }
    19. }

    ProductController

    1. package com.lrs.product.controller;
    2. import com.csdn.common.entity.Product;
    3. import com.lrs.product.service.ProductService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. /**
    10. * @作者:刘壬杉
    11. * @创建时间 2022/8/19 19:19
    12. **/
    13. @RestController
    14. @RequestMapping("product")
    15. public class ProductController {
    16. @Autowired
    17. private ProductService productService;
    18. @GetMapping("getById/{id}")
    19. public Product getById(@PathVariable Integer id){
    20. return productService.getById(id);
    21. }
    22. }

    ⑥添加一个启动类

    ProductApp

    1. package com.lrs.product;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. /**
    5. * @作者:刘壬杉
    6. * @创建时间 2022/8/19 19:23
    7. **/
    8. @SpringBootApplication
    9. public class ProductApp {
    10. public static void main(String[] args) {
    11. SpringApplication.run(ProductApp.class,args);
    12. }
    13. }

    商品微服务模块的工程结构如下图所示:

    (4)建立订单微服务模块

    ①新建maven工程,命名为springcloud-order

    ②导入依赖(同商品微服务中的依赖)

    ③application.properties配置文件(同商品微服务模块,唯一区别:端口号改为8091)

    ④dao,service,controller业务的实现

    OrderMapper:

    1. package com.lrs.order.dao;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.csdn.common.entity.Order;
    4. import org.apache.ibatis.annotations.Mapper;
    5. /**
    6. * @作者:刘壬杉
    7. * @创建时间 2022/8/19 19:30
    8. **/
    9. @Mapper
    10. public interface OrderMapper extends BaseMapper {
    11. }

     OrderServiceImpl:

    这里的业务中需要调用商品微服务中的接口.这就需要用到我们的RestTemplate类,该类可以访问我们其他微服务中的接口内容并接收返回值.

    1. package com.lrs.order.service.impl;
    2. import com.csdn.common.entity.Order;
    3. import com.csdn.common.entity.Product;
    4. import com.csdn.common.utils.CommonResult;
    5. import com.lrs.order.dao.OrderMapper;
    6. import com.lrs.order.service.OrderService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import org.springframework.web.client.RestTemplate;
    10. /**
    11. * @作者:刘壬杉
    12. * @创建时间 2022/8/19 19:33
    13. **/
    14. @Service
    15. public class OrderServiceImpl implements OrderService {
    16. @Autowired
    17. private OrderMapper orderMapper;
    18. @Autowired
    19. private RestTemplate restTemplate;
    20. @Override
    21. public CommonResult saveOrder(Integer pid, Integer num) {
    22. Order order = new Order();
    23. order.setUid(1);
    24. order.setUsername("lrs");
    25. order.setNumber(num);
    26. order.setPid(pid);
    27. Product product = restTemplate.getForObject("http://localhost:8081/getById" + pid, Product.class);
    28. order.setPname(product.getPname());
    29. order.setPprice(product.getPprice());
    30. int insert = orderMapper.insert(order);
    31. if (insert==1){
    32. return new CommonResult(2000,"下单成功",null);
    33. }
    34. return new CommonResult(5000,"下单失败",null);
    35. }
    36. }

    OrderController:

    1. package com.lrs.order.controller;
    2. import com.csdn.common.utils.CommonResult;
    3. import com.lrs.order.service.OrderService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. /**
    10. * @作者:刘壬杉
    11. * @创建时间 2022/8/19 19:38
    12. **/
    13. @RestController
    14. @RequestMapping("order")
    15. public class OrderController {
    16. @Autowired
    17. private OrderService orderService;
    18. @GetMapping("saveOrder/{pid}/{num}")
    19. public CommonResult saveOrder(@PathVariable Integer pid,@PathVariable Integer num){
    20. return orderService.saveOrder(pid, num);
    21. }
    22. }

       RestTemplate类没有交由容器去管理,所以无法自动注入,所以我们需要在启动类或者加了@Configuration注解的类中配置该类交由容器管理.为了方便我直接写在主启动类中

    ⑤建立启动类

    OrderApp

    1. package com.lrs.order;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.web.client.RestTemplate;
    6. /**
    7. * @作者:刘壬杉
    8. * @创建时间 2022/8/19 19:42
    9. **/
    10. @SpringBootApplication
    11. public class OrderApp {
    12. public static void main(String[] args) {
    13. SpringApplication.run(OrderApp.class,args);
    14. }
    15. @Bean
    16. public RestTemplate restTemplate(){
    17. return new RestTemplate();
    18. }
    19. }

    订单微服务的工程目录如下图所示:

  • 相关阅读:
    [网络工程师]-应用层协议-WWW与HTTP
    前端websocket打造实时聊天室
    【编程题】【Scratch四级】2021.09 找出出现次数最多的数字
    Vue中强制手动强制刷新组件的正确方法
    apache 漏洞
    Webpack完整打包流程分析
    【MySQL】数据库基础介绍(使用Navicat和SQLyog演示创建和使用数据库的基本操作)
    Matlab-resample
    学习 XQuery:XML数据查询的关键
    白鹭群优化算法(ESOA)附matlab代码
  • 原文地址:https://blog.csdn.net/lrs998563/article/details/126429844