03-SpringCloud 支付模块搭建
小知识: 常用的创建微服务模块的套路 1:创建Module 2:修改POM配置文件 3:编写yml配置文件 4:编写启动的主类 5:编写业务类
这个支付模块的构建只是说的一种场景,而不是真的对接支付接口,只是走一个微服务,服务之间的调用流程
支付微服务提供者
创建module(只写一次之后的创建只会写修改配置文件,创建流程都是这样的)
右键新建Module
1:选择Maven
2:选择JDK8
3:点击next,不选择模板
修改模块名称,为了方便直接携带端口
点击Finish
修改POM.xml
<?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>springcloud</artifactId> <groupId>com.dance</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8001</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--包含了sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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>
创建application.yml
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123445 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.dance.springcloud.entities # 所有Entity别名类所在包
在这里可以看到连接了MySQL 并且有mybatis的配置
先创建配置对应的别名包,再创建xml存放的mapper包
创建数据库和表
因为连接了mysql,所以我们需要创建对应名称的数据库和之后需要使用的表
有数据库的小伙伴可以直接创建,没有的,我在这里推荐一个好用的mysql工具
那就是xmapp,可以通过面板开启和关闭mysql
然后通过Navicat连接mysql,右击新建数据库
新建表
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 CHARSET=utf8mb4
新建主启动类
package com.dance.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class PaymentMain001 { public static void main(String[] args) { SpringApplication.run(PaymentMain001.class, args); } }
至于包名的话,大家看代码吧,如果不想自己写的话,我会在最后上传自己的码云地址的,有喜欢的小伙伴可以自己去下载
新建实体类
package com.dance.springcloud.entities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
新建统一返回的返回类
package com.dance.springcloud.common; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @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); } }
新建dao
package com.dance.springcloud.dao;
import com.dance.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
*/
@Mapper
//@Repository不用Spring的
public interface PaymentDao
{
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
MyBatis映射文件PaymentMapper.xml,路径:resources/mapper/PaymentMapper.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.dance.springcloud.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <resultMap id="BaseResultMap" type="com.dance.springcloud.entities.Payment"> <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
package com.dance.springcloud.service; import com.dance.springcloud.entities.Payment; import org.apache.ibatis.annotations.Param; /** */ public interface PaymentService { public int create(Payment payment); public Payment getPaymentById(@Param("id") Long id); }
新建Service实现类
package com.dance.springcloud.service.impl; import com.dance.springcloud.dao.PaymentDao; import com.dance.springcloud.entities.Payment; import com.dance.springcloud.service.PaymentService; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * */ @Service public class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; public int create(Payment payment) { return paymentDao.create(payment); } public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } }
新建Controller
package com.dance.springcloud.controller; import com.dance.springcloud.entities.CommonResult; import com.dance.springcloud.entities.Payment; import com.dance.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * */ @RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @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, "插入数据库成功,serverPort: " + serverPort, result); } else { return new CommonResult(444, "插入数据库失败", null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) { Payment payment = paymentService.getPaymentById(id); if (payment != null) { return new CommonResult(200, "查询成功,serverPort: " + serverPort, payment); } else { return new CommonResult(444, "没有对应记录,查询ID: " + id, null); } } }
好了到此,基础的支付微服务提供者8001就创建完成了
启动项目后,应为添加了Eureka依赖的原因,项目启动后,下面会一直访问Eureka的注册地址,访问不到会报错,不过不用管
测试
- 浏览器 - http://localhost:8001/payment/get/1
- Postman - http://localhost:8001/payment/create?serial=lun2
访问成功
测试添加数据
到此提供者支付模块搭建完成,其实我也感觉上面写的挺乱的,所以我创建了码云的git仓库
https://gitee.com/flower-dance-mrz/spring-cloud-learning-code.git
这个就是我的项目地址了
作者:彼岸舞
时间:2021\08\16
内容关于:Spring Cloud H版
本文属于作者原创,未经允许,禁止转发