记录:Eureka 学习笔记

Eureka 是一个基于 REST(REpresentational State Transfer)
的服务,用于定位服务,以实现云端中间层服务器的负载均衡和故障转移。

Eureka 服务续约(心跳机制)

Eureka Client的应用启动时,在com.netflix.discovery.DiscoveryClient类的initScheduledTasks方法中,会做以下几件事:

  1. 周期性更新服务列表;
  2. 周期性服务续约;
  3. 服务注册逻辑;

API

1、服务注册

接口地址: /eureka/apps/appID
调用方式:POST

例:
http://localhost:8761/eureka/apps/HELLO-SPRING-CLOUD-CONSUMER

参数示列:

{
	"instance": {
		"instanceId": "192.168.2.5:hello-spring-cloud-consumer:8762",
		"app": "HELLO-SPRING-CLOUD-CONSUMER",
		"appGroutName": null,
		"ipAddr": "127.0.0.1",
		"sid": "na",
		"homePageUrl": null,
		"statusPageUrl": null,
		"healthCheckUrl": null,
		"secureHealthCheckUrl": null,
		"vipAddress": "demo-order2",
		"secureVipAddress": "demo-order2",
		"countryId": 1,
		"dataCenterInfo": {
			"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
			"name": "MyOwn"
		},
		"hostName": "127.0.0.1",
		"status": "UP",
		"leaseInfo": null,
		"isCoordinatingDiscoveryServer": false,
		"lastUpdatedTimestamp": 1529391461000,
		"lastDirtyTimestamp": 1529391461000,
		"actionType": null,
		"asgName": null,
		"overridden_status": "UNKNOWN",
		"port": {
			"$": 11102,
			"@enabled": "false"
		},
		"securePort": {
			"$": 7002,
			"@enabled": "false"
		},
		"metadata": {
			"@class": "java.util.Collections$EmptyMap"
		}
	}
}

2、发送心跳

接口地址: /eureka/apps/appID/instanceID
调用方式:PUT
接口说明:发送心跳,保持客户端在线。

Eureka Client 会每隔 30 秒发送一次心跳来续约。 通过续约来告知 Eureka Server 该 Eureka Client 运行正常,没有出现问题。 默认情况下,如果 Eureka Server 在 90 秒内没有收到 Eureka Client 的续约,Server 端会将实例从其注册表中删除

例:

http://localhost:8761/eureka/apps/HELLO-SPRING-CLOUD-CONSUMER/192.168.2.5:hello-spring-cloud-consumer:8762

3、服务下线

接口地址: /eureka/apps/appID/instanceID
调用方式:DELETE
接口说明:下次心跳又会注册上来。

例:
http://localhost:8761/eureka/apps/HELLO-SPRING-CLOUD-CONSUMER/192.168.2.5:hello-spring-cloud-consumer:8762

4、服务列表查询

接口地址: /eureka/apps
调用方式:GET

例:
http://localhost:8761/eureka/apps

设置返回数据类型:
Content-Type:application/json
Accept:application/json

返回示列:

{
    "applications": {
        "versions__delta": "1",
        "apps__hashcode": "UP_1_",
        "application": [
            {
                "name": "HELLO-SPRING-CLOUD-PRODUCER",
                "instance": [
                    {
                        "instanceId": "192.168.2.5:hello-spring-cloud-producer:8763",
                        "hostName": "192.168.2.5",
                        "app": "HELLO-SPRING-CLOUD-PRODUCER",
                        "ipAddr": "192.168.2.5",
                        "status": "UP",
                        "overriddenStatus": "UNKNOWN",
                        "port": {
                            "$": 8763,
                            "@enabled": "true"
                        },
                        "securePort": {
                            "$": 443,
                            "@enabled": "false"
                        },
                        "countryId": 1,
                        "dataCenterInfo": {
                            "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
                            "name": "MyOwn"
                        },
                        "leaseInfo": {
                            "renewalIntervalInSecs": 30,
                            "durationInSecs": 90,
                            "registrationTimestamp": 1639123544298,
                            "lastRenewalTimestamp": 1639128887758,
                            "evictionTimestamp": 0,
                            "serviceUpTimestamp": 1639123543236
                        },
                        "metadata": {
                            "management.port": "8763",
                            "jmx.port": "50877"
                        },
                        "homePageUrl": "http://192.168.2.5:8763/",
                        "statusPageUrl": "http://192.168.2.5:8763/actuator/info",
                        "healthCheckUrl": "http://192.168.2.5:8763/actuator/health",
                        "vipAddress": "hello-spring-cloud-producer",
                        "secureVipAddress": "hello-spring-cloud-producer",
                        "isCoordinatingDiscoveryServer": "false",
                        "lastUpdatedTimestamp": "1639123544298",
                        "lastDirtyTimestamp": "1639123542699",
                        "actionType": "ADDED"
                    }
                ]
            }
        ]
    }
}
  • lastRenewalTimestamp:3分钟刷新一次

你可能感兴趣的:(eureka,学习,spring,cloud)