Eureka 是 Netflix 开源的一个 RESTful 服务,主要用于服务注册与发现。
它由 Eureka server 和 Eureka client 组成。
Eureka server 提供服务的注册、删除、查询、续约等功能,是服务管理中心。
Eureka cliet 用来向 server 注册服务、查询服务、调用服务等。
Eureka 有三种角色:
Service Registy 服务注册中心
Service Provider 服务提供方
Service Consumer 服务消费者
网上 demo 很多,仅供参考。 史上最简单的 SpringCloud 教程 | 第二篇: 服务消费者(rest+ribbon)
服务治理的本质:服务的增删改查。
增:注册。有服务提供方向服务注册中心注册。
删:服务清除。服务注册中心将定时清除失效服务。
改:无
查:服务发现、获取。由服务消费者向服务注册中心获取服务提供方信息。
服务续约:服务提供方定时向服务注册中心发送心跳(默认 30 秒发送一次),服务提供方超过一段时间(默认 90 秒)未向服务注册中心发送心跳,则服务注册中心将服务下线。
由于数据是缓存在本地,主要就是对 registry 变量进行操作。
- private final ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> registry =new ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>();
如何构建集群?
Eureka server 还可以作为 Eureka client,指向另外一个 Eureka Server。
Eureka sever A
- spring:
- profiles: development_ha1
- application:
- name: eureka-server-ha1
- eureka:
- server:
- enable-self-preservation: false #关闭自我保护,仅在开发环境中采用此配置。
- eviction-interval-timer-in-ms: 3000
- instance:
- prefer-ip-address: true
- lease-expiration-duration-in-seconds: 10
- lease-renewal-interval-in-seconds: 3
- client:
- registry-fetch-interval-seconds: 3 # 默认为30秒
- serviceUrl:
- defaultZone: http://ziyun:dd2016@192.168.100.119:10001/eureka/
Eureka sever B
- spring:
- profiles: development_ha2
- application:
- name: eureka-server-ha2
- eureka:
- server:
- enable-self-preservation: false #关闭自我保护,仅在开发环境中采用此配置。
- eviction-interval-timer-in-ms: 3000
- instance:
- prefer-ip-address: true
- lease-expiration-duration-in-seconds: 10
- lease-renewal-interval-in-seconds: 3
- client:
- registry-fetch-interval-seconds: 3 # 默认为30秒
- serviceUrl:
- defaultZone: http://ziyun:dd2016@192.168.100.118:10001/eureka/
关键配置为
- client:
- serviceUrl:
- defaultZone: http://ziyun:dd2016@192.168.100.118:10001/eureka/
- client:
- serviceUrl:
- defaultZone: http://ziyun:dd2016@192.168.100.119:10001/eureka/
具体实现方式:接收到 Service Provider 请求的 Eureka Server,把请求再次转发到其它的 Eureka Server,调用同样的接口,传入同样的参数,除了会在 header 中标记 isReplication=true,从而避免重复的 replicate。
启动时把自己当做是 Service Consumer 服务消费者,从其它 Peer Eureka 获取(get Registry)所有服务的注册信息(即相当于获取服务列表)。然后遍历服务列表,对每个服务,在自己这里执行 Register,isReplication=true,从而完成初始化。
Eureka clients are built to handle the failure of one or more Eureka servers. Since Eureka clients have the registry cache information in them, they can operate reasonably well, even when all of the eureka servers go down.Eureka clients 将数据缓存在本地,即使所有 Eureka servers 都挂了,仍能正常运行。
Eureka Servers are resilient to other eureka peers going down. Even during a network partition between the clients and servers, the servers have built-in resiliency to prevent a large scale outage. 当其他 server 挂了,或者网络中断了, server 仍能独立工作
对于非 java 语言的服务:
官方说法:
There are some overlaps in certain areas of what Zookeeper and Eureka provide especially in the areas of replicating registry information. Eureka could use zookeeper to cache registry information and replicate the same, but replication is just a small part of what Eureka provides.
Eureka deals with various other things apart from replication:
Zookeeper's power comes to the fore with leader election, ordered updates, distributed synchronization along with its consistency guarantees (quorums).
None of the above except the replication registry really applies to Eureka to justify an other dependency that we have to deal with the following complications:
And further more, Eureka has been built carefully without any hard dependency on any external components .
另外一篇文章中也做了对比: Zookeeper 做注册中心的缺陷 。
文中主要提出了三个缺点:
当然,Peter Kelley 提出的这几个问题并不是不能克服的,并不能说明基于 ZooKeeper 就不能做好一个服务发现系统,但是我们可能有更简洁的方案比如 Eureka 来实现。
Eureka Server 的安全认证官网链接 - 老版本 在最新版本中放在了 config 中 client
如果客户端的 eureka.client.serviceUrl.defaultZone 参数值 (即 Eureka Server 的地址) 中包含 HTTP Basic Authentication 信息,如 http://user: password@localhost :8761/eureka,那么客户端就会自动使用该用户名、密码信息与 Eureka 服务端进行验证。如果你需要更复杂的验证逻辑,你必须注册一个 DiscoveryClientOptionalArgs 组件,并将 ClientFilter 组件注入,在这里定义的逻辑会在每次客户端向服务端发起请求时执行。
server 端配置:
- security:
- user:
- name: ziyun
- password: dd2016
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
client 配置
- http://user:password@EurekaHost:EurekaPort/eureka/
- eureka:
- client:
- serviceUrl:
- defaultZone: http://ziyun:dd2016@eureka-server:10001/eureka/
note: 密码不能带特殊符号!!!
提示信息如下: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.
解决方案:
- 1、 注册中心关闭自我保护机制,修改检查失效服务的时间。
- eureka:
- server:
- enable-self-preservation: false
- eviction-interval-timer-in-ms: 3000
- 2、 微服务修改减短服务心跳的时间。
- # 默认90秒
- lease-expiration-duration-in-seconds: 10
- # 默认30秒
- lease-renewal-interval-in-seconds: 3
- eureka:
- server:
- enable-self-preservation: false #关闭自我保护,仅在开发环境中采用此配置。
- eviction-interval-timer-in-ms: 3000 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
- instance:
- prefer-ip-address: true
- lease-expiration-duration-in-seconds: 10 # 发呆时间,即服务续约到期时间(缺省为90s)
- lease-renewal-interval-in-seconds: 3 # 心跳时间,即服务续约间隔时间(缺省为30s)
- client:
- registry-fetch-interval-seconds: 3 # 默认为30秒
产生原因:Eureka Server 在运行期间,会统计心跳失败的比例在 15 分钟之内是否低于 85%,如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server 会将当前的实例注册信息保护起来,同时提示这个警告。保护模式主要用于一组客户端和 Eureka Server 之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server 将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。
另外在 Camden.SR3 中可以配置 Ribbon 请求重试,可以参考 DD 大神的新作: 为 Spring Cloud Ribbon 配置请求重试(Camden.SR2+)
- ZoneAwareLoadBalancer < Server > lb = (ZoneAwareLoadBalancer < Server > ) springClientFactory.getLoadBalancer("CLOUD-SERVICE");
- Server server = lb.chooseServer();
- System.out.println("error->" + server.getHostPort());
- lb.markServerDown(server);
默认的心跳实现方式可以有效的检查 eureka 客户端进程是否正常运作,但是无法保证客户端应用能够正常提供服务。
由于大多数微服务应用都会有一些其他的外部资源依赖,比如数据库,REDIS 缓存等,如果我们的应用与这些外部资源无法连通的时候,实际上已经不能提供正常的对外服务了,但因为客户端心跳依然在运行,所以它还是会被服务消费者调用,而这样的调用实际上并不能获得预期的后果。
我们可以通过在 eureka 客户端中配置: eureka.client.healthcheck.enabled=true,就可以改变 eureka server 对客户端健康检测的方式,改用 actuator 的 / health 端点来检测。
改变 eureka server 中注册的服务的健康检测方式 Spring Cloud 实战小贴士:健康检查SpringCloud 提供了 3 个服务发现组件:Eureka/Consul/Zookeeper。在摩拜公开的 SpringCloud 实践的 PPT 中,采用了是 Consul。
来源: http://www.bubuko.com/infodetail-2445333.html