본문으로 건너뛰기

API Gateway

개요

API Gateway는 모든 클라이언트 요청의 단일 진입점으로, 19개의 백엔드 마이크로서비스로 요청을 라우팅하는 리버스 프록시 역할을 수행합니다.

항목내용
언어Go 1.21+
프레임워크Gin
데이터베이스ElastiCache (Valkey) - Rate Limiting
네임스페이스core-services
포트8080
헬스체크/healthz, /readyz

아키텍처

주요 기능

1. 리버스 프록시

  • 경로 prefix 기반 서비스 라우팅
  • 자동 서비스 디스커버리 (Kubernetes DNS)
  • 업스트림 에러 핸들링

2. Rate Limiting

  • Token Bucket 알고리즘 구현
  • API Key 또는 Client IP 기반 제한
  • ElastiCache Valkey를 통한 분산 카운터

3. 리전 포워딩

  • Secondary 리전에서 쓰기 요청 감지
  • Primary 리전으로 자동 포워딩
  • X-Forwarded-Region 헤더 전파

API 엔드포인트

프록시 라우팅

메서드경로설명백엔드 서비스
ANY/api/v1/products/*상품 카탈로그product-catalog.core-services
ANY/api/v1/search/*검색search.core-services
ANY/api/v1/cart/*장바구니cart.core-services
ANY/api/v1/orders/*주문order.core-services
ANY/api/v1/payments/*결제payment.core-services
ANY/api/v1/inventory/*재고inventory.core-services
ANY/api/v1/auth/*인증user-account.user-services
ANY/api/v1/profiles/*프로필user-profile.user-services
ANY/api/v1/wishlists/*위시리스트wishlist.user-services
ANY/api/v1/reviews/*리뷰review.user-services
ANY/api/v1/shipments/*배송shipping.fulfillment
ANY/api/v1/warehouses/*창고warehouse.fulfillment
ANY/api/v1/returns/*반품returns.fulfillment
ANY/api/v1/pricing/*가격pricing.business-services
ANY/api/v1/recommendations/*추천recommendation.business-services
ANY/api/v1/notifications/*알림notification.business-services
ANY/api/v1/sellers/*판매자seller.business-services
ANY/api/v1/events/*이벤트 버스event-bus.platform
ANY/api/v1/analytics/*분석analytics.platform

Rate Limit 응답 헤더

X-RateLimit-Limit: 6000
X-RateLimit-Remaining: 5999
Retry-After: 60

Rate Limit 초과 응답

{
"error": "rate limit exceeded",
"limit": 6000,
"window": 60,
"retryIn": 60
}

데이터 모델

Route Map

// GetRouteMap returns the mapping of API path prefixes to backend services
func GetRouteMap() map[string]string {
return map[string]string{
// Core Services
"/api/v1/products": "product-catalog.core-services.svc.cluster.local:80",
"/api/v1/search": "search.core-services.svc.cluster.local:80",
"/api/v1/cart": "cart.core-services.svc.cluster.local:80",
"/api/v1/orders": "order.core-services.svc.cluster.local:80",
"/api/v1/payments": "payment.core-services.svc.cluster.local:80",
"/api/v1/inventory": "inventory.core-services.svc.cluster.local:80",
// ... 추가 서비스들
}
}

Config

type Config struct {
ServiceName string
Port string
AWSRegion string
RegionRole string // PRIMARY or SECONDARY
PrimaryHost string
CacheHost string
CachePort int
RateLimitRPS int // 초당 요청 제한
RateLimitBurst int // 버스트 허용량
RateLimitWindow int // 윈도우 크기 (초)
}

이벤트 (Kafka)

API Gateway는 Kafka 이벤트를 직접 발행하거나 구독하지 않습니다. 모든 이벤트 처리는 백엔드 서비스에서 수행됩니다.

환경 변수

변수명설명기본값
PORT서버 포트8080
AWS_REGIONAWS 리전us-east-1
REGION_ROLE리전 역할 (PRIMARY/SECONDARY)PRIMARY
PRIMARY_HOSTPrimary 리전 호스트-
CACHE_HOSTElastiCache 호스트localhost
CACHE_PORTElastiCache 포트6379
RATE_LIMIT_RPS초당 요청 제한100
RATE_LIMIT_BURST버스트 허용량200
RATE_LIMIT_WINDOWRate Limit 윈도우 (초)60
LOG_LEVEL로그 레벨info

서비스 의존성

의존하는 서비스

  • ElastiCache (Valkey): Rate Limiting 카운터 저장
  • 모든 백엔드 서비스: 프록시 대상

이 서비스에 의존하는 컴포넌트

  • 웹/모바일 클라이언트: 모든 API 요청
  • CloudFront: CDN 오리진
  • ALB: 로드 밸런싱

미들웨어 체인

  1. Recovery: 패닉 복구
  2. OTel Tracing: 분산 추적
  3. JSON Logger: 구조화된 로깅
  4. Rate Limiter: 요청 제한 (Valkey 사용 가능 시)
  5. Region Forward: 쓰기 요청 Primary 포워딩
  6. Reverse Proxy: 백엔드 서비스 라우팅

에러 응답

404 Not Found

{
"error": "no route found"
}

502 Bad Gateway

{
"error": "upstream unavailable"
}

429 Too Many Requests

{
"error": "rate limit exceeded",
"limit": 6000,
"window": 60,
"retryIn": 60
}