一后德、我們的業(yè)務是一個訂單支付系統(tǒng),創(chuàng)建兩個微服務抄腔。一個 order(端口80) 和 payment(端口8001)
二瓢湃、本文創(chuàng)建 Moudle 的步驟分為 4 步:
1、【建Modile】 :選中父級項目名赫蛇,new Moudle绵患;選擇Maven項目和JDK版本;輸入 Moudle 名稱
可以看到父 pom 中已經(jīng)存在該Moudle 了
2悟耘、【改POM】:引入子 pom 的 <dependencie>
引入完后記得重新導入Maven的包落蝙,點擊下圖按鈕
<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)數(shù)據(jù)庫配置的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捐顷、【寫業(yè)務】:
一、建庫建表
1悲酷、建Mysql庫套菜,字符集選擇 utfmb4,該字符集支持4每個字符四個字節(jié)设易,utf8 只支持每個字符3個字節(jié);
2蛹头、排序規(guī)則選擇 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 ;
二、寫業(yè)務代碼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 模塊自己沒有業(yè)務邏輯既峡,業(yè)務邏輯在 payment 中羡榴。所以在 cloud-order-service 服務中,我們只需要一個 Controller 層對外暴漏接口运敢,然后訪問 cloud-provider-payment8001 的接口就行校仑。
(1)寫一個配置類,我們通過 RestTemplate 來實現(xiàn)模塊間的 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