서비스 디스커버리(Service Discovery)는 마이크로서비스 환경에서 서비스 인스턴스의 네트워크 위치(IP:Port)를 자동으로 찾는 메커니즘이다.
서비스 디스커버리 패턴
| 패턴 | 설명 | 대표 도구 |
|---|
| 클라이언트 사이드 | 클라이언트가 직접 레지스트리 조회 후 로드밸런싱 | Eureka + Ribbon |
| 서버 사이드 | 클라이언트 → LB → 레지스트리 조회 | AWS ALB + Route53 |
| 서비스 메시 | 사이드카 프록시가 자동 처리 | Istio + Envoy |
서비스 레지스트리 비교
Consul 서비스 등록 예시
python
import consul
import socket
c = consul.Consul(host='consul-server', port=8500)
# 서비스 등록
c.agent.service.register(
name="payment-service",
service_id=f"payment-{socket.gethostname()}",
address=socket.gethostbyname(socket.gethostname()),
port=8080,
tags=["v2", "production"],
check=consul.Check.http(
url="http://localhost:8080/health",
interval="10s",
timeout="2s",
deregister="30s" # 30초 헬스체크 실패 시 자동 제거
)
)
# 서비스 조회
_, services = c.health.service("payment-service", passing=True)
for svc in services:
addr = svc['Service']['Address']
port = svc['Service']['Port']
print(f"사용 가능한 인스턴스: {addr}:{port}")
Kubernetes Service Discovery
yaml
# ClusterDNS 자동 서비스 디스커버리
# payment-service.default.svc.cluster.local:8080
apiVersion: v1
kind: Service
metadata:
name: payment-service
spec:
selector:
app: payment
ports:
- port: 8080
targetPort: 8080
type: ClusterIP
---
# Endpoint 자동 등록 (파드 생성/삭제 시 갱신)
관련 문서
- •[[global-id-generator|글로벌 ID 생성기]]
- •[[distributed-counter|분산 카운터]]
- •[[url-shortener-design|URL 단축기 설계]]