一、統一版本
cloud—>Hoxton.SR1
boot—>2.2.2.RELEASE
cloud alibaba—>2.1.0.RELEASE
Java—>Java8
Maven—>3.5及以上
Mysql—>5.7及以上
二、基於分佈式的微服務架構常用技術與實現
1、服務註冊與發現(EUREKA)
1.Eureka停更,現在推薦使用Zookeeper、Consul、Nacos(強烈推薦)
2、服務負載與調用(NETFLIX|OSS RIBBON)
1.Ribbon、LoadBalancer
3、服務負載與調用(NETFLIX FELGN)
1.OpenFeign
4、服務熔斷降級(HYSTRIX)
1.resilience4j(國外)、Sentinel(推薦)
5、服務網關(NETFLIX|OSS Zuul)
1.gateway(重點)
6、服務分佈式配置(Spring Cloud Config)
1.Nacos
7、服務總線
1.Nacos
8、服務開發(Spring Boot)
三、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.atguigu.springcloud</groupId>
<artifactId>cloud2020</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.cource>1.8</maven.compiler.cource>
<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>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<!--子模塊繼承之後,提供作用:鎖定版本+子modlue不用寫groupId和version-->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</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>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>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</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>
1、dependencyManagement和dependencies的區別
Maven使用dependencyManagement元素來提供了一種管理依賴版本號的方式。通常會在一個組織或者目的最頂層的父pom中看到dependencyManagement元素
使用pom.xml中的dependencyManagement元素能讓所有在子項目中引用一個依賴而不用顯式的列出版本號。Maven會沿着父子層次向上走,知道找到一個擁有dependencyManagement元素的項目,然後它就會使用這個dependencyManagement元素中指定的版本號。
這樣做的好處就是:如果有多個子項目都引用一樣的依賴,則可以避免在每個使用的子項目裏都聲明一個版本號,這樣當想升級或切換到另一個版本時,只需要在頂層父容器裏更新,而不需要一個個子項目的修改;另外如果某個子項目需要另外的一個版本,只需要聲明version就可以了。
dependencyManagement裏只是聲明依賴,並不實現引入,因此子項目需要顯示的聲明需要用的依賴。
如果不在子項目中聲明依賴,是不會從父項目中繼承下來的;只有在子項目中寫了該依賴項,並且沒有指定具體版本,纔會從父項目中 繼承 該項,並且version和scope都讀取自父pom。
如果子項目中指定了版本號,那麼會使子項目中指定的jar版本。
四、支付模塊環境搭建
1.建module
在父工程中新建一個子項目
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>
<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>
3.寫YML
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource #當前數據源操作類型
drover-class-name: org.gjt.mm.mysql.Driver #mysql驅動包
url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username:root
password:123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.atguitu.springcloud.entities #所有Entity別名類所在包
4.主啓動
新建項目啓動類
package comatguigu.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)建表SQL
CREATE TABLE `payment` (
`id` bigint(20) NOT NULL,
`serial` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of payment
-- ----------------------------
INSERT INTO `payment` VALUES ('35', 'aaabbb01');
2)entities
Payment實體類:
package comatguigu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
private long id;
private String serial;
}
CommentResult類(前後端分離所使用的類):
package comatguigu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
//404 not_found
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message){
this(code,message,null);
}
}
3)dao
package comatguigu.springcloud.dao;
import comatguigu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentDao {
//crud....
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
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(id,serial) values(#{id},#{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>
注:
<resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
<id column="order_status" property="orderStatus" jdbcType="VARCHAR"/>
</resultMap>
resultMap中,column和property可能不同,column是數據庫字段的命名規範,而property是實體類中的命名規範,可能會有不同。
4)service
接口:
package com.atguigu.springcloud.service;
import com.atguigu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
實現類:
package com.atguigu.springcloud.service.impl;
import com.atguigu.springcloud.dao.PaymentDao;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
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);
}
}
5)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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@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(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,"沒有對應的記錄",null);
}
}
}
6)測試
由於瀏覽器一般不支持post請求,使用Postman工具模擬測試,自測通過
五、熱部署(Devtools)
1.Adding devtools to your project
2.Adding plugin to your pom.xml
3.Enabling automatic build
4.Update the value of
5.重啓IDEA
六、訂單模塊搭建
1.建cloud-consumer-orderr80
2.改pom
3.寫yml
4.主啓動類
5.業務類
1)entities
拷貝上面的支付模塊的實體類(Payment和CommonResult)
2)首說RestTemplate
使用RestTemplate完成兩個模塊的遠程調用,一個調用接口方式的封裝。
是什麼:
RestTemplate提供了多種便捷訪問遠程Http服務的方法,
是一種簡單便捷的訪問restful服務模板類,是spring提供的用於訪問Rest服務的客戶端模板工具集。
使用方法:
官網地址
使用:
使用restTemplate訪問restful接口非常的簡單。
(url,requestMmap,ResponseBean.class)這三個參數分別代表REST請求地址、請求參數、HTTP相應轉換被轉換成的對象類型。
3)配置restTemplate
package com.atguigu.springcloud.config;
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();
}
}
4)Controller
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
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;
@RestController
@Slf4j
public class OrderController {
public static final 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);
}
}
6.測試
查詢:
http://localhost/consumer/payment/get/35
添加:
http://localhost/consumer/payment/create?id=22&serial=1111
發現添加不成功,此時需要在8001支付模塊添加的controller中增加@RequestBody註解
@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);
}
}
運行過程中沒有Run DashBoard解決方案
選擇viwe->Tool Windows->Services
即可
測試通過!!
七、工程重構
由於兩個工程中有重複的實體代碼,
將兩個工程中相同的代碼提到一個公開共用的工程中,供大家統一調配使用。
1.新建工程 colud-api-commons
2.新建通用封裝類
3.使用maven install
4.刪除工程中重複的實體類
5.在需要使用這個公共包的時只需要引入依賴即可
pom.xml:
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
零基礎入門完結。。。