1、cloud-provider-payment8001微服务提供者Module模块
构建步骤:
1.1、建立module
创建完成后回到父工程查看pom文件变化
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
<!--表示父工程-->
<packaging>pom</packaging>
<!--表示子工程-->
<modules>
<module>cloud-provider-payment8001</module>
</modules>
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>cloud2020</artifactId>
<groupId>com.atguigu.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>
</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>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
1.3、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 # mysql驱动包
url: jdbc:mysql://192.168.3.3:3306/springcloud-2020?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
http: #解决输出中文信息到页面为乱码的问题
encoding:
charset: UTF-8
force: true
enabled: true
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
1.4、主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @create 2020-12-07 16:58
*/
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
1.5、业务
建表SQL:
CREATE TABLE `payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
`serial` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '支付流水号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '支付表' ROW_FORMAT = Dynamic;
创建实体类:Payment
package com.atguigu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @create 2020-12-07 19:17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private long id;
private String serial;
}
创建Json封装体CommonResult
package com.atguigu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @create 2020-12-07 19:45
* 创建一个前端通用的JSON实体串,前端可以通过返回值来确定是否调用成功
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult <T> {
//数值类型比如404、500
private Integer code;
//字符串类型比如 not_found
private String message;
private T date;
//date可能是空参,所以构造方法给个null
public CommonResult (Integer code,String message){
this(code,message,null);
}
}
创建接口PaymentDao:
package com.atguigu.springcloud.dao;
import com.atguigu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @create 2020-12-07 19:57
*/
@Mapper
public interface PaymentDao {
//写
public int create (Payment payment);
//读,@Param用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳
public Payment getpaymentByid(@Param("id") Long id);
}
创建mybatis的映射文件PaymentMapper.xml,路径:src\main\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.atguigu.springcloud.dao.PaymentDao">
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment (serial)values (#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.atguigu.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>
创建接口PaymentService
package com.atguigu.springcloud.service;
import com.atguigu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
/**
* @create 2020-12-08 16:48
*/
public interface PaymentService {
//写
public int create (Payment payment);
//读,@Param用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳
public Payment getpaymentByid(@Param("id") Long id);
}
创建PaymentService实现类PaymentServiceimpl
package com.atguigu.springcloud.service;
import com.atguigu.springcloud.dao.PaymentDao;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @create 2020-12-08 16:49
*/
@Service
public class PaymentServiceimpl implements PaymentService{
@Resource
private PaymentDao paymentDao;
//写
@Override
public int create (Payment payment){
return paymentDao.create(payment);
};
//读,@Param用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳
@Override
public Payment getpaymentByid(Long id){
return paymentDao.getpaymentByid(id);
}
}
创建controller
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @create 2020-12-08 17:02
*/
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
//写用PostMapping
@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,"插入数据库失败",null);
}
}
//读用GetMapping
@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.1、查询:http://192.168.3.5:8001/payment/get/1
1.2、插入:http://192.168.3.5:8001/payment/create?serial=guigu002,原因是游览器对于Post请求不理想
使用postman模拟post,测试成功
2、热部署Devtools
2.1、引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
2.2、聚合父类总工程的pom.xml
<build>
<fileName>你自己的工程名字,这个名字可以写也可以不写<fileName>
<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、idea设置
2.4、在父pom设置
2.5、在重启idea
3、cloud-consumer-order80微服务消费者订单Module模块
构建步骤:
3.1、建立module
创建完成后回到父工程查看pom文件变化
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
<!--表示父工程-->
<packaging>pom</packaging>
<modules>
<module>cloud-provider-payment8001</module>
<module>cloud-consumer-order80</module>
</modules>
3.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.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-order80</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.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>
3.3、application.yml
server:
port: 80
spring:
application:
name: cloud-order-service
http: #解决输出中文信息到页面为乱码的问题
encoding:
charset: UTF-8
force: true
enabled: true
3.4、主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @create 2020-12-10 13:31
*/
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
3.5、业务
创建实体类:Payment
package com.atguigu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @create 2020-12-07 19:17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private long id;
private String serial;
}
创建Json封装体CommonResult
package com.atguigu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @create 2020-12-07 19:45
* 创建一个前端通用的JSON实体串,前端可以通过返回值来确定是否调用成功
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult <T> {
//数值类型比如404、500
private Integer code;
//字符串类型比如 not_found
private String message;
private T date;
//date可能是空参,所以构造方法给个null
public CommonResult (Integer code,String message){
this(code,message,null);
}
}
创建config配置类:
package com.atguigu.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @className: ApplicationContextConfig
* @description:
* @author: liusCoding
* @create: 2020-06-04 11:57
*/
@Configuration
public class ApplicationContextConfig {
@Bean("restTemplate")
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
Resttemplate:
Resttemplate使用:
创建controller:
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @create 2020-12-10 13:54
*/
@RestController
public class OrderController {
public static String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult <Payment> create(Payment payment){
//写操作
return restTemplate.postForObject(PAYMENT_URL + "/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult <Payment> getPayment(@PathVariable("id") Long id){
//读操作
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/"+id,CommonResult.class);
}
}
启动服务,idea会弹出Run Dashboard
如果没有弹出则需要设置:通过修改idea的workspace.xml的方式快速打开Run Dashboard窗口
配置内容:
<component name="RunDashboard">
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType" />
</set>
</option>
</component>
截图:
idea启动,部分版本需要重启
启动后控制台:红框内的选项是用来过滤服务状态的,列如:启动,启动失败等等
测试:
1.1、查询:http://192.168.3.5/consumer/payment/get/1
1.2、插入:http://192.168.3.5/consumer/payment/create?serial=guigu003
这里显示插入成功,但是来看数据库发现是个null,原因:服务提供者方法中缺少一个注解@RequestBody
给8001调用的方法补充@RequestBody,并重新测试
代码:
//写用PostMapping
@PostMapping(value = "payment/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("*****插入结果"+result);
if(result > 0){
return new CommonResult(200,"插入数据库成功",result);
}else{
return new CommonResult(444,"插入数据库失败",null);
}
}
重新访问:http://192.168.3.5/consumer/payment/create?serial=guigu004,插入成功
4、工程重构
4.1、问题:系统中有重复部分的情况,需要进行重构
4.2、新建cloud-api-common
4.3、改pom,并maven命令clean install打包到本地
<?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.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-api-common</artifactId>
<dependencies>
<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>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
4.5、cloud-consumer-order80、cloud-provider-payment8001删除各自的原先的entities文件夹,并引入依赖
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
测试:
查询:http://192.168.3.5/consumer/payment/get/4
插入:http://192.168.3.5/consumer/payment/create?serial=AAAA
插入成功