微服务架构中,Eureka配置常见问题大全

1.什么是Eureka(服务发现框架)

     Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。Eureka包含两个组件:Eureka Server和Eureka Client。Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。

Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

2.Eureka在application.yml中的配置

      既然Eureka能提供我们基本的负载均衡功能,而且支持服务实例的基本轮询调度查找,追随Consul,Zookeeper等系统趋势,但却没有尝试成为一个通用配置存储,而且提供了基于REST的接口,所以可以见得Eureka在服务中的配置现的尤为重要,但也经常会犯错而导致,服务注册与发现有可以失败。好了直接总结常见问题

  • 配置Eureka的Environment(环境)                    eureka.environment: 指定环境
  • 配置Eureka的DataCenter(数据中心)               eureka.datacenter: 指定数据中心

           原文: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.                  文中指出,配置-Deureka.datacenter=cloud,这样eureka将会知道是在AWS云上。

  • 解决Eureka注册服务缓慢的问题                     eureka.instance.leaseRenewalIntervalInSeconds(默认30秒)

           原文: 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 Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。

  • 解决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更新频率将打破服务器的自我保护功能,生产环境下不建议自定义这些配置。 

  • 自定义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 。如果想要自定义这部分的信息怎么办?那就是将我们的Instance ID修改为IP:端口形式,但是这一点又是不利于spring security做安全配置验证的

3.总结

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值