您的位置:首页 > 编程语言 > Java开发

SpringCloud教程 | 二.Eureka常见问题总结

2018-01-17 10:27 330 查看
指定Eureka的Environment

eureka.environment: 指定环境
如果不配置默认的是test,也可以通过以下来设置成dev
eureka.environment=dev
参考文档:https://github.com/Netflix/eureka/wiki/Configuring-Eureka

原文部分描述如下:

The easiest way to configure Eureka client is by using property files. By default, Eureka client searches for the property file eureka-client.properties in the classpath. It further
searches for environment specific overrides in the environment specific properties files. The environment is typically test or prod and is supplied by a -Deureka.environment java commandline switch to the eureka client (without the .properties suffix). Accordingly,
the client also searches for eureka-client-{test,prod}.properties.

大致的意思Eureka默认会搜索eureka-client.properties,如果通过命令行(java命令行属性-Deureka.environment=eureka-client-{test,prod} ,注意此处没有.properties后缀)设置了环境,则会对应搜索eureka-client-{test,prod}.properties来替换默认参数。

指定Eureka的DataCenter

eureka.datacenter: 指定数据中心
参考文档:https://github.com/Netflix/eureka/wiki/Configuring-Eureka

原文:

If you are running in the cloud environment, you will need to pass in the java commandline property -Deureka.datacenter=cloud so that the Eureka Client/Server knows to initialize
the information specific to AWS cloud.

文中大致意思是,通过Java命令行配置-Deureka.datacenter=cloud,这样eureka将会知道是在AWS云上。

如何解决Eureka注册服务慢的问题

eureka.instance.leaseRenewalIntervalInSeconds
参考文档:http://cloud.spring.io/spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service

原文:

Why is it so Slow to Register a Service?

Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata
in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick
with the default because there are some computations internally in the server that make assumptions about the lease renewal period.
翻译:

作为实例还涉及到与注册中心的周期性心跳,默认持续时间为30秒(通过serviceUrl)。在实例、服务器、客户端都在本地缓存中具有相同的元数据之前,服务不可用于客户端发现(所以可能需要3次心跳)。你可以使用eureka.instance.leaseRenewalIntervalInSeconds 配置,这将加快客户端连接到其他服务的过程。在生产中,最好坚持使用默认值,因为在服务器内部有一些计算,他们对续约做出假设。

Eureka的自我保护模式

如果在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 Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。

原因:自我保护机制。Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告。

解决方案:

Eurake有一个配置参数eureka.server.renewalPercentThreshold,定义了renews 和renews threshold的比值,默认值为0.85。当server在15分钟内,比值低于percent,即少了15%的微服务心跳,server会进入自我保护状态,Self-Preservation。在此状态下,server不会删除注册信息,这就有可能导致在调用微服务时,实际上服务并不存在。

这种保护状态实际上是考虑了client和server之间的心跳是因为网络问题,而非服务本身问题,不能简单的删除注册信息

stackoverflow上,有人给出的建议是:

1、在生产上可以开自注册,部署两个server

2、在本机器上测试的时候,可以把比值调低,比如0.49

3、或者简单粗暴把自我保护模式关闭

eureka.server.enableSelfPreservation=false
详见:https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication

如何解决Eureka Server不踢出已关停的节点的问题
在开发过程中,我们常常希望Eureka Server能够迅速有效地踢出已关停的节点,但是新手由于Eureka自我保护模式,以及心跳周期长的原因,常常会遇到Eureka Server不踢出已关停的节点的问题。解决方法如下:

(1) Eureka Server端:配置关闭自我保护,并按需配置Eureka Server清理无效节点的时间间隔。

eureka.server.enable-self-preservation			# 设为false,关闭自我保护
eureka.server.eviction-interval-timer-in-ms     # 清理间隔(单位毫秒,默认是60*1000)

(2) Eureka Client端:配置开启健康检查,并按需配置续约更新时间和到期时间。

eureka.client.healthcheck.enabled			# 开启健康检查(需要spring-boot-starter-actuator依赖)
eureka.instance.lease-renewal-interval-in-seconds		# 续约更新时间间隔(默认30秒)
eureka.instance.lease-expiration-duration-in-seconds 	# 续约到期时间(默认90秒)

示例:

服务器端配置:

eureka:
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 4000
客户端配置:
eureka:
client:
healthcheck:
enabled: true
instance:
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 10
注意:

更改Eureka更新频率将打破服务器的自我保护功能,生产环境下不建议自定义这些配置。
详见:https://github.com/spring-cloud/spring-cloud-netflix/issues/373

自定义Eureka的Instance ID
在Spring Cloud中,服务的Instance ID的默认值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}} ,也就是机器主机名:应用名称:应用端口 。因此在Eureka Server首页中看到的服务的信息类似如下:itmuch:microservice-provider-user:8000
。如果想要自定义这部分的信息怎么办?

示例:

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}        # 将Instance ID设置成IP:端口的形式



Eureka配置最佳实践参考

https://github.com/spring-cloud/spring-cloud-netflix/issues/203


注意点:eureka.client.healthcheck.enabled=true配置项必须设置在application.yml中

eureka.client.healthcheck.enabled=true
 只应该在application.yml中设置。如果设置在bootstrap.yml中将会导致一些不良的副作用,例如在Eureka中注册的应用名称是UNKNOWN等。

参考资料:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: