1.微服务cloud整体聚合工程

1.1.父工程步骤

New Project
《SpringCloud专题03》-微服务架构编码构建-服务提供者_spring
聚合总父工程名字
《SpringCloud专题03》-微服务架构编码构建-服务提供者_maven_02
Maven选版本
《SpringCloud专题03》-微服务架构编码构建-服务提供者_spring_03
字符编码
《SpringCloud专题03》-微服务架构编码构建-服务提供者_xml_04
注解生效激活
《SpringCloud专题03》-微服务架构编码构建-服务提供者_spring_05
java编译版本选8
《SpringCloud专题03》-微服务架构编码构建-服务提供者_xml_06
File Type过滤
《SpringCloud专题03》-微服务架构编码构建-服务提供者_maven_07

1.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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.itxiongmao.springcloud</groupId>
<artifactId>springcloud2020</artifactId>
<version>1.0-SNAPSHOT</version>

<modules>
<module>cloud-provider-payment8001</module>
</modules>

<packaging>pom</packaging>

<!--统一管理jar包版本-->
<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.47</mysql.version>
<druid.version>1.1.16</druid.version>
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
<spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>

<!--子模块继承后,提供作用:锁定版本+子module不用groupId和version-->
<dependencyManagement>
<dependencies>
<!--springboot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</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>

<!--第三方maven私服-->
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>

maven中跳过单元测试
《SpringCloud专题03》-微服务架构编码构建-服务提供者_maven_08

2.Rest微服务工程搭建

2.1.Cloud-provider-payment8001 微服务提供者Module模块

2.1.1.建module

创建完成后回到父工程查看pom文件变化
《SpringCloud专题03》-微服务架构编码构建-服务提供者_spring_09
《SpringCloud专题03》-微服务架构编码构建-服务提供者_xml_10

2.1.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>springcloud2020</artifactId>
<groupId>com.itxiongmao.springcloud</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>
<version>2.0.0</version>
</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-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
2.1.3.主启动
package com.itxiongmao;

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);
}
}
2.1.4.写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
url: jdbc:mysql://127.0.0.1:3306/db2020_cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456

mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.itxiongmao.springcloud.entities
2.1.5.测试数据库
-- ----------------------------
-- Table structure for payment
-- ----------------------------
DROP TABLE IF EXISTS `payment`;
CREATE TABLE `payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`serial` varchar(200) DEFAULT NULL COMMENT '支付流水号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='支付表';

-- ----------------------------
-- Records of payment
-- ----------------------------
INSERT INTO `payment` VALUES ('31', 'test001');
INSERT INTO `payment` VALUES ('32', 'test002');
INSERT INTO `payment` VALUES ('34', 'test003');
2.1.6.主实体Payment

《SpringCloud专题03》-微服务架构编码构建-服务提供者_xml_11
新建一个module,专门把实体类做一个工程

package com.itxiongmao.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.springcloud.entities
* @CreateTime: 2020-07-07 23:32
* @Description: TODO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
<?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>springcloud2020</artifactId>
<groupId>com.itxiongmao.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-api-commons</artifactId>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>

</project>
2.1.7.Json封装体CommonResult
package com.itxiongmao.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.springcloud.entities
* @CreateTime: 2020-07-07 23:36
* @Description: TODO
*/

@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);
}
}
2.1.8.接口PaymentDao
package com.itxiongmao.dao;

import com.itxiongmao.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.dao
* @CreateTime: 2020-07-07 23:38
* @Description: TODO
*/
@Mapper
public interface PaymentDao {

/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
Payment queryById(Long id);

/**
* 查询指定行数据
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
List<Payment> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


/**
* 通过实体作为筛选条件查询
*
* @param payment 实例对象
* @return 对象列表
*/
List<Payment> queryAll(Payment payment);

/**
* 新增数据
*
* @param payment 实例对象
* @return 影响行数
*/
int insert(Payment payment);

/**
* 修改数据
*
* @param payment 实例对象
* @return 影响行数
*/
int update(Payment payment);

/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int deleteById(Long id);

}
2.1.9.mybatis的映射文件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.itxiongmao.dao.PaymentDao">

<resultMap type="com.itxiongmao.springcloud.entities.Payment" id="PaymentMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="serial" column="serial" jdbcType="VARCHAR"/>
</resultMap>

<!--查询单个-->
<select id="queryById" resultMap="PaymentMap" parameterType="Long">
select
id, serial
from payment
where id = #{id}
</select>

<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="PaymentMap">
select
id, serial
from payment
limit #{offset}, #{limit}
</select>

<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="PaymentMap">
select
id, serial
from payment
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="serial != null and serial != ''">
and serial = #{serial}
</if>
</where>
</select>

<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into payment(serial)
values (#{serial})
</insert>

<!--通过主键修改数据-->
<update id="update">
update payment
<set>
<if test="serial != null and serial != ''">
serial = #{serial},
</if>
</set>
where id = #{id}
</update>

<!--通过主键删除-->
<delete id="deleteById">
delete from payment where id = #{id}
</delete>

</mapper>
2.1.10.接口PaymentService
package com.itxiongmao.service;

import com.itxiongmao.springcloud.entities.Payment;

import java.util.List;

/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.service
* @CreateTime: 2020-07-07 23:48
* @Description: TODO
*/
public interface PaymentService {

/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
Payment queryById(Long id);

/**
* 查询多条数据
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
List<Payment> queryAllByLimit(int offset, int limit);

/**
* 新增数据
*
* @param payment 实例对象
* @return 实例对象
*/
Payment insert(Payment payment);

/**
* 修改数据
*
* @param payment 实例对象
* @return 实例对象
*/
Payment update(Payment payment);

/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Long id);

}
2.1.11.接口PaymentService实现类
package com.itxiongmao.service.impl;

import com.itxiongmao.dao.PaymentDao;
import com.itxiongmao.service.PaymentService;
import com.itxiongmao.springcloud.entities.Payment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.service.impl
* @CreateTime: 2020-07-07 23:49
* @Description: TODO
*/
@Service("paymentService")
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;

/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public Payment queryById(Long id) {
return this.paymentDao.queryById(id);
}

/**
* 查询多条数据
*
* @param offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
@Override
public List<Payment> queryAllByLimit(int offset, int limit) {
return this.paymentDao.queryAllByLimit(offset, limit);
}

/**
* 新增数据
*
* @param payment 实例对象
* @return 实例对象
*/
@Override
public Payment insert(Payment payment) {
this.paymentDao.insert(payment);
return payment;
}

/**
* 修改数据
*
* @param payment 实例对象
* @return 实例对象
*/
@Override
public Payment update(Payment payment) {
this.paymentDao.update(payment);
return this.queryById(payment.getId());
}

/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
@Override
public boolean deleteById(Long id) {
return this.paymentDao.deleteById(id) > 0;
}
}
2.1.12.controller
package com.itxiongmao.controller;

import com.itxiongmao.service.PaymentService;
import com.itxiongmao.springcloud.entities.CommonResult;
import com.itxiongmao.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @BelongsProject: springcloud2020
* @BelongsPackage: com.itxiongmao.controller
* @CreateTime: 2020-07-07 23:50
* @Description: TODO
*/
@RestController
@RequestMapping("payment")
public class PaymentController {
/**
* 服务对象
*/
@Resource
private PaymentService paymentService;

/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("get/{id}")
public CommonResult<Payment> selectOne(@PathVariable("id") Long id) {
Payment payment = this.paymentService.queryById(id);
return new CommonResult<Payment>(200,"select success 8001!",payment);
}

@PostMapping("create")
public CommonResult create(Payment payment) {
Payment insert = this.paymentService.insert(payment);
System.out.println(insert);
System.out.println("1234567890");
return new CommonResult(200,"insert success" ,insert);
}
}

3.启动测试

3.1.测试访问

​http://127.0.0.1:8001/payment/get/31​​​《SpringCloud专题03》-微服务架构编码构建-服务提供者_maven_12

3.2.postman模拟post

《SpringCloud专题03》-微服务架构编码构建-服务提供者_maven_13

3.3.开启Run DashBoard

通过修改idea的workspace.xml的方式快速打开Run Dashboard窗口,开启Run DashBoard

<component name="RunDashboard">
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType" />
</set>
</option>
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>

《SpringCloud专题03》-微服务架构编码构建-服务提供者_maven_14
部分同学可能由于idea版本不同,需要关闭重启

4.热部署Devtools

Adding devtools to your project(添加到你的子工程)

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

Adding plugin to your pom.xml(添加到你的父工程)

<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>

Enabling automatic build
《SpringCloud专题03》-微服务架构编码构建-服务提供者_xml_15
Update the value of
《SpringCloud专题03》-微服务架构编码构建-服务提供者_spring_16
《SpringCloud专题03》-微服务架构编码构建-服务提供者_xml_17
重启idea,测试热部署功能,修改Java代码,SpringBoot项目会重新自动重启!