Lesson 22 of 40
Architecture
Expert
65 min
Microservices Architecture
Design and implement a microservices system with service discovery, API gateways, event-driven communication, and distributed tracing.
Part 1: Service Communication Patterns
| Pattern | Use When | Technology |
|---|---|---|
| Synchronous | Immediate response needed | gRPC, HTTP |
| Async Messaging | Decouple services | Azure Service Bus, RabbitMQ |
| Event Streaming | Event log, replay | Apache Kafka, Azure Event Hub |
Part 2: Yarp API Gateway
// appsettings.json - YARP config
{
"ReverseProxy": {
"Routes": {
"orders-route": {
"ClusterId": "orders-cluster",
"Match": { "Path": "/api/orders/{**catch-all}" }
}
}
}
}
{
"ReverseProxy": {
"Routes": {
"orders-route": {
"ClusterId": "orders-cluster",
"Match": { "Path": "/api/orders/{**catch-all}" }
}
}
}
}
Part 3: Resilience with Polly v8
builder.Services.AddHttpClient<OrderClient>()
.AddResilienceHandler("pipeline", pipeline =>
{
pipeline.AddRetry(new() { MaxRetryAttempts = 3 });
pipeline.AddCircuitBreaker(new() { FailureRatio = 0.5 });
pipeline.AddTimeout(TimeSpan.FromSeconds(10));
});
.AddResilienceHandler("pipeline", pipeline =>
{
pipeline.AddRetry(new() { MaxRetryAttempts = 3 });
pipeline.AddCircuitBreaker(new() { FailureRatio = 0.5 });
pipeline.AddTimeout(TimeSpan.FromSeconds(10));
});
Part 4: OpenTelemetry Distributed Tracing
builder.Services.AddOpenTelemetry()
.WithTracing(b => b
.AddAspNetCoreInstrumentation()
.AddEntityFrameworkCoreInstrumentation()
.AddOtlpExporter());
// Traces appear in Aspire Dashboard / Jaeger / Zipkin
.WithTracing(b => b
.AddAspNetCoreInstrumentation()
.AddEntityFrameworkCoreInstrumentation()
.AddOtlpExporter());
// Traces appear in Aspire Dashboard / Jaeger / Zipkin