三、(准备篇)Maven父子项目搭建—子级篇
一、我们的业务是一个订单支付系统,创建两个微服务。一个 order(端口80) 和 payment(端口8001)
image.png二、本文创建 Moudle 的步骤分为 4 步:
image.png1、【建Modile】 :选中父级项目名,new Moudle;选择Maven项目和JDK版本;输入 Moudle 名称
new Moudle.png选择Maven项目.png
输入 Moudle 名称.png
可以看到父 pom 中已经存在该Moudle 了
父 pom.png
2、【改POM】:引入子 pom 的 <dependencie>
引入完后记得重新导入Maven的包,点击下图按钮
image.png
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<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>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3、【写YML】:在/src/main/resource 路径下新建 application.yml 文件
(1)mapperLocations:classpath:mapper/*.xml 表示在resources路径下有个 mapper 文件夹,里面防止映射xml文件。加载的时候会去这里找
(2)数据库配置的url末尾有个 “useSSL=false” 表示建立SSL连接,子啊mysql的高版本中不设置的话,连接会报错
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://localhost:3306/db2020?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.solomon.springcloud.eitities #所有Entity别名类所在包
(3)踩坑:yml 文件有严格的缩进要求,缩进不对会报错,笔者因为datasource定格了就报了如下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
4、【主启动】:
package com.solomon.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、【写业务】:
一、建库建表
1、建Mysql库,字符集选择 utfmb4,该字符集支持4每个字符四个字节,utf8 只支持每个字符3个字节;
image.png
2、排序规则选择 utf8mb4_general_ci,它在排序和比较的时候速度较其它更快。
CREATE TABLE `payment` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`serial` varchar(20) DEFAULT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 ;
二、写业务代码Controller、Service、Dao、Mapper
参考gitHub地址 https://github.com/Solomon258/cloud2020/tree/dev
三、重复上述所有步骤,新建一个子模块 cloud-consumer-order80 用于调用 payment
1、下图是 cloud-consumer-order80 的 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>cloud2020</artifactId>
<groupId>com.solomon.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-order80</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<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>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、我们的设计是 order 模块对外暴漏接口,供前端调用,但是 Order 模块自己没有业务逻辑,业务逻辑在 payment 中。所以在 cloud-order-service 服务中,我们只需要一个 Controller 层对外暴漏接口,然后访问 cloud-provider-payment8001 的接口就行。
(1)写一个配置类,我们通过 RestTemplate 来实现模块间的 Rest 接口调用
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
3、写Controller,然后 clean—> install,然后启动两个服务,就可以通过 order 调用 payment 了
import com.solomon.springcloud.entities.CommonResult;
import com.solomon.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class OderController {
@Resource
private RestTemplate restTemplate;
private final static String PAYMENT_URI = "http://localhost:8001";
@PostMapping("/consumer/create")
public CommonResult<Payment> create(@RequestBody Payment payment) {
return restTemplate.postForObject(PAYMENT_URI + "/paymnet/create",payment,CommonResult.class);
}
@GetMapping("/consumer/get/{id}")
public CommonResult<Payment> get(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URI + "/paymnet/getPaymentById/"+id, CommonResult.class);
}
}
参考gitHub地址 https://github.com/Solomon258/cloud2020/tree/dev