- 重新定义Spring Cloud实战
- 许进
- 864字
- 2023-07-26 11:37:15
2.3 Eureka Server的REST API简介
前面已经介绍了一个最基本的Eureka Server及Eureka Client,它是整个Spring Cloud生态里面“服务注册及发现”的一个缩影。这里举的是使用Java语言的Client端的例子,但是实际Eureka Server提供了REST API,允许非Java语言的其他应用服务通过HTTP REST的方式接入Eureka的服务发现中。本节主要介绍下Eureka Server的REST API的基本操作。
2.3.1 REST API列表
Eureka在GitHub的wiki上专门写了一篇《Eureka REST operations》来介绍Eureka Server的REST API接口,Spring Cloud Netflix Eureka跟Spring Boot适配之后,提供的REST API与原始的REST API有一点点不同,其路径中的{version}值固定为eureka,其他的变化不大,如表2-2所示。
表2-2 Eureka REST API接口列表
2.3.2 REST API实例
1.查询所有应用实例
HTTP/1.1200 Content-Type: application/xml Transfer-Encoding: chunked Date: Tue, 19 Jun 2018 06:22:56 GMT <applications> <versions__delta>1</versions__delta> <apps__hashcode>UP_1_</apps__hashcode> <application> <name>DEMO-CLIENT1</name> <instance> <instanceId>10.2.238.223:demo-client1:8081</instanceId> <hostName>10.2.238.223</hostName> <app>DEMO-CLIENT1</app> <ipAddr>10.2.238.223</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8081</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataC enterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1529377552795</registrationTimestamp> <lastRenewalTimestamp>1529389437739</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1529377552795</serviceUpTimestamp> </leaseInfo> <metadata> <management.port>8081</management.port> </metadata> <homePageUrl>http://10.2.238.223:8081/</homePageUrl> <statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl> <healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl> <vipAddress>demo-client1</vipAddress> <secureVipAddress>demo-client1</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp> <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application> </applications>
2.根据appId查询
curl -i http://localhost:8761/eureka/apps/demo-client1 HTTP/1.1200 Content-Type: application/xml Transfer-Encoding: chunked Date: Tue, 19 Jun 2018 06:28:34 GMT <application> <name>DEMO-CLIENT1</name> <instance> <instanceId>10.2.238.223:demo-client1:8081</instanceId> <hostName>10.2.238.223</hostName> <app>DEMO-CLIENT1</app> <ipAddr>10.2.238.223</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8081</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1529377552795</registrationTimestamp> <lastRenewalTimestamp>1529389797866</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1529377552795</serviceUpTimestamp> </leaseInfo> <metadata> <management.port>8081</management.port> </metadata> <homePageUrl>http://10.2.238.223:8081/</homePageUrl> <statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl> <healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl> <vipAddress>demo-client1</vipAddress> <secureVipAddress>demo-client1</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp> <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application>
查询不到则返回如下结果:
curl -i http://localhost:8761/eureka/apps/demo HTTP/1.1404 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 06:38:14 GMT
3.根据appId及instanceId查询
查询出数据时跟上面的返回一致,查找不到则返回如下结果:
curl -i http://localhost:8761/eureka/apps/demo-client1/notfound HTTP/1.1404 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 06:33:42 GMT
4.根据instanceId查询
curl -i http://localhost:8761/eureka/instances/10.2.238.223:demo-client1:8081 HTTP/1.1200 Content-Type: application/xml Transfer-Encoding: chunked Date: Tue, 19 Jun 2018 06:36:09 GMT <instance> <instanceId>10.2.238.223:demo-client1:8081</instanceId> <hostName>10.2.238.223</hostName> <app>DEMO-CLIENT1</app> <ipAddr>10.2.238.223</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8081</port> <securePort enabled="false">443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1529377552795</registrationTimestamp> <lastRenewalTimestamp>1529390248015</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1529377552795</serviceUpTimestamp> </leaseInfo> <metadata> <management.port>8081</management.port> </metadata> <homePageUrl>http://10.2.238.223:8081/</homePageUrl> <statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl> <healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl> <vipAddress>demo-client1</vipAddress> <secureVipAddress>demo-client1</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp> <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance>
查询不到则返回如下结果:
curl -i http://localhost:8761/eureka/instances/demo-instance-id HTTP/1.1404 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 06:36:51 GMT
5.注册新应用实例
xml格式提交的实例如下:
curl -i -H "Content-Type: application/xml" -H "Content-Length: 773" -H "Accept- Encoding: gzip" -X POST -d '<instance> <instanceId>client2:8082</instanceId> <hostName>127.0.0.1</hostName> <app>CLIENT2</app> <ipAddr>127.0.0.1</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8082</port> <securePort enabled="false">7002</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <metadata class="java.util.Collections$EmptyMap"/> <vipAddress>client2</vipAddress> <secureVipAddress>client2</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1529397327986</lastUpdatedTimestamp> <lastDirtyTimestamp>1529397327986</lastDirtyTimestamp> </instance>' http://localhost:8761/eureka/apps/client2 HTTP/1.1204 Content-Type: application/xml Date: Tue, 19 Jun 2018 08:35:59 GMT
json格式提交的实例如下:
curl -i -H "Content-Type: application/json" -X POST -d '{ "instance": { "instanceId": "client2:8082", "app": "client2", "appGroupName": null, "ipAddr": "127.0.0.1", "sid": "na", "homePageUrl": null, "statusPageUrl": null, "healthCheckUrl": null, "secureHealthCheckUrl": null, "vipAddress": "client2", "secureVipAddress": "client2", "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": { "$": 8082, "@enabled": "true" }, "securePort": { "$": 7002, "@enabled": "false" }, "metadata": { "@class": "java.util.Collections$EmptyMap" } } }' http://localhost:8761/eureka/apps/client2 HTTP/1.1204 Content-Type: application/xml Date: Tue, 19 Jun 2018 06:58:18 GMT
6.注销应用实例
curl -i -X DELETE http://localhost:8761/eureka/apps/client2/client2:8082 HTTP/1.1200 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 06:59:31 GMT
7.暂停/下线应用实例
成功则返回:
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo- client1:8081/status\? value\=OUT_OF_SERVICE HTTP/1.1200 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 07:12:24 GMT
此时Eureka界面示例如图2-3所示。
图2-3 下线服务时Eureka界面展示示例
找不到该实例返回:
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo- client1:8081/status\? value\=OUT_OF_SERVICE HTTP/1.1404 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 07:11:17 GMT
8.恢复应用实例
curl -i -X DELETE http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo- client1:8081/status HTTP/1.1200 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 07:14:03 GMT
9.应用实例发送心跳
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo- client1:8081 HTTP/1.1200 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 07:16:38 GMT
10.修改应用实例元数据
curl -i -X PUT http://localhost:8761/eureka/apps/demo-client1/10.2.238.223:demo- client1:8081/metadata\? profile\=canary HTTP/1.1200 Content-Type: application/xml Content-Length: 0 Date: Tue, 19 Jun 2018 07:18:18 GMT
设置metadata,其中key为proflie, value为canary。