Spring Cloud - Eureka Client Consumer Example



In Spring Cloud - Creating Eureka Client chapter, we've seen that our Eureka Server created in Spring Cloud - Creating Eureka Server has got the registered client instances of the EurekaClient setup. We can now setup the Consumer which can ask the Eureka Server the address of the EurekaClient nodes.

Example - Eureka Registry Consumer

Let us add a controller which can get the information from the Eureka Registry. This controller will be added to our earlier Eureka Client itself. Let us create the following controller to the client.

EurekaClientInstanceController.java

The code for main Spring Boot application class file is as shown below −

package com.tutorialspoint.eurekaclient;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EurekaClientInstanceController {
	 @Autowired
	 private DiscoveryClient eurekaConsumer;
	   
	 @GetMapping("/customer_services")
	 public ResponseEntity<List<String>> getInstance() {
		 List<String> services = eurekaConsumer.getServices();
		 return new ResponseEntity<>(services, HttpStatus.OK);
	 }  	 
}

Note the annotation @DiscoveryClient which is what Spring framework provides to talk to the registry.

Run the Application and Verify the output

Run the Client Application as a Spring Boot App and you can get the output as shown below −


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.5.6)[0;39m

[2m2025-10-07T16:37:31.575+05:30[0;39m [32m INFO[0;39m [35m32072[0;39m [2m--- [eurekaclient] [           main] [0;39m[36mc.t.e.EurekaclientApplication           [0;39m [2m:[0;39m Starting EurekaclientApplication using Java 21.0.6 with PID 32072 (D:\Projects\eurekaclient\target\classes started by mahes in D:\Projects\eurekaclient)
...
[2m2025-10-07T16:38:03.417+05:30[0;39m [32m INFO[0;39m [35m32072[0;39m [2m--- [eurekaclient] [reshExecutor-%d] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Registered Applications size is zero : true
[2m2025-10-07T16:38:03.417+05:30[0;39m [32m INFO[0;39m [35m32072[0;39m [2m--- [eurekaclient] [reshExecutor-%d] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Application version is -1: false
[2m2025-10-07T16:38:03.417+05:30[0;39m [32m INFO[0;39m [35m32072[0;39m [2m--- [eurekaclient] [reshExecutor-%d] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m Getting all instance registry info from the eureka server
[2m2025-10-07T16:38:03.478+05:30[0;39m [32m INFO[0;39m [35m32072[0;39m [2m--- [eurekaclient] [reshExecutor-%d] [0;39m[36mcom.netflix.discovery.DiscoveryClient   [0;39m [2m:[0;39m The response status is 200
[2m2025-10-07T16:42:32.995+05:30[0;39m [32m INFO[0;39m [35m32072[0;39m [2m--- [eurekaclient] [rap-executor-%d] [0;39m[36mc.n.d.s.r.aws.ConfigClusterResolver     [0;39m [2m:[0;39m Resolving eureka endpoints via configuration

Verify the server output to have the client registered.

...
[2m2025-10-07T16:37:33.503+05:30[0;39m [32m INFO[0;39m [35m17048[0;39m [2m--- [eurekaserver] [nio-8761-exec-1] [0;39m[36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Registered instance EUREKACLIENT/Home:eurekaclient with status UP (replication=false)
[2m2025-10-07T16:37:34.121+05:30[0;39m [32m INFO[0;39m [35m17048[0;39m [2m--- [eurekaserver] [nio-8761-exec-3] [0;39m[36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Registered instance EUREKACLIENT/Home:eurekaclient with status UP (replication=true)

Now let's hit the url as http://localhost:8080/customer_services and you'll see the following output −

["eurekaclient"]
Advertisements