In previous posts we’ve used Steeltoe to interact with Eureka. Obviously Steeltoe gives us good patterns for interacting with Eureka but also has the capability to work with other service discovery registries etc.
Steeltoe’s great, but what if we wanted to just write our own Eureka either for learning how things work or maybe we just want to write our own for .NET or for another language/technology?
Eureka exposes REST operations for everything you’ll need to do with a service registry, let’s take a look.
Note: I’ll give examples using localhost, obviously change to your Eureka host/ip and I’ll also show data based upon the weatherapi we’ve worked on previously.
Query for all applications and their instances
- GET method:
http://localhost:8761/eureka/apps
- Response:
<applications> <versions__delta>1</versions__delta> <apps__hashcode>UP_1_</apps__hashcode> <application> <name>WEATHERAPI</name> <instance> <instanceId>localhost:weatherapi:5001</instanceId> <hostName>localhost</hostName> <app>WEATHERAPI</app> <ipAddr>localhost</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="false">8080</port> <securePort enabled="true">5001</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1611512017058</registrationTimestamp> <lastRenewalTimestamp>1611515916921</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1611512016403</serviceUpTimestamp> </leaseInfo> <metadata class="java.util.Collections$EmptyMap"/> <homePageUrl>https://localhost:5001/</homePageUrl> <statusPageUrl>https://localhost:5001/actuator/info</statusPageUrl> <healthCheckUrl>https://localhost:5001/actuator/health</healthCheckUrl> <vipAddress>weatherapi</vipAddress> <secureVipAddress>weatherapi</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1611512017058</lastUpdatedTimestamp> <lastDirtyTimestamp>1611512016217</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application> </applications>
Query for instances for a specific application id
- GET method:
http://localhost:8761/eureka/apps/{your app id} // Example http://localhost:8761/eureka/apps/weatherapi
- Response:
<application> <name>WEATHERAPI</name> <instance> <instanceId>localhost:weatherapi:5001</instanceId> <hostName>localhost</hostName> <app>WEATHERAPI</app> <ipAddr>localhost</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="false">8080</port> <securePort enabled="true">5001</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1611512017058</registrationTimestamp> <lastRenewalTimestamp>1611516936969</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1611512016403</serviceUpTimestamp> </leaseInfo> <metadata class="java.util.Collections$EmptyMap"/> <homePageUrl>https://localhost:5001/</homePageUrl> <statusPageUrl>https://localhost:5001/actuator/info</statusPageUrl> <healthCheckUrl>https://localhost:5001/actuator/health</healthCheckUrl> <vipAddress>weatherapi</vipAddress> <secureVipAddress>weatherapi</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1611512017058</lastUpdatedTimestamp> <lastDirtyTimestamp>1611512016217</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application>
Query for instances by its instance id
- GET method:
http://localhost:8761/eureka/apps/{your app id}/{your instance id} // Example http://localhost:8761/eureka/apps/weatherapi/localhost:weatherapi:5001
- Response:
<instance> <instanceId>localhost:weatherapi:5001</instanceId> <hostName>localhost</hostName> <app>WEATHERAPI</app> <ipAddr>localhost</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="false">8080</port> <securePort enabled="true">5001</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1611512017058</registrationTimestamp> <lastRenewalTimestamp>1611517116997</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1611512016403</serviceUpTimestamp> </leaseInfo> <metadata class="java.util.Collections$EmptyMap"/> <homePageUrl>https://localhost:5001/</homePageUrl> <statusPageUrl>https://localhost:5001/actuator/info</statusPageUrl> <healthCheckUrl>https://localhost:5001/actuator/health</healthCheckUrl> <vipAddress>weatherapi</vipAddress> <secureVipAddress>weatherapi</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1611512017058</lastUpdatedTimestamp> <lastDirtyTimestamp>1611512016217</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance>
Register an application instance
- POST method:
Headers: “Content-Type”: “application/json”, “Accept”: “application/json”http://localhost:8761/eureka/apps/{your app id} // Example http://localhost:8761/eureka/apps/myapp
-
Body:
{ "instance": { "hostName": "myhost", "app": "myapp", "vipAddress": "myservice", "secureVipAddress": "myservice", "ipAddr": "10.0.0.10", "status": "STARTING", "port": {"$": "8080", "@enabled": "true"}, "securePort": {"$": "8443", "@enabled": "true"}, "healthCheckUrl": "http://myservice:8080/healthcheck", "statusPageUrl": "http://myservice:8080/status", "homePageUrl": "http://myservice:8080", "dataCenterInfo": { "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", "name": "MyOwn" } } }
- Response: 204 No Content
As you will see from the status we’ve set this service to STARTING and hence we need set the status to UP when our service is running. Eureka, ofcourse, is just a registry and hence you service need to updated information as required.
Delete an instance
- DELETE method:
http://localhost:8761/eureka/apps/{your app id}/{your instance id} // Example http://localhost:8761/eureka/apps/myapp/myhost
- Response: 200 OK
Send an application instance heartbeat
- PUT method:
http://localhost:8761/eureka/apps/{your app id}/{your instance id} // Example http://localhost:8761/eureka/apps/myapp/myhost
- Response: 200 OK, or 404 if the instance doesn’t exist
Take an instance out of service
- PUT method:
http://localhost:8761/eureka/apps/{your app id}/{your instance id}/status?value=OUT_OF_SERVICE // Example http://localhost:8761/eureka/apps/myapp/myhost/status?value=OUT_OF_SERVICE
- Response: 200 OK, or 500 on failure
Now in the Eureka dashboard you’ll see the large, red OUT_OF_SERVICE text.
Remove the out of service state
- DELETE method:
http://localhost:8761/eureka/apps/{your app id}/{your instance id}/status?value=UP // Example http://localhost:8761/eureka/apps/myapp/myhost/status?value=UP
- Response: 200 OK, or 500 on failure
The status UP is optional, it’s a suggestion for the status after the removal of the OUT OF SERVICE override.
Update meta data
- PUT method:
http://localhost:8761/eureka/apps/{your app id}/{your instance id}/metadata?key=val // Example http://localhost:8761/eureka/apps/myapp/myhost/metadata?Version=2
- Response: 200 OK, or 500 on failure
Query for all instance of a vip address
- GET method:
http://localhost:8761/eureka/vips/{your vipAddress} // Example http://localhost:8761/eureka/vips/myservice
- Response: 200 OK, or 404 if vipAddress does not exist
<applications> <versions__delta>-1</versions__delta> <apps__hashcode>UP_1_</apps__hashcode> <application> <name>MYAPP</name> <instance> <hostName>myhost</hostName> <app>MYAPP</app> <ipAddr>10.0.0.10</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8080</port> <securePort enabled="true">8443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1611519061947</registrationTimestamp> <lastRenewalTimestamp>1611519061947</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1611518524498</serviceUpTimestamp> </leaseInfo> <metadata> <Version>2</Version> </metadata> <homePageUrl>http://myservice:8080</homePageUrl> <statusPageUrl>http://myservice:8080/status</statusPageUrl> <healthCheckUrl>http://myservice:8080/healthcheck</healthCheckUrl> <vipAddress>myservice</vipAddress> <secureVipAddress>myservice</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1611519061947</lastUpdatedTimestamp> <lastDirtyTimestamp>1611518524498</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application> </applications>
Query for all instance of a secure vip address
- GET method:
http://localhost:8761/eureka/svips/{your svipAddress} // Example http://localhost:8761/eureka/svips/myservice
- Response: 200 OK, or 404 is svipAddress does not exist
<applications> <versions__delta>-1</versions__delta> <apps__hashcode>UP_1_</apps__hashcode> <application> <name>MYAPP</name> <instance> <hostName>myhost</hostName> <app>MYAPP</app> <ipAddr>10.0.0.10</ipAddr> <status>UP</status> <overriddenstatus>UNKNOWN</overriddenstatus> <port enabled="true">8080</port> <securePort enabled="true">8443</securePort> <countryId>1</countryId> <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo"> <name>MyOwn</name> </dataCenterInfo> <leaseInfo> <renewalIntervalInSecs>30</renewalIntervalInSecs> <durationInSecs>90</durationInSecs> <registrationTimestamp>1611519061947</registrationTimestamp> <lastRenewalTimestamp>1611519061947</lastRenewalTimestamp> <evictionTimestamp>0</evictionTimestamp> <serviceUpTimestamp>1611518524498</serviceUpTimestamp> </leaseInfo> <metadata> <Version>2</Version> </metadata> <homePageUrl>http://myservice:8080</homePageUrl> <statusPageUrl>http://myservice:8080/status</statusPageUrl> <healthCheckUrl>http://myservice:8080/healthcheck</healthCheckUrl> <vipAddress>myservice</vipAddress> <secureVipAddress>myservice</secureVipAddress> <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer> <lastUpdatedTimestamp>1611519061947</lastUpdatedTimestamp> <lastDirtyTimestamp>1611518524498</lastDirtyTimestamp> <actionType>ADDED</actionType> </instance> </application> </applications>