Search code examples
netflix-eurekaspring-cloud-netflix

How to register more than one service per eureka client on eureka server?


I am starting learning Eureka APIs. I created a Eureka Server using spring boot and corresponding clients. For starting I created a Eureka Client and registered with Server. The client was exposing a get service with no path

@GetMapping("/")

This works fine. As as soon as I get server Instance using DiscoveryClient as below, i can hit the service

List<ServiceInstance> list = client.getInstances(service);
    if (list != null && list.size() > 0) {
        URI uri = list.get(0).getUri();
        String url=uri.toString();
        if (url != null) {
            return (new RestTemplate()).getForObject(url, String.class);
        }
    }

But I was not sure how to configure a service that has a path e.g.

@GetMapping("/greetings")

For now I can hardcode it as

List<ServiceInstance> list = client.getInstances(service);
    if (list != null && list.size() > 0) {
        URI uri = list.get(0).getUri();
        String url=uri.toString()+"/greetings";
        if (url != null) {
            return (new RestTemplate()).getForObject(url, String.class);
        }
    }

Or register this as service name in bootstrap.yml at client as

spring:
  application:
    name: eurekaClient2/greetings

But is there some other way to do this? What if i want to expose a get service and a post service? How do i configure in that case?


Solution

  • From Eureka server, you will not get endpoints provided by a service whose instances are registered in Eureka server.

    Response from Eureka server only contains information about availability of application, like hostname, IP address, etc.

    Clients will have to hardcode the endpoints (or read them from properties) of other services they want to call. Eureka only provides the hostname (and other related details) of the registered instances.

    spring.application.name is used as service-id. A service registers itself to Eureka server registry using this service-id.