JSON format to register service with Eureka

Last Updated on by

Post summary: What JSON data is needed to register service node with Eureka server.

Eureka is a REST based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.

Jersey 1 vs Jersey 2

As of now Eureka client works only with Jersey 1. There is PR to create Jersey 2 client, but by the time this post is created it is still not developed. Jersey 1 and Jersey 2 are mutually exclusive. If your product works with Jersey 2 then in order to register with Eureka you have to write your own client.

Registering with Eureka

Eureka documentation is giving example how to register a server or service node with Eureka server. Given example is an XML one and there is no JSON example. The XML example cannot be straight-forward converted for JSON because JSON does not support attributes as XML does.

Solution

Use following JSON in order to register with Eureka server with custom REST client:

{
	"instance": {
		"hostName": "WKS-SOF-L011",
		"app": "com.automationrhapsody.eureka.app",
		"vipAddress": "com.automationrhapsody.eureka.app",
		"secureVipAddress": "com.automationrhapsody.eureka.app"
		"ipAddr": "10.0.0.10",
		"status": "STARTING",
		"port": {"$": "8080", "@enabled": "true"},
		"securePort": {"$": "8443", "@enabled": "true"},
		"healthCheckUrl": "http://WKS-SOF-L011:8080/healthcheck",
		"statusPageUrl": "http://WKS-SOF-L011:8080/status",
		"homePageUrl": "http://WKS-SOF-L011:8080",
		"dataCenterInfo": {
			"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", 
			"name": "MyOwn"
		},
	}
}

Implementation details

Important part which is not clear enough in standard documentation is: “securePort”: {“$”: “8443”, “@enabled”: “true”}. Note that “securePort”: “8443” would also work, but this will just set the port number without enabling it. By default secure port is disabled unless register call enables it.

I would recommend to use one and the same for “vipAddress” and “secureVipAddress”. When searching for HTTP endpoint Eureka server uses “vipAddress” and if you switch your client to search for HTTPS port Eureka server will now search for “secureVipAddress”. If both are different this may lead to confusion why no endpoint is returned although there is one with HTTPS enabled.

Implementation is needed for DataCenterInfo interface. One such implementation is:

private static final class DefaultDataCenterInfo implements DataCenterInfo {
	private final Name name;

	private DefaultDataCenterInfo(Name name) {
		this.name = name;
	}

	@Override
	public Name getName() {
		return name;
	}

	public static DataCenterInfo myOwn() {
		return new DefaultDataCenterInfo(Name.MyOwn);
	}
}

As seen in JSON example application status is STARTING. This is good practice to keep STARTING state until application is fully started. This will prevent current not yet ready node to be returned for usage by Eureka server. Once application is fully started then with subsequent call you can change the status to UP. This is done with REST PUT call to /eureka/v2/apps/appID/instanceID/status?value=UP. InstanceID is basically the hostname. AppID is the one registered with “app” in JSON. Also good idea is to have “app” same as “vipAddress” to minimise confusion.

Conclusion

Eureka is a really nice tool. It has a default client which can be used out of the box. Problem is that this client uses Jersey 1. If you need Jersey 2 client by the time of this post you have to make it on your own. This post gives basic direction how to do this. Since official documentation is lacking details how to resister a node with JSON this port gives more clarity. The most important part is: “securePort”: {“$”: “8443”, “@enabled”: “true”}.

If you find this post useful, please share it to reach more people. Sharing is caring!
Share on FacebookShare on LinkedInTweet about this on TwitterShare on Google+Email this to someone
Category: Java | Tags: , ,
  • Happy Squirrel

    I’m trying to make this work with Eureka 1.6.2. However, regardless if I use JSON or XML, REST response is ‘can’t parse message body’. It looks like it’s somehow connected to jersey-json. However, it doesn’t work even after I installed everything jersey1-* in my Debian system.
    Can you point me into the right direction?

    • Lyudmil Latinov

      Can you dump the request you are trying to send to Eureka?

      • Happy Squirrel

        Yes, I can. However, the problem was partially solved for me by adding jersey-json to eureka server build. After that, JSON request started working.

      • Happy Squirrel

        Here are both requests, XML and JSON.

        {
        “instance”: {
        “hostName”: “localhost”,
        “app”: “com.edulog.edp”,
        “vipAddress”: “com.edulog.edp”,
        “secureVipAddress”: “com.edulog.edp”,
        “ipAddr”: “10.1.1.30”,
        “status”: “STARTING”,
        “port”: {“$”: “8080”, “@enabled”: “true”},
        “securePort”: {“$”: “8443”, “@enabled”: “true”},
        “healthCheckUrl”: “http://localhost:8080/healthcheck”,
        “statusPageUrl”: “http://localhost:8080/status”,
        “homePageUrl”: “http://localhost:8080”,
        “dataCenterInfo”: {
        “@class”: “com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo”,
        “name”: “MyOwn”
        }
        }
        }

        localhost
        COM.EDULOG.EDP
        10.1.1.11
        STARTING
        8080
        8443

        MyOwn

        http://localhost:8080
        http://localhost:8080/status
        http://localhost:8080/healthcheck
        com.edulog.edp
        com.edulog.edp

  • Veera

    Hi, I would like to register soap service with Eureka permanently. The problem is that, if Eureka server restarted then eureka is not showing my soap service, so, i need to send new rest request to register. So, is there any way to stop registering the same service every time while restarting the eureka server?

    • Lyudmil Latinov

      Not big expert on the subject, but from Eureka requirements I can see it needs Tomcat, there is no DB requirement, so most likely it does not persist the data, hence restart removes the nodes. Only option you have is to have several Eureka servers that work in a cluster and replicate the data. If one is restarted then it gets the data from others.