Eureka是什么
Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,于定位服务,以实现云端中间层服务发现和故障转移。服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,不要修改服务调用的配置文件了。功能类似于dubbo的注册中心,比如Zookeeper;
原理
Eureka的基本架构
Spring Cloud封装了Netflix 公司开发的Eureka模块来实现服务注册和发现(请对比Zookeeper);
Eureka采用了C-S的设计架构。Eureka Server作为服务注册功能的服务器,它是服务注册中心;
而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。SpringCloud的一些其他模块(比如Zuul) 就可以通过Eureka Server来发现系统中的其他微服务,并执行相关的逻辑;
Eureka包含两个组件: Eyreka Server和Eureka Client
Eyreka Server
Eureka Server提供服务注册服务各个节点启动后,会在Eureka Server中进行注册,这样Eureka Server中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到;
Eureka Client
EurekaClient是一个ava客户瑞,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒);
构建步骤
microservicecloud-eureka-7001 eureka服务注册中心Module
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>springcloud</artifactId>
<groupId>com.javax</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-eureka-7001</artifactId>
<dependencies>
<!-- eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml
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/
EurekaServer7001_App
package com.javax.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //EnableServer服务器端启动类,接受其他微服务注册进来
public class EurekaServer7001_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001_App.class,args);
}
}
测试
No application available没有服务被发现,因为没有注册服务233;
microservicecloud-provider-dept-8001 将已有的部门微服务注册进eureka服务中心
pom.xml(修改部分)
<!-- 将微服务provider注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<!--<version>2.1.3.RELEASE</version>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<!--<version>2.1.3.RELEASE</version>-->
</dependency>
application.yml(修改部分)
eureka:
#客户端注册进eureka服务列表内
client:
service-url:
defaultZone: http://localhost:7001/eureka
DeptProvider8001_App(修改部分)
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
测试
启动7001,8001;
actuator与注册微服务信息完善
主机名称:服务名称修改
修改microservicecloud-provider-dept-8001的application.yml文件
eureka:
instance:
#自定义服务名称信息
instance-id: microservicecloud-dept8001
改之前
改之后
访问信息有IP信息提示
修改microservicecloud-provider-dept-8001的application.yml文件
eureka:
instance:
#访问路径可以显示IP地址
prefer-ip-address: true
微服务info内容详细信息
修改microservicecloud-provider-dept-8001的pom.xml文件
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
总的父工程springcloud修改pom.xml添加构建build信息
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<configuration>
<!-- 报错就算了,emmm -->
<delimiters>
<delimiter>$</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
修改microservicecloud-provider-dept-8001的application.yml文件
info:
app.name: springcloud-microservicecloud
#公司名
company.name: springcloud
build.artifactId: $project.artifactId$
build.version: $project.version$
自我保护(不建议关闭)
定义
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳, EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了一因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过 “ 自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丟过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式;
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话讲解:好死不如赖活着;
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也坏盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、 稳定;
在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false禁用自我保护模式;
导致原因:某时刻某一个微服务不可用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存;
eureka:
server:
#禁用自我保护
enable-self-preservation: false
microservicecloud-provider-dept-8001服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
microservicecloud-provider-dept-8001工程的DeptController(修改)
import org.springframework.cloud.client.discovery.DiscoveryClient;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/discovery")
public Object discovery(){
List<String> list = discoveryClient.getServices();
System.out.println("*********" + list);
List<ServiceInstance> srvList = discoveryClient.getInstances("MICROSERVICECLOUD-DEPT");
for (ServiceInstance element : srvList) {
System.out.println(element.getServiceId() + "\t" +element.getHost() + "\t" + element.getPort() + "\t" + element.getUri());
}
return this.discoveryClient;
}
DeptProvider8001_App(修改)
@EnableDiscoveryClient //服务发现
测试
启动7001,8001。访问:http://localhost:8001/dept/discovery
查看日志
修改microservicecloud-consumer-dept-80的DeptController_Consumer
//测试@EnableDiscoveryClient,消费端可以调用服务发现
@GetMapping("/discovery")
public Object discovery(){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery",Object.class);
}
测试
启动7001、8001、80。访问:http://localhost:80/consumer/dept/discovery
查看日志
集群配置
新建microservicecloud-eureka-7002、microservicecloud-eureka-7003
按照7001为模板粘贴POM
<dependencies>
<!-- eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
修改7002和7003的主启动类
package com.javax.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //EnableServer服务器端启动类,接受其他微服务注册进来
public class EurekaServer7002_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7002_App.class,args);
}
}
修改映射配置,找到C:\Windows\System32\drivers\etc路径下的hosts文件(备份),添加下面代码;
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
修改7001,7002,7003的application.yml配置
server:
#7002,7003
port: 7001
eureka:
instance:
#eureka服务端的实例名称
#eureka7002.com,eureka7003.com
hostname: eureka7001.com
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端技术注册中心,负责维护服务实例,并不需要检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
#单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
#7002: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
#7003: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
修改8001的application.yml配置
eureka:
#客户端注册进eureka服务列表内
client:
service-url:
# 单机:defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
测试
启动7001,7002,7003,8001。访问:http://eureka7001.com:7001/,http://eureka7002.com:7002/,http://eureka7003.com:7003/
作为服务注册中心,Eureka比Zookeeper好在哪里
作为服务注册中心,Eureka比Zookeeper好在哪里著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A(可用性)和P(分区容错性)。 由于分区容错性P在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡。因此Zookeeper保证的是CP,Eureka则是AP;
Zookeeper保证CP
当向注册中心查询服务列表时,我们可以容忍注册中心返回的是几分钟以前的注册信息, 但不能接受服务直接down掉不可用。也就是说,服务注册功能对可用性的要求要高于一致性。但是zk会出现这样一种情况,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行leader选举。问题在于,选举leader的时间太长,30 ~ 120s,且选举期间整个zk集群都是不可用的,这就导致在选举期间注册服务瘫痪。在云部署的环境下,因网络问题使得zk集群失去master节点是较大概率会发生的事,虽然服务能够最终恢复,但是漫长的选举时间导致的注册长期不可用是不能容忍的;
Eureka保证AP
Eureka看明白了这一点,因此在设计时就优先保证可用性。Eureka各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。 除此之外,Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现以下几种情况:
- Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务;
- Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用);
- 当网络稳定时,当前实例新的注册信息会被同步到其它节点中;
结论
因此,Eureka可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪。