Mastering Service Discovery: Spring Cloud Eureka & Consul in Cloud‑Native Microservices
This tutorial walks through the fundamentals of service registration and discovery in microservice architectures, comparing Eureka, Zookeeper, Consul, and Etcd, and provides step‑by‑step Spring Cloud implementations for both Eureka and Consul with code samples and configuration details.
Microservice Series – Service Registration and Discovery
We previously introduced four common service‑registry technologies—Eureka, Zookeeper, Consul, and Etcd—and compared them on consistency protocol, health‑check, watch/long‑polling, avalanche protection, security, multi‑data‑center support, management UI, and integration with Spring Cloud, Dubbo, and Kubernetes.
4种注册中心技术对比
指标 | Eureka | Zookeeper | Consul | Etcd
--- | --- | --- | --- | ---
一致性协议 | AP | CP (Paxos) | CP (Raft) | CP (Raft)
健康检查 | TTL | TCP Keep Alive | TTL/HTTP/TCP/Script | Lease TTL KeepAlive
watch/long polling | 不支持 | watch | long polling | watch
雪崩保护 | 支持 | 不支持 | 不支持 | 不支持
安全与权限 | 不支持 | ACL | ACL | RBAC
是否支持多数据中心 | 是 | 否 | 是 | 否
是否有管理界面 | 是 | 否 (可用第三方 ZkTools) | 是 | 否
Spring Cloud 集成 | 支持 | 支持 | 支持 | 支持
Dubbo 集成 | 不支持 | 支持 | 支持 | 不支持
K8S 集成 | 不支持 | 不支持 | 支持 | 支持All four registries integrate well with Spring Cloud, which offers a one‑stop solution for configuration management, service discovery, load balancing, circuit breaking, intelligent routing, and more.
1 Service Registry Center
Spring Cloud abstracts service governance, allowing seamless switching between Eureka, Consul, etc., without changing application code.
2 Spring Cloud Implementation
2.1 Spring Cloud Eureka
Eureka is part of Spring Cloud Netflix and provides service discovery, circuit breaker, routing, and client‑side load balancing.
2.1.1 Create Registry
Create a parent Maven project micro-service-center with packaging pom . Add a module eureka-service and include the dependency spring-cloud-netflix-eureka-server in its pom.xml .
<code>@SpringBootApplication
@EnableEurekaServer
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
System.out.println("Start Eureka Service");
}
}
</code>Configure application.yml to disable self‑registration:
<code>server:
port: 1000
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # do not register as client
fetch-registry: false # do not fetch registry
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
</code>After starting, the Eureka UI is available at http://localhost:1000/ .
2.1.2 Create Client
Add a module eureka-client with dependency spring-cloud-netflix-eureka-server and Lombok.
<code>@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
System.out.println("start client!");
}
}
</code>Client application.yml :
<code>server:
port: 1001
spring:
application:
name: eureka-client
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka/
</code>Expose an endpoint to list registered services:
<code>@Controller
@RequestMapping("/eurekacenter")
public class EuServiceController {
@Autowired
DiscoveryClient discoveryClient;
@RequestMapping(value = "/service", method = RequestMethod.GET)
@ResponseBody
public String getServiceInfo() {
return "service:" + discoveryClient.getServices() + " , memo:" + discoveryClient.description();
}
}
</code>Visiting http://localhost:1000/ now shows the client service (port 1001) in the Eureka UI, and http://localhost:1001/eurekacenter/service returns the list of services.
2.2 Spring Cloud Consul
Consul provides service discovery, health checking, KV store, multi‑data‑center support, and a web UI.
2.2.1 Consul Advantages
Raft‑based consistency (simpler than Paxos)
Multi‑data‑center support
Built‑in health checks
HTTP and DNS APIs
Web management UI
2.2.2 Install Consul
Download the 64‑bit binary from https://www.consul.io/downloads.html , place it in /usr/local/bin , and start in dev mode:
<code>consul agent -dev
</code>The client address is 127.0.0.1:8500 . Access the UI at http://127.0.0.1:8500/ui/dc1/services .
2.2.3 Create Service Provider
Add a module consul-client with dependency spring-cloud-starter-consul-discovery and spring-boot-starter-actuator .
<code>@SpringBootApplication
@EnableDiscoveryClient
public class ConsulClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulClientApplication.class, args);
}
}
</code>Configure application.yml :
<code>spring:
application:
name: consul-producer
cloud:
consul:
host: localhost
port: 8500
server:
port: 8501
</code>After starting, the service appears in the Consul UI as consul-producer .
3 Summary
Beyond Eureka and Consul, other registries such as Zookeeper and Nacos exist, but all aim to solve two core problems in microservices:
Decoupling services : Replace hard‑coded IP/port dependencies with logical service names.
Dynamic management : Enable real‑time registration, deregistration, and health‑based removal of services to support scaling and fault tolerance.
Architecture & Thinking
🍭 Frontline tech director and chief architect at top-tier companies 🥝 Years of deep experience in internet, e‑commerce, social, and finance sectors 🌾 Committed to publishing high‑quality articles covering core technologies of leading internet firms, application architecture, and AI breakthroughs.
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.