2、支付模块构建和热部署

系列文章目录

1、父工程创建
2、支付模块构建和热部署
3、消费者订单模块
4、服务注册中心-Eureka
5、zookeeper没学习
6、服务注册中心-Consul
7、Eureka、Consul异同
8、服务调用-Ribbon
9、服务调用-OpenFeign

10、服务降级-Hystrix
11、服务降级-Hystrix(二)
12、服务熔断-Hystrix
13、服务网关-Gateway
14-17 在git上做配置中心,没有学习
17、请求链路跟踪-Sleuth
18、Spring Cloud Alibaba-Nacos注册中心与配置中心
19、Spring Cloud Alibaba-Nacos集群和持久化配置
20、Sentinel流控
21、Sentinel熔断降级、热点key限流
22、SentinelResource配置
23、Sentinel 服务熔断与持久化


1. 新建一个模块步骤

  1. 新建module
  2. 改POM
  3. 写YML
  4. 主启动类
  5. 业务类

新建为cloud-provider-payment8001的Maven工程

1.1 POM

<?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">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.chzu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
    </dependencies>
</project>

1.2 写YML

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: com.mysql.cj.jdbc.Driver         # mysql驱动包
    url: jdbc:mysql://localhost:3306/my/mysql_lwd?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root
    password: root

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.chzu.springcloud.entities    # 所有Entity别名类所在包

1.3 主启动类

package com.chzu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author lwd
 * @description 主启动类
 * @create 2021/07/14
 */
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

1.4 构建业务类

建表的语句

CREATE TABLE `payment`(
    `id` BIGINT(20) not NULL AUTO_INCREMENT comment 'ID',
    `serial` VARCHAR(200) DEFAULT '',
    PRIMARY KEY(`id`)
    )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHAR SET=utf8

新建如下几个包

  • entities

Payment实体类

//支付实体类
@Data
@NoArgsConstructor //全参构造器
@AllArgsConstructor //无参构造器
public class Payment {
    
    private Long id;

    private String serial;
}

JSON封装体CommonResult:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {

    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String msg){
        this(code,msg,null);
    }
}
  • dao
@Mapper
public interface PaymentDao{

    /**
     * 创建一个订单记录
     * @return
     */
    int create(Payment payment);

    /**
     * 根据id获取订单信息
     * @param id
     * @return
     */
    Payment getPaymentById(@Param("id") Long id);
}

在resources目录下新建mapper包,在这个包之下新建PaymentDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.chzu.springcloud.dao.PaymentDao">
	
	<!--useGeneratedKeys 取值范围true、false 默认值是:false。
    含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中-->
    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment(serial)  values(#{serial});
    </insert>

    <resultMap id="BaseResultMap" type="com.chzu.springcloud.entities.Payment">
    	<!--column="数据库中的字段名" property="java类中的属性名" -->
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id};
    </select>

</mapper>
  • service

接口PaymentService

public interface PaymentService {

    /**
     * 创建一个订单类
     * @return
     */
    int create(Payment payment);

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    Payment getPaymentById(@Param("id") Long id);
}

在service包下新建impl包,创建PaymentService的实现类PaymentServiceImpl

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentDao dao;

    @Override
    public int create(Payment payment) {
        return dao.create(payment);
    }

    @Override
    public Payment getPaymentById(Long id) {
        return dao.getPaymentById(id);
    }
}
  • controller
@RestController
@RequestMapping("payment")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService service;

    @PostMapping(value = "/payment/create")
    public CommonResult create(Payment payment)
    {
        int result = service.create(payment);
        log.info("====插入结果====:"+result);

        if(result > 0)
        {
            return new CommonResult(200,"插入数据成功: ",result);
        }else{
            return new CommonResult(444,"插入数据失败",null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
        Payment payment = service.getPaymentById(id);

        if(payment != null)
        {
            return new CommonResult(200,"查询成功",payment);
        }else{
            return new CommonResult(444,"查询无记录 "+id,null);
        }
    }

}

1.5 进行测试

在Postman中:
http://localhost:8001/payment/create?serial=233
在这里插入图片描述
如果想要使用json格式传递数据,需要在后端的Controller中添加注解public CommonResult create(@RequestBody Payment payment),同时别忘了更改postman中的Content-type为JSON
在这里插入图片描述

2. 热部署Devtools

在开发时使用方便调试。

2.1 添加依赖

在payment8001的pom中添加如下依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

2.2 添加插件

在总工程的pom中添加一个插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.3 开启自动编译选项

在这里插入图片描述

2.4 勾选项

crtl+shif+alt+/在弹出的框内点击Registry,勾选如图所示

在这里插入图片描述

2.5 重启idea

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值