Skip to content

Consul / Eureka Clients

Developer UtilitiesMicroservices / Service DiscoveryJava

What it is

Consul and Eureka are popular service discovery tools for microservices. Java clients integrate applications with these services, enabling automatic registration, service lookup, and health monitoring for distributed systems.

Clients for Eureka or Consul enable automatic registration of services, health check management, and runtime discovery of other services. This allows Java microservices to communicate without hardcoded URLs, supporting dynamic scaling and resiliency.

Installation

Eureka Client: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.1.6</version> </dependency> Consul Client: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> <version>3.1.6</version> </dependency>

Getting started

The smallest useful thing you can do with it, and what each part means.

Eureka Client Registration
@SpringBootApplication
@EnableEurekaClient
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
Registers a Spring Boot application with a Eureka server for service discovery.
Consul Client Registration
@SpringBootApplication
@EnableDiscoveryClient
public class MyConsulApp {
    public static void main(String[] args) {
        SpringApplication.run(MyConsulApp.class, args);
    }
}
Registers a Spring Boot application with Consul for service discovery and health checks.

Advanced usage

Where the library earns its place over a simpler alternative.

Service lookup with RestTemplate
@Bean
@LoadBalanced
RestTemplate restTemplate() {
    return new RestTemplate();
}

String response = restTemplate.getForObject("http://MY-SERVICE/endpoint", String.class);
Uses service names instead of hardcoded URLs with client-side load balancing for Eureka or Consul.
Health checks and metadata
management.endpoint.health.show-details=always
spring.cloud.consul.discovery.health-check-path=/actuator/health
Configures health check endpoints and service metadata for Consul or Eureka.
Dynamic service registration
spring.application.name=my-service
spring.cloud.consul.discovery.instance-id=${spring.application.name}-${random.value}
Automatically generates unique instance IDs for dynamic registration in Consul.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

ServiceNotFoundException
Occurs when the requested service is not registered or unavailable. Ensure the service is running and registered with Eureka/Consul.
Connection refused
Check that the Eureka or Consul server is running and reachable from the client.
Timeout during discovery
Increase timeout or check network connectivity between services and the registry.

Best practices

  • Use client-side load balancing with RestTemplate or WebClient for scalable calls.
  • Configure health check endpoints to detect unhealthy instances quickly.
  • Set proper instance metadata and service names for easier monitoring.
  • Enable circuit breakers and retries for service-to-service communication.
  • Monitor service registries for changes and remove stale instances periodically.

Background

Why it exists, and what it was reacting to.

As microservices architectures grew, managing service instances dynamically became essential. Eureka (from Netflix) and Consul (from HashiCorp) provide registries where services register themselves and discover other services at runtime. Java client libraries simplify integrating Spring Boot and other Java applications with these service registries, improving scalability, load balancing, and fault tolerance.