Service discovery cho phép service tìm nhau theo tên logic thay vì IP:port cứng.
Các lựa chọn 2026:
1. Kubernetes Service (phổ biến nhất):
- K8s tự handle service discovery qua DNS (service-name.namespace.svc.cluster.local).
- Spring app chỉ cần cấu hình URL: http://order-service/api/orders.
- Không cần Eureka, không cần Spring Cloud Discovery.
2. Spring Cloud + Eureka (traditional):
// Eureka Server
@SpringBootApplication
@EnableEurekaServer
class DiscoveryServer {}
// Client — tự register + discover
@SpringBootApplication
@EnableDiscoveryClient
class OrderService {}spring:
application.name: order-service
eureka:
client:
serviceUrl.defaultZone: http://eureka:8761/eurekaLoad balancing với Spring Cloud LoadBalancer:
@Bean
@LoadBalanced // RestTemplate/RestClient tự load-balance qua service name
RestClient.Builder restClientBuilder() {
return RestClient.builder();
}
// Gọi:
restClient.get().uri("http://order-service/api/orders").retrieve().body(...);
// Spring tự resolve "order-service" → IP:port từ registry2026 reality: nếu deploy Kubernetes → dùng K8s built-in.
Eureka/Consul cho on-premise hoặc không có K8s.
Service discovery lets services find each other by logical name instead of hardcoded IP:port.
2026 options:
1. Kubernetes Service (most popular):
- K8s handles service discovery natively via DNS (service-name.namespace.svc.cluster.local).
- Spring app only needs to configure the URL: http://order-service/api/orders.
- No Eureka, no Spring Cloud Discovery needed.
2. Spring Cloud + Eureka (traditional):
// Eureka Server
@SpringBootApplication
@EnableEurekaServer
class DiscoveryServer {}
// Client — auto-registers and discovers
@SpringBootApplication
@EnableDiscoveryClient
class OrderService {}spring:
application.name: order-service
eureka:
client:
serviceUrl.defaultZone: http://eureka:8761/eurekaLoad balancing with Spring Cloud LoadBalancer:
@Bean
@LoadBalanced // RestClient auto load-balances using service name
RestClient.Builder restClientBuilder() {
return RestClient.builder();
}
// Calling:
restClient.get().uri("http://order-service/api/orders").retrieve().body(...);
// Spring resolves "order-service" → IP:port from the registry2026 reality: on Kubernetes → use K8s built-in.
Eureka/Consul for on-premise or non-K8s environments.