Consul / Eureka Clients

Language: Java

Microservices / Service Discovery

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.

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.

Installation

maven: 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>
gradle: implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:3.1.6' implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery:3.1.6'

Usage

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.

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.

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.

Error Handling

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.