Loading...
Loading...
Use when designing microservices architectures, decomposing monoliths, implementing service boundaries, inter-service communication, or asking about "microservices", "service decomposition", "API gateway", "circuit breaker", "distributed systems"
npx skill4agent add eyadsibai/ltk microservices-patternsfrom tenacity import retry, stop_after_attempt, wait_exponential
class ServiceClient:
def __init__(self, base_url: str):
self.base_url = base_url
self.client = httpx.AsyncClient(timeout=5.0)
@retry(stop=stop_after_attempt(3), wait=wait_exponential(min=2, max=10))
async def get(self, path: str, **kwargs):
response = await self.client.get(f"{self.base_url}{path}", **kwargs)
response.raise_for_status()
return response.json()from aiokafka import AIOKafkaProducer
class EventBus:
async def publish(self, event: DomainEvent):
await self.producer.send_and_wait(
event.event_type,
value=asdict(event),
key=event.aggregate_id.encode()
)class CircuitBreaker:
def __init__(self, failure_threshold=5, recovery_timeout=30):
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.state = CircuitState.CLOSED
async def call(self, func, *args, **kwargs):
if self.state == CircuitState.OPEN:
if self._should_attempt_reset():
self.state = CircuitState.HALF_OPEN
else:
raise CircuitBreakerOpenError()
try:
result = await func(*args, **kwargs)
self._on_success()
return result
except Exception:
self._on_failure()
raiseclass APIGateway:
@circuit(failure_threshold=5, recovery_timeout=30)
async def call_service(self, service_url: str, path: str, **kwargs):
response = await self.http_client.request("GET", f"{service_url}{path}", **kwargs)
response.raise_for_status()
return response.json()
async def aggregate(self, order_id: str) -> dict:
# Parallel requests to multiple services
order, payment, inventory = await asyncio.gather(
self.call_order_service(f"/orders/{order_id}"),
self.call_payment_service(f"/payments/order/{order_id}"),
self.call_inventory_service(f"/reservations/order/{order_id}"),
return_exceptions=True
)
return {"order": order, "payment": payment, "inventory": inventory}