@EnableDiscoveryClient tells Spring Cloud that an application should take part in service discovery: it registers itself with a service registry (Eureka, Consul, Kubernetes and others) and can look up other services by name. In current Spring Cloud versions the annotation is optional, because adding a discovery client starter to the classpath already activates the behaviour through auto-configuration.
The annotation is the vendor-neutral switch. The registry itself is chosen by the dependency you add, not by the annotation.
@EnableDiscoveryClient worksIn a microservice system, instances start, stop and move constantly, so hard-coded host names and ports break. Service discovery gives each service a logical name (for example inventory-service). Instances register their address under that name, and callers ask the registry for healthy instances at call time.
Spring Cloud abstracts this behind the DiscoveryClient interface in spring-cloud-commons. Implementations exist for Netflix Eureka, Consul, Apache ZooKeeper and Kubernetes, so your calling code does not change when you switch registry.
@EnableDiscoveryClient works@EnableDiscoveryClient imports the discovery configuration of whichever implementation is on the classpath, so the instance registers with that registry at startup. It has an autoRegister attribute (default true) that controls whether the instance registers itself.
Two points are often misunderstood:
DiscoveryClient implementation such as the Eureka client is on the classpath, the instance registers automatically. You can still add the annotation for clarity or to set autoRegister = false.@EnableEurekaClient is the older Eureka-specific variant and is deprecated in favour of @EnableDiscoveryClient or no annotation at all.Auto-registration can be switched off with the property spring.cloud.service-registry.auto-registration.enabled=false.
Add the Spring Cloud BOM and the Eureka client starter. Pick the Spring Cloud release train that matches your Spring Boot version from the official compatibility table.
Configure the service name and the registry address:
Then write the application class:
Run a Eureka server (a separate application using spring-cloud-starter-netflix-eureka-server and @EnableEurekaServer) and start this service. It appears in the Eureka dashboard under ORDER-SERVICE.
You can query the registry directly through DiscoveryClient:
Most code does not call the registry directly. Instead, use a load-balanced client and a logical URL:
A request such as restClient.get().uri("http://inventory-service/items/42") is resolved against the registry and spread across instances by Spring Cloud LoadBalancer. Spring Cloud OpenFeign interfaces and Spring Cloud Gateway routes using lb://inventory-service use the same mechanism.
Older tutorials say discovery clients load balance with Ribbon. Netflix Ribbon entered maintenance mode and was removed from Spring Cloud in the 2020.0 release train. The replacement is Spring Cloud LoadBalancer (spring-cloud-starter-loadbalancer), which is included when you use the supported starters. Feign is now Spring Cloud OpenFeign, a declarative HTTP client, not a load balancer in itself.
| Registry | Starter | When it fits |
|---|---|---|
| Eureka | spring-cloud-starter-netflix-eureka-client | Spring-centric systems with a dedicated registry service |
| Consul | spring-cloud-starter-consul-discovery | You also want health checks and key-value config |
| Kubernetes | spring-cloud-starter-kubernetes-client | The platform already provides service discovery |
On Kubernetes, the platform's own DNS-based services are often enough, and an extra registry is unnecessary.
@EnableDiscoveryClient?Usually not. With a discovery client starter on the classpath, registration happens through auto-configuration. Keep the annotation if you want the intent to be visible or need autoRegister.
@EnableDiscoveryClient and @EnableEurekaClient?@EnableEurekaClient works only with Eureka and is deprecated. @EnableDiscoveryClient works with any supported registry.
Check that spring.application.name is set, that eureka.client.service-url.defaultZone points to a running server, and that eureka.client.register-with-eureka has not been set to false.
No. Discovery locates instances; a gateway such as Spring Cloud Gateway handles routing, authentication and rate limiting at the edge, and often uses discovery to find its targets.
@EnableDiscoveryClient marks an application as a participant in service discovery, but the registry comes from the starter you include. It is optional in modern Spring Cloud, Ribbon has been replaced by Spring Cloud LoadBalancer, and callers should use logical service names with a load-balanced client.