Understanding Spring Cloud Eureka Architecture and Building a High‑Availability Cluster – A Hands‑On Guide
This article explains the core concepts of Spring Cloud Eureka, demonstrates how to create a Eureka server, a provider, and a consumer with full code examples, and shows how to configure a two‑node and multi‑node Eureka cluster for high availability in micro‑service environments.
Background
In micro‑service systems the service registry, load‑balancer and remote‑call components are core. Eureka, an open‑source Netflix project, provides registration and discovery, allowing instances to self‑register and be discovered.
Architecture Evolution
Without a registry, services call each other via hard‑coded URLs (e.g., http://192.168.1.1:8080/a). Introducing a registry inserts a “service center” so callers first query the registry for an instance, which decouples callers from host/port and simplifies scaling.
Practical Implementation
1. Create the Eureka server
Generate a Spring Boot project named eureka-server and add the spring-cloud-starter-eureka-server dependency in pom.xml (Spring Boot 1.5.4.RELEASE, Spring Cloud Edgware.SR3). Add @EnableEurekaServer to the main class and disable self‑registration in application.properties:
spring.application.name=eureka-server
server.port=8001
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=falseRunning the application and visiting http://localhost:8001/ shows an empty dashboard because no clients are registered.
2. Create a service provider
Generate a Spring Boot project eureka-provider with dependencies spring-cloud-starter-eureka and spring-boot-starter-web. Annotate the main class with @EnableDiscoveryClient. Add a REST controller exposing /hello and /dc endpoints. Configure application.properties:
spring.application.name=eureka-provider
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/3. Create a service consumer
Generate eureka-consumer with the same dependencies. Define a RestTemplate bean, inject DiscoveryClient and LoadBalancerClient, and implement /dc (list services) and /rpc (choose a provider instance via the load balancer and invoke /hello).
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@GetMapping("/rpc")
public String rpc() {
ServiceInstance instance = loadBalancerClient.choose("eureka-provider");
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/hello";
String result = restTemplate.getForObject(url, String.class);
return "Remote call result: " + result;
}Configure application.properties:
spring.application.name=eureka-consumer
server.port=9002
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/Start server → provider → consumer and request http://localhost:9002/rpc; the response shows the provider’s message, confirming successful discovery and remote call.
Cluster Configuration
For high availability, run multiple Eureka instances that register with each other. Example two‑node configuration uses application-eureka1.properties (port 8001, serviceUrl pointing to eureka2) and application-eureka2.properties (port 8002, serviceUrl pointing to eureka1). Add host entries:
127.0.0.1 eureka1
127.0.0.1 eureka2Clients set eureka.client.serviceUrl.defaultZone to both URLs, e.g. http://eureka1:8001/eureka/,http://eureka2:8002/eureka/. In production three or more nodes are typical; the same pattern applies.
Conclusion
Eureka remains a fundamental component of Spring Cloud for service registration and discovery. Although Netflix no longer maintains it, understanding its architecture and cluster setup is valuable for learning the Spring Cloud ecosystem.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
