一、Eureka基础知识

1.1、什么是服务治理

Spring Cloud 封装了Netflix公司开发的Eureka模块来实现服务治理

在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。

1.2、什么是服务注册

​ Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。

​ 在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))

img

1.3、Eureka两组件

Eureka包括两个组件:Eureka Server和Eureka Client,其中

  • Eureka Server提供服务注册服务,各个微服务节点通过配置启动后,会在Eureka Server中进行注册,这样Eureka Server中的服务注册表将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
  • Eureka Client通过服务注册中心访问,是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(Round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认时间周期为30s)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从注册服务表中把这个服务节点移除(默认为90s)。

二、单击Eureka构建步骤

2.1、创建cloud-eureka-server-7001

这个微服务就是eureka Server服务注册中心,为其他微服务提供服务注册。

2.2、引入依赖

1
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
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>com.cloudstudy</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<!-- 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.springframework.boot/spring-boot-starter-test -->
<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>

Eureka 1.x和Eureka 2.x的区别

  • Eureka 1.x 老版本,依赖中不分服务端和客户端
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
  • Eureka 2.x 新版本
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.3、编写yml配置文件

1
2
3
4
5
6
7
8
9
10
11
12
erver:
port: 7001
eureka:
instance:
hostname: eureka7001.com
client:
# 表示不向服务注册中心注册自己
register-with-eureka: false
# 表示自己就是服务注册中心,职责是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

2.4、主启动类

在主启动类上添加@EnableEurekaServer注解,表示这个微服务为服务注册中心。

1
2
3
4
5
6
7
8
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001.class,args);
System.out.println("http://localhost:7001");
}
}

2.5、启动eureka7001

image-20210214171751846

2.6、修改payment8001和order80

引入eureka Client依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

在yml配置文件中添加配置

1
2
3
4
5
6
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka

在payment8001、order80主启动类上添加注解

1
@EnableEurekaClient

启动payment8001、order80

image-20210214173444635

Eureka Server列表中会显示已经注册入注册中心的微服务的spring.application.name

image-20210214173637695

三、集群Eureka构建步骤

3.1、单机eureka存在的问题

微服务RPC远程服务调用最核心的是什么?

  • 高可用,如果注册中心为单机,那么一旦它出现故障,就会导致整个系统不可用。

解决方法

  • 搭建Eureka注册中心集群,实现负载均衡+故障容错

img

3.2、eureka集群的实质

互相注册,相互守望。

集群中的每一台eureka Server要有除自己之外的所有eureka server的信息。

3.3、新建Eureka 7002微服务

修改本地hosts文件,添加域名和ip映射

1
2
3
# My hosts
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

由于依赖、主启动类与7001类似,所以这里只给出yml配置文件

此时需要修改一下7001的配置文件

  • 7001
1
2
3
4
5
6
7
8
9
10
11
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
#集群时7001需要注册到7002
defaultZone: http://eureka7002.com:7002/eureka/
  • 7002
1
2
3
4
5
6
7
8
9
10
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址

3.4、启动7001和7002,测试

image-20210214201206201

3.5、将payment8001和order80注册到eureka集群

修改payment8001和order80的yml文件

  • payment8001
1
2
3
4
5
6
7
8
9
10
11
server:
port: 8001
eureka:
client:
register-with-eureka: true #表识不向注册中心注册自己
fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
spring:
application:
name: cloud-provider-hystrix-payment
  • order80
1
2
3
4
5
6
7
8
9
10
11
server:
port: 80
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
spring:
application:
name: cloud-order-service

测试

image-20210214210219113

四、actuator微服务信息完善

4.1、主机名,服务名称修改

1、当前问题

eureka注册表中含有主机名称

image-20210214211411127

2、修改payment8001

修改payment8001微服务的yml配置文件,添加配置如下

1
2
3
eureka:
instance:
instance-id: payment8001

重启,查看结果

image-20210214212222852

4.2、访问信息有IP信息提示

1、遇到的问题

在eureka点击微服务时没有ip提示

2、添加配置

1
2
3
eureka:
instance:
prefer-ip-address: true

3、测试

image-20210214212622010

4.3、总结

修改主机名、添加IP信息提示的步骤如下:

1、引入依赖

1
2
3
4
5
6
7
8
9
<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>

2、yml文件添加配置

1
2
3
4
5
eureka:
instance:
instance-id: 服务名称
# 开启IP信息提示
prefer-ip-address: true

五、服务发现Discovery

5.1、概述

对于注册入Eureka中的微服务,可以通过服务发现来获得该服务的信息。

5.2、使用

这里以payment8001为例

1、修改payment8001的Controller

注入一个DiscoveryClient对象

1
2
@Autowired
private DiscoveryClient discoveryClient;

在Controller中添加一个方法,这个方法用户获取微服务的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@GetMapping("/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
services.stream().forEach(service -> {
log.info("-----------element:" + service);
});

//在getInstances方法中输入注册到eureka中的微服务名称
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
instances.stream().forEach(instance -> {
log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
});
return this.discoveryClient;
}

2、在payment8001主启动类上添加注解

1
@EnableDiscoveryClient

3、重启payment8001

访问 http://localhost:8001/payment/discovery

image-20210214214946293

image-20210214215324991

六、Eureka自我保护

6.1、概述

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何服务

即:某时刻某一个微服务不可用了,Eureka不会立即清理,依旧会对该微服务的信息进行保存。

  • 如果在Eureka Server首页中看到以下这段提示,则说明Eureka进入了保护模式

image-20210214220118806

6.2、导致原因

1、为什么会产生Eureka自我保护机制?

为了防止EurekaClient可以正常运行,但是与EurekaServer网络不通情况下,Eurekaserver不会立刻将EurekaClient服务剔除

2、什么是自我保护模式

​ 默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但当网络分区故障发生(延时、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。

img

6.3、禁止自我保护

以eureka7001和payment8001举例说明

1、修改eureka7001的配置文件

禁用自我保护模式

1
2
3
4
eureka:
# 关闭自我保护机制
server:
enable-self-preservation: false

重启7001,查看结果

image-20210214222939563

2、修改payment8001的配置文件

设置eureka客户端向服务端发送心跳的时间间隔

1
2
3
4
5
6
eureka:
instance:
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认为10s)
lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为s,超时直接剔除服务
lease-expiration-duration-in-seconds: 2

重启payment8001,查看结果

image-20210214223337465

关闭payment8001,查看结果

image-20210214223531487

可以看到,在禁用自我保护模式的情况下,关闭payment8001后服务被剔除