0
0
MicroservicesHow-ToBeginner ยท 4 min read

How to Use Eureka for Service Discovery in Microservices

To use Eureka for service discovery, set up a Eureka Server as a registry where services register themselves. Then, configure your microservices as Eureka Clients to register with the server and discover other services dynamically.
๐Ÿ“

Syntax

Eureka Server: Acts as a service registry where microservices register and discover each other.

Eureka Client: Microservices that register themselves to the Eureka Server and query it to find other services.

Typical configuration involves enabling Eureka Server and Client in Spring Boot applications using annotations and properties.

java
/* Eureka Server Setup */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

/* Eureka Client Setup */
@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

/* application.properties for Eureka Server */
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761

/* application.properties for Eureka Client */
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8080
๐Ÿ’ป

Example

This example shows a simple Eureka Server and a microservice client registering with it. The client can discover other services via Eureka.

java
/* Eureka Server Application */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

/* application.properties for Eureka Server */
server.port=8761
spring.application.name=eureka-server

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false


/* Eureka Client Application */
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceApplication {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Service!";
    }

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

/* application.properties for Eureka Client */
server.port=8080
spring.application.name=sample-service

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Output
Eureka Server running on port 8761 Service 'sample-service' registered with Eureka Client responds to GET /hello with "Hello from Service!"
โš ๏ธ

Common Pitfalls

  • Forgetting to disable self-registration on the Eureka Server causes it to register as a client to itself, which is unnecessary.
  • Incorrect eureka.client.service-url.defaultZone URL prevents clients from registering.
  • Not setting unique spring.application.name for each service causes registration conflicts.
  • Network issues or firewall blocking ports can stop service registration or discovery.
properties
/* Wrong: Eureka Server registers as client to itself */
eureka.client.register-with-eureka=true

/* Right: Disable self-registration on Eureka Server */
eureka.client.register-with-eureka=false
๐Ÿ“Š

Quick Reference

Eureka Server: Use @EnableEurekaServer and disable client registration.

Eureka Client: Use @EnableEurekaClient, set spring.application.name, and point eureka.client.service-url.defaultZone to the server.

Ports: Default Eureka Server port is 8761; clients use any free port.

ConceptConfiguration/AnnotationNotes
Eureka Server@EnableEurekaServerRun on port 8761, disable self-registration
Eureka Client@EnableEurekaClientSet service name and server URL
Service Namespring.application.nameUnique name per service
Server URLeureka.client.service-url.defaultZoneURL of Eureka Server
Portsserver.port8761 for server, any free port for clients
โœ…

Key Takeaways

Set up a Eureka Server with @EnableEurekaServer and disable its self-registration.
Configure microservices as Eureka Clients with @EnableEurekaClient and point them to the server URL.
Each service must have a unique spring.application.name to register correctly.
Verify network connectivity and correct URLs to avoid registration failures.
Use Eureka to enable dynamic discovery and load balancing of microservices.