# SpringCloud
# 版本选择
SpringBoot : 2.3.12.RELEASE
SpringCloud : Hoxton.SR12
SpringCloud alibaba : 2.2.7.RELEASE
java : 1.8.0_291
maven : 3.8.1
mysql : 5.7.36
[MindManager2020 64位 下载地址](http://dwnld.mindjet.com/stubs/Builds/MindManager2020/20_0_334/64Bit/MindManager 2020.msi)
[MindManager2020 32位 下载地址](http://dwnld.mindjet.com/stubs/Builds/MindManager2020/20_0_334/32Bit/MindManager 2020.msi)
激活码 : MP20-345-DP56-7778-919A
# 文章选择
consul + ribbon + openfeign + hystrix
gateway + config + bus + stream + sleuth
# cloud 升级
# 服务注册中心
对比
# 服务调用
# 服务降级
# 服务网关
# 服务配置
# 服务总线
# 总结
# 参考资料
springcloud官方文档 (opens new window)
springcloud中文文档 (opens new window)
springboot官方文档 (opens new window)
# 微服务架构编码构建
永远要记住
约定 > 配置 > 编码
# IDEA新建project工作空间
# New Project
# 聚合总父工程名字
# Maven 选版本
# 字符编码
# 注解生效激活
# java编译版本选8
# File Type过滤
# 父工程 POM
# 添加 packing
<packaging>pom</packaging>
# 删除 src 目录
# 修改 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>springcloud-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<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>2.14.1</log4j.version>
<lombok.version>1.18.22</lombok.version>
<mysql.version>8.0.27</mysql.version>
<druid.version>1.2.8</druid.version>
<mybatis.spring.boot.version>2.2.0</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本 + 子 module 不用写 groupId 和version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.3.12.RELEASE -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.12.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR12 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 2.2.7.RELEASE -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- springboot start-->
<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>
<!-- log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- lombok -->
<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>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# DependencyManagement 和 Dependencies
dependencyManagement
Maven 使用 dependencyManagement 元素来提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父 POM 中看到 dependencyManagement 元素。
使用 pom.xml 中的 dependencyManagement 元素能让所有在子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用这个 dependencyManagement 元素中指定的版本号。
这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改 ;另外如果某个子项目需要另外的一个版本,只需要声明 version 就可。
dependencyManagement 里只是声明依赖,==并不实现引入==,因此子项目需要显示的声明需要用的依赖。
如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且 version 和 scope 都读取自父 pom;
如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
# maven中跳过单元测试
# Rest微服务工程构建
图解
# 微服务模块构建 - payment 模块
微服务模块构建步骤
- 建 module
- 改 POM
- 写 Yaml
- 主启动
- 业务类
# 建 module
module 名 : cloud-provider-payment8001
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.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.2.8</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>
<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>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 写 yaml
server:
port: 8001
spring:
application:
name: cloud-provider-service
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql 驱动包
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://175.24.229.41:1617/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: W6YnvKnLzpSFNGuc.
mybatis:
mapper-locations: classpath:mapper/*.xml
# 所有 pojo 别名类所在包
type-aliases-package: com.yixihan.springcloud.pojo
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author : yixihan
* @create : 2022-01-16-15:07
*/
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run (PaymentMain8001.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 业务类
图解
# 建表 sql
CREATE DATABASE IF NOT EXISTS `springcloud`;
CREATE TABLE IF NOT EXISTS `payment`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`serial` varchar(200) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8
2
3
4
5
6
7
8
# pojo
Payment
package com.yixihan.springcloud.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author : yixihan
* @create : 2022-01-16-15:18
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
/**
* id
*/
private Long id;
/**
* 流水号
*/
private String serial;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.yixihan.springcloud.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Json 封装体
* @author : yixihan
* @create : 2022-01-16-15:19
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# mapper
PaymentMapper.java
package com.yixihan.springcloud.mapper;
import com.yixihan.springcloud.pojo.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @author : yixihan
* @create : 2022-01-16-15:23
*/
@Mapper
public interface PaymentMapper {
/**
* 创建一个订单对象
* @param payment 订单对象
* @return 订单对象
*/
Integer create (Payment payment);
/**
* 根据 id 获取订单对象
* @param id 订单 id
* @return 订单对象
*/
Payment getPaymentById (@Param ("id") Long id);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.yixihan.springcloud.mapper.PaymentMapper">
<!--useGeneratedKeys : 是否返回值-->
<!--keyProperty : 主键-->
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into springcloud.payment (serial)
VALUES (#{serial});
</insert>
<resultMap id="BaseResultMap" type="Payment">
<id property="id" column="id" jdbcType="BIGINT"/>
<result property="serial" column="serial" jdbcType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select *
from springcloud.payment
where id = #{id};
</select>
</mapper>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# service
PaymentService
package com.yixihan.springcloud.service;
import com.yixihan.springcloud.pojo.Payment;
import org.apache.ibatis.annotations.Param;
/**
* @author : yixihan
* @create : 2022-01-16-15:37
*/
public interface PaymentService {
/**
* 创建一个订单对象
* @param payment 订单对象
* @return 订单对象
*/
Integer create (Payment payment);
/**
* 根据 id 获取订单对象
* @param id 订单 id
* @return 订单对象
*/
Payment getPaymentById (@Param("id") Long id);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
PaymentServiceImpl
package com.yixihan.springcloud.service;
import com.yixihan.springcloud.mapper.PaymentMapper;
import com.yixihan.springcloud.pojo.Payment;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author : yixihan
* @create : 2022-01-16-15:36
*/
@Service
public class PaymentServiceImpl implements PaymentService{
@Resource
private PaymentMapper paymentMapper;
@Override
public Integer create(Payment payment) {
return paymentMapper.create (payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentMapper.getPaymentById (id);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# controller
package com.yixihan.springcloud.controller;
import com.yixihan.springcloud.pojo.CommonResult;
import com.yixihan.springcloud.pojo.Payment;
import com.yixihan.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;
/**
* @author : yixihan
* @create : 2022-01-16-15:40
*/
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
/**
* 小坑 : 要加 @RequestBody 这个注解
*/
@PostMapping("/payment/create")
public CommonResult<Integer> create(@RequestBody Payment payment) {
Integer result = paymentService.create (payment);
log.info ("****插入结果 : {}", result);
return result > 0 ?
new CommonResult<> (200, "插入成功", result) :
new CommonResult<> (444, "插入失败", null);
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment result = paymentService.getPaymentById (id);
log.info ("****查询结果 : {}", result);
return result != null ?
new CommonResult<> (200, "查询成功", result) :
new CommonResult<> (444, "没有对应记录, 查询 ID : " + id, null);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 测试
预先插入一条记录
insert into springcloud.payment (serial) values ('张三');
测试查询接口
http://localhost:8001/payment/get/id
http://localhost:8001/payment/create
# 总结
就五步
- 建 module
- 改 POM
- 写 YAML
- 主启动
- 业务类
# 热部署 Devtools
只允许在开发阶段使用
# Adding devtools to your project
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
2
3
4
5
6
# Adding plugin to your pom.xml
<build>
<finalName>springcloud-learn</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.12.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
2
3
4
5
6
7
8
9
10
11
12
13
14
# Enabling automatic build
# Update the value of
# 重启IDEA
即可
# 微服务模块构建 - customer 模块
图解
# 建 module
module 名 : cloud-consumer-order80
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-order80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<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>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 写 YAML
server:
port: 80
2
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author : yixihan
* @create : 2022-01-16-16:31
*/
@SpringBootApplication
public class MainApp80 {
public static void main(String[] args) {
SpringApplication.run (MainApp80.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 业务类
# pojo
同模块 module - cloud-provider-payment8001
pojo 包里 Payment 和 CommonResult
# RestTemplate
RestTemplate 是什么
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
RestTemplate 官方 API 文档 (opens new window)
使用
使用restTemplate访问restful接口非常的简单粗暴无脑。
(url, requestMap, ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
# config配置类
package com.yixihan.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author : yixihan
* @create : 2022-01-16-16:47
*/
@Configuration
public class ApplicationContextConfig {
@Bean("getRestTemplate")
public RestTemplate getRestTemplate () {
return new RestTemplate ();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# controller
package com.yixihan.springcloud.controller;
import com.yixihan.springcloud.pojo.CommonResult;
import com.yixihan.springcloud.pojo.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;
/**
* @author : yixihan
* @create : 2022-01-16-16:36
*/
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult create (Payment payment) {
return restTemplate.postForObject (PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult getPayment (@PathVariable("id") Long id) {
return restTemplate.getForObject (PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 测试
注意 : 开启两个服务需要开启 Run DashBoard(service 服务)
idea 会弹出提示, 同意即可
也可以通过修改 idea 的 workspace.xml 的方式来快速打开 Run Dashboard 窗口
workspace.xml 在项目 ./idea 目录下添加如下内容
<component name="RunDashboard">
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType" />
</set>
</option>
</component>
2
3
4
5
6
7
或者使用快捷键 alt + 8
若插入的值为 null, 则在 payment 模块内 create 函数 payment参数上加 @RequestBody 注解
# 工程重构
问题
系统中有重复部分,重构 (pojo)
# 微服务模块构建 - commons 模块
# 建 module
module 名 : cloud-api-commons
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-api-commons</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<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>
<!-- hutu 工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# pojo
同之前两个模块的 pojo 包
# 打包
执行 maven 指令 clean, install, 打包成可执行的 jar 包
# 改造 Payment 模块 和 customer 模块
删除 pojo 类
修改 POM
新增如下内容 :
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
2
3
4
5
6
# Eureka 服务注册
# Eureka 基础知识
# 服务治理
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理
在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
# 服务注册
Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
Eureka系统架构
Dubbo的架构
# Eureka两组件
Eureka Server
Eureka Server 提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
Eureka Client
EurekaClient 通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
# 单机Eureka构建
图解
# 单机Eureka构建步骤
# 建 module
module 名 : cloud-eureka-server7001
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--boot web actuator-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 写 YAML
server:
port: 7001
eureka:
instance:
# eureka 服务端的实例名称
hostname: localhost
client:
# false 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己端就是注册中心, 职责就是维护服务实例, 并不需要去检索服务
fetch-registry: false
service-url:
# 设置与 Eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author : yixihan
* @create : 2022-01-17-7:47
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run (EurekaMain7001.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 测试
# payment8001 模块注册进 EurekaServer
# 改 POM
新增内容
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2
3
4
5
# 写 YAML
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true. 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
2
3
4
5
6
7
8
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author : yixihan
* @create : 2022-01-16-15:07
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run (PaymentMain8001.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 测试
ps : 先要启动EurekaServer
微服务注册名配置说明
# Eureka 自我保护机制 (后面会解决)
Eureka 控制台上面的红字警告即 Eureka 自我保护机制的一种体现
默认情况下,如果Eureka Server在一定时间内(默认90秒)没有接收到某个微服务实例的心跳,Eureka Server将会移除该实例。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,而微服务本身是正常运行的,此时不应该移除这个微服务,所以引入了自我保护机制。
自我保护模式正是一种针对网络异常波动的安全保护措施,使用自我保护模式能使Eureka集群更加的健壮、稳定的运行。
自我保护机制的工作机制是如果在15分钟内超过85%的客户端节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,Eureka Server自动进入自我保护机制
自我保护机制时会出现以下几种情况
- Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
- Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用
- 当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中
解决建议
在生产上可以开自注册,部署两个server
在本机器上测试的时候,可以把比值调低,比如0.49
把自我保护模式关闭
eureka: server: enableSelfPreservation: false
1
2
3
# order80 模块注册进 EurekaServer
# 改 POM
新增内容
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2
3
4
5
# 写 YAML
spring:
application:
name: cloud-order-service
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true. 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
2
3
4
5
6
7
8
9
10
11
12
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author : yixihan
* @create : 2022-01-16-16:31
*/
@SpringBootApplication
@EnableEurekaClient
public class MainApp80 {
public static void main(String[] args) {
SpringApplication.run (MainApp80.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 测试
ps : 先要启动EurekaServer
控制台页面
测试接口
# 集群Eureka构建
# Eureka集群原理说明
原理图
微服务RPC远程服务调用最核心的是什么
高可用,试想你的注册中心只有一个only one, 它出故障了那就呵呵( ̄▽ ̄)"了,会导致整个为服务环境不可用
解决办法:搭建Eureka注册中心集群 ,实现负载均衡+故障容错
图解
EurekaServer集群环境构建步骤
# EurekaServer集群环境构建步骤
# 建 module
module 名 : cloud-eureka-server7002
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7002</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--boot web actuator-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 修改映射配置
修改映射配置添加进hosts文件
# springcloud 学习
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
2
3
4
# 写 YAML
单机 eureka 配置
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2
3
4
5
6
7
8
9
10
11
12
13
14
集群 eureka 配置
7001
server:
port: 7001
eureka:
instance:
# eureka 服务端的实例名称
hostname: eureka7001.com
client:
# false 表示不向注册中心注册自己。
register-with-eureka: false
# false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
7002
server:
port: 7002
eureka:
instance:
# eureka 服务端的实例名称
hostname: eureka7002.com
client:
# false 表示不向注册中心注册自己。
register-with-eureka: false
# false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
2
3
4
5
6
7
8
9
10
11
12
13
14
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author : yixihan
* @create : 2022-01-17-8:29
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run (EurekaMain7002.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 测试
eureka 7001 控制台 (opens new window)
eureka 7002 控制台 (opens new window)
控制台页面
出现图中所示, 即可证明 eureka 集群搭建成功
# 将 payment8001 微服务发布到Eureka集群配置中
改 yaml - eureka
server:
port: 8001
spring:
application:
name: cloud-provider-service
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql 驱动包
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://175.24.229.41:1617/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: W6YnvKnLzpSFNGuc.
mybatis:
mapper-locations: classpath:mapper/*.xml
# 所有 pojo 别名类所在包
type-aliases-package: com.yixihan.springcloud.pojo
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true. 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 将 order80 微服务发布到Eureka集群配置中
改 yaml - eureka
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true. 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 测试 eureka 集群
ps : 要先启动 eureka server, 再启动服务提供者 8001. 最后启动消费者 80
eureka 7001 控制台 (opens new window)
eureka 7002 控制台 (opens new window)
控制台页面
接口测试
# payment 8001 集群环境构建
# 建 module
module 名 : cloud-provider-payment8002
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8002</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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.2.8</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>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 写 YAML
server:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql 驱动包
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://175.24.229.41:1617/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: W6YnvKnLzpSFNGuc.
mybatis:
mapper-locations: classpath:mapper/*.xml
# 所有 pojo 别名类所在包
type-aliases-package: com.yixihan.springcloud.pojo
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true. 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author : yixihan
* @create : 2022-01-17-9:03
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run (PaymentMain8002.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 业务类
同 payment 8001
# 修改 8001/8002 的 controller
package com.yixihan.springcloud.controller;
import com.yixihan.springcloud.pojo.CommonResult;
import com.yixihan.springcloud.pojo.Payment;
import com.yixihan.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author : yixihan
* @create : 2022-01-16-15:40
*/
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
/**
* 小坑 : 要加 @RequestBody 这个注解
*/
@PostMapping("/payment/create")
public CommonResult<Integer> create(@RequestBody Payment payment) {
Integer result = paymentService.create (payment);
log.info ("****插入结果 : {}", result);
return result > 0 ?
new CommonResult<> (200, "插入成功, serverPort : " + serverPort, result) :
new CommonResult<> (444, "插入失败, serverPort : " + serverPort, null);
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment result = paymentService.getPaymentById (id);
log.info ("****查询结果 : {}", result);
return result != null ?
new CommonResult<> (200, "查询成功, serverPort : " + serverPort, result) :
new CommonResult<> (444, "没有对应记录, 查询 ID : " + id + ", serverPort : " + serverPort, null);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 负载均衡
图解
# 修改 order 80 代码
controller
package com.yixihan.springcloud.controller;
import com.yixihan.springcloud.pojo.CommonResult;
import com.yixihan.springcloud.pojo.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;
/**
* @author : yixihan
* @create : 2022-01-16-16:36
*/
@RestController
@Slf4j
public class OrderController {
// public static final String PAYMENT_URL = "http://localhost:8001";
/**
* localhost:8001 -> 微服务名, 即 eureka 控制台上显示的名字 (CLOUD-PAYMENT-SERVICE)
*/
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@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);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
config
package com.yixihan.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author : yixihan
* @create : 2022-01-16-16:47
*/
@Configuration
public class ApplicationContextConfig {
/**
* 使用 @LoadBalanced 注解赋予 RestTemplate 负载均衡的能力
*/
@Bean("getRestTemplate")
@LoadBalanced
public RestTemplate getRestTemplate () {
return new RestTemplate ();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 测试 payment 集群
ps : 先启动 eureka server 7001/7002, 再启动服务提供者 8001/8002, 最后启动消费者 80
eureka 7001 控制台 (opens new window)
eureka 7002 控制台 (opens new window)
eureka 控制台
接口测试
Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号,且该服务还有负载功能了。O(∩_∩)O
# actuator微服务信息完善
# 主机名称:服务名称修改
当前问题
# 修改 YAML 文件
eureka:
instance:
# 主机名称 : 服务名称修改
instance-id: order80
2
3
4
# 访问信息有IP信息提示
当前问题
# 修改 YAML 文件
eureka:
instance:
# 访问路径可以显示 IP 地址
prefer-ip-address: true
2
3
4
# 测试
# 服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
# 修改 payment controller
package com.yixihan.springcloud.controller;
import com.yixihan.springcloud.pojo.CommonResult;
import com.yixihan.springcloud.pojo.Payment;
import com.yixihan.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* @author : yixihan
* @create : 2022-01-16-15:40
*/
@Slf4j
@RestController
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@Resource
private DiscoveryClient discoveryClient;
/**
* 小坑 : 要加 @RequestBody 这个注解
*/
@PostMapping("/payment/create")
public CommonResult<Integer> create(@RequestBody Payment payment) {
Integer result = paymentService.create (payment);
log.info ("****插入结果 : {}", result);
return result > 0 ?
new CommonResult<> (200, "插入成功, serverPort : " + serverPort, result) :
new CommonResult<> (444, "插入失败, serverPort : " + serverPort, null);
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment result = paymentService.getPaymentById (id);
log.info ("****查询结果 : {}", result);
return result != null ?
new CommonResult<> (200, "查询成功, serverPort : " + serverPort, result) :
new CommonResult<> (444, "没有对应记录, 查询 ID : " + id + ", serverPort : " + serverPort, null);
}
@GetMapping ("/payment/getService")
public CommonResult<List<String>> getServices () {
List<String> services = discoveryClient.getServices ();
for (String element : services) {
System.out.println(element);
}
return new CommonResult<> (200, "获取 services 信息成功", services);
}
@GetMapping ("/payment/getInstances")
public CommonResult<List<ServiceInstance>> getInstances () {
List<ServiceInstance> instances = discoveryClient.getInstances ("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance element : instances) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return new CommonResult<> (200, "获取 instances 信息成功", instances);
}
@GetMapping ("/payment/getDiscovery")
public CommonResult<DiscoveryClient> getDiscovery () {
List<String> services = discoveryClient.getServices ();
for (String element : services) {
System.out.println(element);
}
List<ServiceInstance> instances = discoveryClient.getInstances ("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance element : instances) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return new CommonResult<> (200, "获取 discovery 信息成功", discoveryClient);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 修改 payment 主启动类
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 注解 @EnableDiscoveryClient => 开启服务发现
* @author : yixihan
* @create : 2022-01-17-9:03
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run (PaymentMain8002.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 修改 order controller
package com.yixihan.springcloud.controller;
import com.yixihan.springcloud.pojo.CommonResult;
import com.yixihan.springcloud.pojo.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
/**
* @author : yixihan
* @create : 2022-01-16-16:36
*/
@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
// public static final String PAYMENT_URL = "http://localhost:8001";
/**
* localhost:8001 -> 微服务名, 即 eureka 控制台上显示的名字 (CLOUD-PAYMENT-SERVICE)
*/
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
@GetMapping("/payment/create")
public CommonResult<Payment> create (Payment payment) {
return restTemplate.postForObject (PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPayment (@PathVariable("id") Long id) {
return restTemplate.getForObject (PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
}
@GetMapping ("/payment/getService")
public CommonResult<List<String>> getServices () {
return restTemplate.getForObject (PAYMENT_URL + "/payment/getService", CommonResult.class);
}
@GetMapping ("/payment/getInstances")
public CommonResult<List<ServiceInstance>> getInstances () {
return restTemplate.getForObject (PAYMENT_URL + "/payment/getInstances", CommonResult.class);
}
@GetMapping ("/payment/getDiscovery")
public CommonResult<DiscoveryClient> getDiscovery () {
return restTemplate.getForObject (PAYMENT_URL + "/payment/getDiscovery", CommonResult.class);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 测试
log 信息
游览器信息
# eureka 自我保护
# 故障现象
保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。
如果在Eureka Server的首页看到以下这段提示,则说明Eureka进入了保护模式: EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE
# 导致原因
为什么会产生Eureka自我保护机制?
为了防止EurekaClient可以正常运行,但是 与 EurekaServer网络不通情况下,EurekaServer不会立刻将EurekaClient服务剔除
什么是自我保护模式?
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生(延时、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。
==一句话讲解:好死不如赖活着==
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
# 禁止自我保护
出厂默认,自我保护机制是开启的
# 注册中心 eureka server 端
eureka:
instance:
# eureka 服务端的实例名称
hostname: eureka7002.com
client:
# false 表示不向注册中心注册自己。
register-with-eureka: false
# false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
server:
# 关闭自我保护机制,保证不可用服务被及时踢除
enable-self-preservation: false
# 最长等待时间
eviction-interval-timer-in-ms: 2000
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 生产者消费者客户 eureka client 端
eureka:
client:
# 表示是否将自己注册进 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true. 单节点无所谓,集群必须设置为 true 才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
# 单机版
# defaultZone: http://localhost:7001/eureka
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
# 主机名称 : 服务名称修改
instance-id: payment8001
# 访问路径可以显示 IP 地址
prefer-ip-address: true
# Eureka 客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-expiration-duration-in-seconds: 1
# Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
lease-renewal-interval-in-seconds: 2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Zookeeper 服务注册
Eureka停止更新了你怎么办
解决方法之一 : SpringCloud整合Zookeeper代替Eureka
图解
# 注册中心 Zookeeper 简述
zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后启动zookeeper服务器
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
# 服务提供者
# 微服务模块构建
# 建module
module 名 : cloud-provider-payment8004
# 改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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- zookeeper -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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.2.8</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>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 写YAML
server:
port: 8004
# 需要写数据库配置, 不然会报错
spring:
application:
# 服务别名----注册 zookeeper 到注册中心名称
name: cloud-provider-payment
cloud:
zookeeper:
# zookeeper 主机 : 端口号
connect-string: 175.24.229.41:2181
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql 驱动包
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://175.24.229.41:1617/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: W6YnvKnLzpSFNGuc.
mybatis:
mapper-locations: classpath:mapper/*.xml
# 所有 pojo 别名类所在包
type-aliases-package: com.yixihan.springcloud.pojo
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author : yixihan
* @create : 2022-01-17-14:17
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Payment8004 {
public static void main(String[] args) {
SpringApplication.run (Payment8004.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 业务类
controller
package com.yixihan.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @author : yixihan
* @create : 2022-01-17-14:18
*/
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/zk")
public String paymentZk() {
return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 测试
启动后有有概率出现的问题
未配置数据库连接
服务器 Zookeeper 版本与导入的 Zookeeper 版本不一致 解决方法 :
<!-- SpringBoot整合zookeeper客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <!--先排除自带的zookeeper3.5.3--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--添加zookeeper3.4.9版本 自己 Zookeeper 的版本--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.9</version> </dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
测试接口
查看 Zookeeper 信息
查看 json 字符串
json 字符串格式化网站 (opens new window)
# 总结
服务节点是临时节点
# 服务消费者
# 建 module
module 名 : cloud-consumerzk-order80
# 改 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>springcloud-learn</artifactId>
<groupId>com.yixihan.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerzk-order80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- zookeeper -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.yixihan.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 写 YAML
server:
port: 80
spring:
application:
# 服务别名----注册 zookeeper 到注册中心名称
name: cloud-comsumer-order
cloud:
zookeeper:
# zookeeper 主机 : 端口号
connect-string: 175.24.229.41:2181
2
3
4
5
6
7
8
9
10
11
12
# 主启动
package com.yixihan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author : yixihan
* @create : 2022-01-17-18:23
*/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
public static void main(String[] args) {
SpringApplication.run (OrderZkMain80.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 业务类
config
package com.yixihan.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author : yixihan
* @create : 2022-01-17-18:23
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate () {
return new RestTemplate ();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
controller
package com.yixihan.springcloud.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @author : yixihan
* @create : 2022-01-17-18:24
*/
@RestController
@RequestMapping("/consumer")
public class OrderZkController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/payment/zk")
public String paymentInfo () {
return restTemplate.getForObject (INVOKE_URL + "/payment/zk", String.class);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 测试
测试接口
Zookeeper 控制台