Spring Cloud里的服务提供者如何与注册中心进行通信

5796101-ea387b7770fe90b3.png
image.png

从这张图我们知道,无论是服务提供者还是服务的调用者,他们都需要与Eureka服务器进行通信。他们之间是如何进行消息传递的呢?通过wireshark抓包工具可以很清楚的看到他们之间的通讯信息。步骤如下:
1:让wireshark能够抓取本机的包,默认就无法抓取本机的包的,方法在 https://www.jianshu.com/p/486d1c063861 这篇文章里有介绍。

2:通过 src host 本机ip && dst host 本机ip对抓到的包进行过滤。抓到包的截图如下:


Spring Cloud里的服务提供者如何与注册中心进行通信_第1张图片
image.png

从图中我们至少能很直观的得到两个信息:

  • 默认情况下服务提供者与Eureka Server通信的时间跨度是30秒
  • Eureka Server的API设计成了REST风格,主要的接口有图中圈出来的三个,请求的方法分别为GET, POST, PUT,

GET请求接口(用于得到注册中心所有的服务提供者)

Get接口用于请求注册中心目前有那些服务提供者,请求的接口url如下图所示:


Spring Cloud里的服务提供者如何与注册中心进行通信_第2张图片
image.png

返回的结果是json格式的数据,数据信息如下:

{
    "applications": {
        "versions__delta": "1",
        "apps__hashcode": "UP_2_",
        "application": [
            {
                "name": "CONSUMER",
                "instance": [
                    {
                        "instanceId": "021ZJ1705.synacast.local:consumer:9000",
                        "hostName": "10.200.121.41",
                        "app": "CONSUMER",
                        "ipAddr": "10.200.121.41",
                        "status": "UP",
                        "overriddenstatus": "UNKNOWN",
                        "port": {
                            "$": 9000,
                            "@enabled": "true"
                        },
                        "securePort": {
                            "$": 443,
                            "@enabled": "false"
                        },
                        "countryId": 1,
                        "dataCenterInfo": {
                            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
                            "name": "MyOwn"
                        },
                        "leaseInfo": {
                            "renewalIntervalInSecs": 30,
                            "durationInSecs": 90,
                            "registrationTimestamp": 1530688491242,
                            "lastRenewalTimestamp": 1530689331391,
                            "evictionTimestamp": 0,
                            "serviceUpTimestamp": 1530688490719
                        },
                        "metadata": {
                            "management.port": "9000",
                            "jmx.port": "64966"
                        },
                        "homePageUrl": "http://10.200.121.41:9000/",
                        "statusPageUrl": "http://10.200.121.41:9000/info",
                        "healthCheckUrl": "http://10.200.121.41:9000/health",
                        "vipAddress": "consumer",
                        "secureVipAddress": "consumer",
                        "isCoordinatingDiscoveryServer": "false",
                        "lastUpdatedTimestamp": "1530688491242",
                        "lastDirtyTimestamp": "1530688490594",
                        "actionType": "ADDED"
                    }
                ]
            },
            {
                "name": "PROVIDER",
                "instance": [
                    {
                        "instanceId": "021ZJ1705.synacast.local:provider:8000",
                        "hostName": "10.200.121.41",
                        "app": "PROVIDER",
                        "ipAddr": "10.200.121.41",
                        "status": "UP",
                        "overriddenstatus": "UNKNOWN",
                        "port": {
                            "$": 8000,
                            "@enabled": "true"
                        },
                        "securePort": {
                            "$": 443,
                            "@enabled": "false"
                        },
                        "countryId": 1,
                        "dataCenterInfo": {
                            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
                            "name": "MyOwn"
                        },
                        "leaseInfo": {
                            "renewalIntervalInSecs": 30,
                            "durationInSecs": 90,
                            "registrationTimestamp": 1530686242737,
                            "lastRenewalTimestamp": 1530689333558,
                            "evictionTimestamp": 0,
                            "serviceUpTimestamp": 1530686242111
                        },
                        "metadata": {
                            "management.port": "8000",
                            "jmx.port": "63616"
                        },
                        "homePageUrl": "http://10.200.121.41:8000/",
                        "statusPageUrl": "http://10.200.121.41:8000/info",
                        "healthCheckUrl": "http://10.200.121.41:8000/health",
                        "vipAddress": "provider",
                        "secureVipAddress": "provider",
                        "isCoordinatingDiscoveryServer": "false",
                        "lastUpdatedTimestamp": "1530686242737",
                        "lastDirtyTimestamp": "1530686241987",
                        "actionType": "ADDED"
                    }
                ]
            }
        ]
    }
}

注:如果Eureka客户端每次请求都取得所有服务提供者的所有信息,会给Eureka Server服务器带来访问压力。Eureka服务器提供了增量获取数据的接口,接口信息如下图所示:


Spring Cloud里的服务提供者如何与注册中心进行通信_第3张图片
image.png

Post请求接口

用于提交系统当前服务信息到Eureak Server,请求的url信息如下图:


Spring Cloud里的服务提供者如何与注册中心进行通信_第4张图片
image.png

提交的json结构如下:

{
    "instance": {
        "instanceId": "021ZJ1705.synacast.local:consumer:9000",
        "hostName": "10.200.121.41",
        "app": "CONSUMER",
        "ipAddr": "10.200.121.41",
        "status": "UP",
        "overriddenstatus": "UNKNOWN",
        "port": {
            "$": 9000,
            "@enabled": "true"
        },
        "securePort": {
            "$": 443,
            "@enabled": "false"
        },
        "countryId": 1,
        "dataCenterInfo": {
            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
            "name": "MyOwn"
        },
        "leaseInfo": {
            "renewalIntervalInSecs": 30,
            "durationInSecs": 90,
            "registrationTimestamp": 0,
            "lastRenewalTimestamp": 0,
            "evictionTimestamp": 0,
            "serviceUpTimestamp": 0
        },
        "metadata": {
            "management.port": "9000",
            "jmx.port": "64966"
        },
        "homePageUrl": "http://10.200.121.41:9000/",
        "statusPageUrl": "http://10.200.121.41:9000/info",
        "healthCheckUrl": "http://10.200.121.41:9000/health",
        "vipAddress": "consumer",
        "secureVipAddress": "consumer",
        "isCoordinatingDiscoveryServer": "false",
        "lastUpdatedTimestamp": "1530688489595",
        "lastDirtyTimestamp": "1530688490594"
    }
}

Put方法请求

Put方法请求用于维持服务提供者与Eureka Server的心跳服务,请求的信息如下图:


Spring Cloud里的服务提供者如何与注册中心进行通信_第5张图片
image.png

请求的URL 里包括 application.name信息,服务的URL,状态信息与最后的更新时间。

你可能感兴趣的:(Spring Cloud里的服务提供者如何与注册中心进行通信)