eureka开篇
一、前言
In AWS cloud, because of its inherent nature, servers come and go. Unlike the traditional load balancers which work with servers with well known IP addresses and host names, in AWS, load balancing requires much more sophistication in registering and de-registering servers with load balancer on the fly. Since AWS does not yet provide a middle tier load balancer, Eureka fills a big gap in the area of mid-tier load balancing.
为什么我们需要eureka。服务发现早就不是一个新鲜词了,早在SOA推行的时候就有服务发现的思路。这几年由于互联网的发展,越来越多的公司选择云作为替代IDC的选择。服务的动态扩容变得更加简单和方便,因此对服务发现的需求就愈发强烈。
注:遗憾的是eureka的2.0版本已经不再继续维护,这意味着很多公司也要开始寻找新的替代方案了
二、源码下载运行
git clone https://github.com/Netflix/eureka.git
我使用的版本是1.4.x
如何运行demo参考:
https://github.com/Netflix/eureka/wiki/Running-the-Demo-Application
三、协议分析
我本地安装了wireshark进行抓包,方便对整个通讯的流程进行分析。
一共起了3个DEMO进程
- ExampleEurekaService。eureka客户端,对外提供服务。
- ExampleEurekaClient。eureka客户端,请求ExampleEurekaService。
- eureka server。eureka服务端,维护接入eureka的服务列表。
通讯协议使用http协议。
image.png
ExampleEurekaService会定时请求eureka server。
- 定时上报服务心跳包
Hypertext Transfer Protocol
PUT /eureka/v2/apps/SAMPLEREGISTERINGSERVICE/201709-07262?status=UP&lastDirtyTimestamp=1552742035025 HTTP/1.1\r\n
DiscoveryIdentity-Name: DefaultClient\r\n
DiscoveryIdentity-Version: 1.4\r\n
DiscoveryIdentity-Id: 172.19.10.230\r\n
Accept-Encoding: gzip\r\n
Content-Length: 0\r\n
Host: localhost:8080\r\n
Connection: Keep-Alive\r\n
User-Agent: Java-EurekaClient/v<version_unknown>\r\n
\r\n
[Full request URI: http://localhost:8080/eureka/v2/apps/SAMPLEREGISTERINGSERVICE/201709-07262?status=UP&lastDirtyTimestamp=1552742035025]
[HTTP request 1/1]
[Response in frame: 7296]
- 获取eureka server的app列表对应的哈希值。通过源码我们可以知道,eureka client会定时把app列表拉到本地内存,考虑到绝大部分的场景下其实不会发生变更,这里会先拉取一次哈希值,如果哈希值发生变更了才会进一步去拉取整个app列表。
Hypertext Transfer Protocol
GET /eureka/v2/apps/delta HTTP/1.1\r\n
Accept: application/json\r\n
DiscoveryIdentity-Name: DefaultClient\r\n
DiscoveryIdentity-Version: 1.4\r\n
DiscoveryIdentity-Id: 172.19.10.230\r\n
Accept-Encoding: gzip\r\n
Host: localhost:8080\r\n
Connection: Keep-Alive\r\n
User-Agent: Java-EurekaClient/v<version_unknown>\r\n
\r\n
[Full request URI: http://localhost:8080/eureka/v2/apps/delta]
[HTTP request 1/1]
[Response in frame: 7297]
另外通过抓包我们可以发现,eureka client这个定时任务的时间间隔为30s。
to be continue...