2.项目搭建
2.1 父工程模块的构建
我们会通过一个支付模块来把微服务的各个新老技术全部串联一遍。
先建立一个父工程。
使用本地自己下载的maven。如未下载可以参考maven的下载与安装教程(超详细)_Java修炼记-CSDN博客_maven下载教程。
最后出现Build Success 说明创建成功了。
统一好字符编码,避免后面出现乱码等问题。
开启注解支持。
java编译版本选择为java8.
FileType过滤。这一步选做,可以把项目中不想显示的文件过滤,这样项目看起来会清爽一点。
2.2 父工程的pom文件配置
将父工程的src目录删除,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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wangzhou.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.49</mysql.version>
<druid.version>1.1.17</druid.version>
<mybatis.spring.boot.version>2.0.5</mybatis.spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<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>
</project>
去除maven生命周期中的test 环节。
点击install ,出现Build Success 说明maven与Idea的整合成功了。
2.3 Rest微服务工作空间构建
我们的需求可以用下图表示:通过order 订单模块调用payment 模块完成支付。
一个微服务的构建过程只要五步:1.建立module 2.改pom 3.写yaml 4.主启动类 5.业务类
(1)建立module
顺带一提,由于我们之前在父工程中使用dependencymanagement ,这里GroupId 与Version 都是自动生成的。
(2)改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>cloud2020</artifactId>
<groupId>com.wangzhou.springcloud</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>
<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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<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>
这里可能会爆红。
将父工程pom文件mybatis的版本与springboot保持一致即可。
(3)写Yaml
新建application.yml。
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud2021?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.wangzhou.springcloud.entities
(4)主启动类
package com.wangzhou.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
(5)业务类
对于后端程序员来说,业务也特别简单,就是五步:建SQL、entities、Dao、Service、Controller。
建数据库
CREATE DATABASE cloud2021;
建表
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=utf8
插入数据。
INSERT INTO payment(`serial`)VALUES("张三");
建立entities。在这之前这里先放出项目的目录结构的截图供您参考。
主实体。
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Payment implements Serializable {
private Long id;
private String serial;
}
统一结果返回。
@AllArgsConstructor
@NoArgsConstructor
@Data
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message) {
this(code, message, null);
}
}
Dao。
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
Payment.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.wangzhou.springcloud.dao.PaymentDao">
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into Payment(serial) values (#{serial})
</insert>
<resultMap id="BaseMap" type="com.wangzhou.springcloud.entities.Payment">
<id column="id" property="id" javaType="BIGINT"/>
<id column="serial" property="serial" javaType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseMap">
select * from payment where id=#{id}
</select>
</mapper>
Service
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
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);
}
}
Controller.
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
@Resource
private PaymentService service;
@PostMapping("/created")
public CommonResult create(@RequestBody Payment payment) {
int result = service.create(payment);
log.info("插入结果是:" + result);
if(result > 0) {
return new CommonResult(200, "插入数据成功", 1);
} else {
return new CommonResult(444, "插入数据失败");
}
}
@GetMapping(value = "/get/{id}")
public CommonResult getPaymentById(@PathVariable("id")Long id){
Payment result = service.getPaymentById(id);
log.info("-----查询结果----: "+ result);
if (result != null){
return new CommonResult(200,"查询成功",result);
}else {
return new CommonResult(444,"没有对应记录,查询id: "+id,null);
}
}
}
启动项目。遇到如下问题。
Injection of resource dependencies failed;耐心的看报错信息,看到下图错误。
对应看xml文件。发现jdbcType 写成了javaType
总结:对于异常一定要先耐心读打印出来的异常信息。
查询测试:localhost:8001/payment/get/1
查询结果如下。
插入结果如下。
|