• 【SpringCloud】2-SpringCloud微服务架构实例-订单支付


    1. 搭建父工程

    建立Maven工程,在pom.xml中引入依赖:

        <repositories>
            <repository>
                <id>springid>
                <url>https://maven.aliyun.com/repository/springurl>
                <releases>
                    <enabled>trueenabled>
                releases>
                <snapshots>
                    <enabled>trueenabled>
                snapshots>
            repository>
        repositories>
    
    
            
            <dependencies>
                
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>2.3.2.RELEASEversion>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>Hoxton.RELEASEversion>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>com.alibaba.cloudgroupId>
                    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                    <version>2.2.0.RELEASEversion>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>mysqlgroupId>
                    <artifactId>mysql-connector-javaartifactId>
                    <version>8.0.28version>
                dependency>
                <dependency>
                    <groupId>com.alibabagroupId>
                    <artifactId>druidartifactId>
                    <version>1.1.16version>
                dependency>
                <dependency>
                    <groupId>org.mybatis.spring.bootgroupId>
                    <artifactId>mybatis-spring-boot-starterartifactId>
                    <version>1.3.0version>
                dependency>
                <dependency>
                    <groupId>junitgroupId>
                    <artifactId>junitartifactId>
                    <version>4.12version>
                    <scope>testscope>
                dependency>
                <dependency>
                    <groupId>log4jgroupId>
                    <artifactId>log4jartifactId>
                    <version>1.2.17version>
                dependency>
                <dependency>
                    <groupId>org.projectlombokgroupId>
                    <artifactId>lombokartifactId>
                    <version>1.18.24version>
                dependency>
            dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <fork>truefork>
                        <addResources>trueaddResources>
                    configuration>
                plugin>
            plugins>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    如果采用以下方式进行依赖管理,一定要先注释掉,等Maven下载完jar包之后再加上,不然会爆红。

        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>1.8maven.compiler.source>
            <maven.compiler.target>1.8maven.compiler.target>
            <junit.version>4.12junit.version>
            <log4j.version>1.2.17log4j.version>
            <lombok.version>1.16.18lombok.version>
            <mysql.version>8.0.18mysql.version>
            <druid.verison>1.1.16druid.verison>
            <mybatis.spring.boot.verison>1.3.0mybatis.spring.boot.verison>
        properties>
    
    <dependencyManagement>
       dependencies>
    	   	<dependency>
    	        ...
    	    dependency>
       dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2. 支付模块构建

    约定 > 配置 > 编码

    **微服务模块 **

    1. 建module
    2. 改pom
    3. 写yml
    4. 主启动
    5. 业务类

    2.1 搭建子模块

    文件架构
    在这里插入图片描述

    1. 配置子模块pom文件。
    
    <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">
        <parent>
            <artifactId>cloud2020artifactId>
            <groupId>com.atguigu.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>cloud-provider-payment8001artifactId>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
                <version>2.6.3version>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    1. resource包下新建application.yml文件
    server:
      port: 8001
    spring:
      application:
        name: cloud-payment-service
      datasource:
        # 当前数据源操作类型
        type: com.alibaba.druid.pool.DruidDataSource
        # mysql驱动类
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://你的服务器ip:3306/你的数据库名称?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
        username: 你的数据库用户名
        password: 你的数据库密码
    mybatis:
      mapper-locations: classpath*:mapper/*.xml
      type-aliases-package: com.moke.springcloud.entities
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 建立实体类、DAO、Mapper

    实体类:Payment

    /**
     * @author Chen Ruoyi
     * @date 2022/7/21 15:33
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
        private Long id;
        private String serial;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    与前端的交互类:CommonResult

    /**
     * @author Chen Ruoyi
     * @date 2022/7/21 15:34
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult <T>{
        private Integer code;
        private String message;
        private T data;
    
        public CommonResult(Integer code, String message){
            this(code,message,null);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    DAO

    /**
     * @author Chen Ruoyi
     * @date 2022/7/21 15:36
     */
    @Mapper
    public interface PaymentDao {
        public int create(Payment payment);
    
        public  Payment getPaymentById(@Param("id") Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在resources下建立mapper文件夹,建立paymentMapper.xml文件

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.cherry.springcloud.dao.PaymentDao">
        <resultMap id="BaseResultMap" type="com.cherry.springcloud.entities.Payment">
            <id column="id" property="id" jdbcType="BIGINT">id>
            <id column="serial" property="serial" jdbcType="VARCHAR">id>
        resultMap>
    
        <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
    insert into payment(serial) values(#{serial});
        insert>
    
        <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
            select * from payment where id = #{id};
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 建立Service -> ServiceImpl -> Controller

    Service

    /**
     * @author Chen Ruoyi
     * @date 2022/7/21 15:49
     */
    public interface PaymentService {
        public int create(Payment payment);
    
        public  Payment getPaymentById(@Param("id") Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ServiceImpl

    /**
     * @author Chen Ruoyi
     * @date 2022/7/21 15:50
     */
    @Service
    public class PaymentServiceImpl implements PaymentService{
        @Resource
        private PaymentDao paymentDao;
    
    
        @Override
        public int create(Payment payment) {
            return paymentDao.create(payment);
        }
    
        @Override
        public Payment getPaymentById(Long id) {
            return paymentDao.getPaymentById(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Controller

    /**
     * @author Chen Ruoyi
     * @date 2022/7/21 15:53
     */
    @RestController
    @Slf4j
    public class PaymentController {
        @Resource
        private PaymentService paymentService;
    
        @PostMapping(value = "/payment/create")
        public CommonResult create(Payment payment) {
            int result = paymentService.create(payment);
            log.info("****插入结果:", result);
            if (result > 0) {
                return new CommonResult(200, "插入数据库成功", result);
            } else {
                return new CommonResult(444, "插入数据库失败", result);
            }
        }
    
        @GetMapping(value = "/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") Long id) {
            Payment payment = paymentService.getPaymentById(id);
            log.info("****插入结果:", payment);
    
            if (payment != null) {
                return new CommonResult(200, "查询成功", payment);
            } else {
                return new CommonResult(444, "没有对应记录, 查询id:" + id, null);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2.2 测试结果

    (此处使用ApiPost6进行接口测试
    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    科目三 临时停车 与 红绿灯 路口 LTS
    Scientific Reports|比较转录组分析揭示了杀菌剂氰烯菌酯对尖孢镰刀菌的抗性调控机制和杀菌活性
    笔记本电脑重装系统win10教程-u盘装系统教程
    【数据结构】图的广度优先遍历
    年前端react面试打怪升级之路
    URLSearchParams快速解析URL查询参数
    J2EE项目部署与发布(Windows版本)->会议OA单体项目Windows部署,spa前后端分离项目Windows部署
    ZCMU--1367: Data Structure
    跑步需要哪些运动装备?跑步爱好者者的装备推荐
    APS高级排产软件在快消品行业的应用
  • 原文地址:https://blog.csdn.net/CherryChenieth/article/details/125133039