服务的注册和发现+Eureka实践过程的一些问题及解决

服务的注册和发现的概念

分享几篇我认为的服务发现讲的好的文章:
小白科普:服务那点事儿
浅谈服务发现

基于ZooKeeper的:
什么是Zookeeper?
服务注册与发现

Eureka实践过程的一些问题及解决

问题一:注册服务下线后反应时间慢

问题描述:用Eureka做服务的注册和发现,有一个奇怪的现象:有些服务停止后(如图中的A-BOOTIFUL-CLIENT-2服务),该服务还会在Eureka上显示一段时间,需要过一段时间在更新该服务才会在Eureka上消失。
这里写图片描述

原因:这是由于Eureka的Fetch Registry,解释原因为:

Eureka clients fetches the registry information from the server and caches it locally. After that, the clients use that information to find other services. This information is updated periodically (every 30 seconds) by getting the delta updates between the last fetch cycle and the current one. The delta information is held longer (for about 3 mins) in the server, hence the delta fetches may return the same instances again. The Eureka client automatically handles the duplicate information.
After getting the deltas, Eureka client reconciles the information with the server by comparing the instance counts returned by the server and if the information does not match for some reason, the whole registry information is fetched again. Eureka server caches the compressed payload of the deltas, whole registry and also per application as well as the uncompressed information of the same. The payload also supports both JSON/XML formats. Eureka client gets the information in compressed JSON format using jersey apache client.

问题二:结合Spring Cloud随机端口功能使用时,Eureka的实例名端口号与服务端口号不对应

问题描述:设置server.port=0,当应用启动的时候会自动的分配一个随机端口,但是该方式在注册到Eureka的时候会一个问题:所有实例都使用了同样的实例名(如:Lenovo-zhaiyc:hello-service:0),这导致只出现了一个实例。所以,我们还需要修改实例ID的定义,让每个实例的ID不同。(http://blog.didispace.com/spring-cloud-tips-2/)
上文给出了两种解决问题的方法,可以解决问题,但是这两种方法都存在一个需要注意的地方:服务的端口号和eureka的注册信息不对应。具体情况如下:

给服务随机分配的端口号为10856和12172:
实际服务端口号

但是在eureka注册的服务显示的端口号却是再次生成的随机数17958和16359:
端口号不对应

git上有关于这个问题的讨论
Document setting eureka instance id when running multiple local apps use ‘server.port=0’
issue : setting random port for eureka client within a scope
开发人员的解释是”The trouble is, we don’t know the port until the container has given it to spring boot, much later than when eureka needs it”,代码层面解决这个问题比较困难,他们只准备在文档进行说明。

猜你喜欢

转载自blog.csdn.net/sinat_34763749/article/details/80997070