系统列表
Distributed Systems in Go:生产级分布式平台 SDK 与微服务实战
专栏《分布式 Go 系统工程》配套完整代码实战工程,涵盖并发模型、消息队列适配、Saga 编排、弹性容错与可观测性等生产级模式。
README.mdSYSTEM.mdcmd/auth/main.gocmd/gateway/main.gocmd/orders/main.goconfig/grafana/datasources/datasources.yamlconfig/prometheus.yamlconfig/tempo.yamldeploy/kube/orderflow-infra.yamldocker-compose.ymlgo.modgo.suminternal/auth/handler.gointernal/auth/service.gointernal/domain/events.gointernal/inventory/handler.gointernal/inventory/service.gointernal/notifications/service.gointernal/orders/handler.gointernal/orders/service.gointernal/payment/service.gomigrations/000001_init_schema.up.sqlpkg/platform/auth/auth_test.gopkg/platform/auth/claims.gopkg/platform/auth/jwks.gopkg/platform/auth/middleware.gopkg/platform/auth/password.gopkg/platform/auth/token.gopkg/platform/cli/cli_test.gopkg/platform/cli/health.gopkg/platform/concurrency/context.gopkg/platform/concurrency/shutdown.gopkg/platform/concurrency/shutdown_test.gopkg/platform/concurrency/workerpool.gopkg/platform/concurrency/workerpool_test.gopkg/platform/config/config_test.gopkg/platform/config/loader.gopkg/platform/consistency/consistency_test.gopkg/platform/consistency/idempotency.gopkg/platform/consistency/optimistic.gopkg/platform/consistency/saga.gopkg/platform/ctxutil/context.gopkg/platform/ctxutil/context_test.gopkg/platform/ctxutil/keys.gopkg/platform/database/database_test.gopkg/platform/database/errors.gopkg/platform/database/migrate.gopkg/platform/database/pool.gopkg/platform/database/retry.gopkg/platform/database/tx.gopkg/platform/messaging/fake.gopkg/platform/messaging/fake_test.gopkg/platform/messaging/interfaces.gopkg/platform/messaging/kafka/consumer.gopkg/platform/messaging/kafka/producer.gopkg/platform/messaging/nats/publisher.gopkg/platform/messaging/nats/subscriber.gopkg/platform/messaging/schema.gopkg/platform/middleware/bodyclose.gopkg/platform/middleware/chain.gopkg/platform/middleware/cors.gopkg/platform/middleware/errors.gopkg/platform/middleware/health.gopkg/platform/middleware/logging.gopkg/platform/middleware/middleware_test.gopkg/platform/middleware/recover.gopkg/platform/middleware/requestid.gopkg/platform/middleware/timeout.gopkg/platform/observability/context.gopkg/platform/observability/logger.gopkg/platform/observability/metrics.gopkg/platform/observability/observability_test.gopkg/platform/observability/tracing.gopkg/platform/orchestration/budget.gopkg/platform/orchestration/degrade.gopkg/platform/orchestration/fanout.gopkg/platform/orchestration/orchestration_test.gopkg/platform/resilience/circuit.gopkg/platform/resilience/resilience_test.gopkg/platform/resilience/retry.gotests/live_infra_test.gotests/orderflow_integration_test.go
internal/orders/service.go
| package orders |
| import ( |
| "context" |
| "encoding/json" |
| "errors" |
| "fmt" |
| "log/slog" |
| "sync" |
| "time" |
| "distributed-systems-in-go/internal/domain" |
| "distributed-systems-in-go/pkg/platform/concurrency" |
| "distributed-systems-in-go/pkg/platform/database" |
| "distributed-systems-in-go/pkg/platform/messaging" |
| "github.com/google/uuid" |
| ) |
| var ( |
| ErrOrderNotFound = errors.New("order not found") |
| ) |
| type Order struct { |
| ID string `json:"id"` |
| UserID string `json:"user_id"` |
| ProductID string `json:"product_id"` |
| Quantity int `json:"quantity"` |
| Amount float64 `json:"amount"` |
| Status string `json:"status"` // "pending", "paid", "cancelled" |
| Version int `json:"version"` |
| CreatedAt time.Time `json:"created_at"` |
| } |
| type OrderStore interface { |
| Create(ctx context.Context, o *Order) error |
| GetByID(ctx context.Context, id string) (*Order, error) |
| UpdateStatus(ctx context.Context, id, status string, expectedVersion int) error |
| } |
| type MemoryOrderStore struct { |
| mu sync.RWMutex |
| orders map[string]*Order |
| } |
| func NewMemoryOrderStore() *MemoryOrderStore { |
| return &MemoryOrderStore{orders: make(map[string]*Order)} |
| } |
| func (s *MemoryOrderStore) Create(ctx context.Context, o *Order) error { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| copied := *o |
| s.orders[o.ID] = &copied |
| return nil |
| } |
| func (s *MemoryOrderStore) GetByID(ctx context.Context, id string) (*Order, error) { |
| s.mu.RLock() |
| defer s.mu.RUnlock() |
| o, ok := s.orders[id] |
| if !ok { |
| return nil, ErrOrderNotFound |
| } |
| copied := *o |
| return &copied, nil |
| } |
| func (s *MemoryOrderStore) UpdateStatus(ctx context.Context, id, status string, expectedVersion int) error { |
| s.mu.Lock() |
| defer s.mu.Unlock() |
| o, ok := s.orders[id] |
| if !ok { |
| return ErrOrderNotFound |
| } |
| if o.Version != expectedVersion { |
| return database.ErrConflict |
| } |
| o.Status = status |
| o.Version++ |
| return nil |
| } |
| // InventoryClient defines inventory interactions required by orders. |
| type InventoryClient interface { |
| ReserveInventory(ctx context.Context, productID string, quantity int) error |
| ReleaseReservation(ctx context.Context, productID string, quantity int) error |
| } |
| type CreateOrderRequest struct { |
| UserID string `json:"user_id"` |
| ProductID string `json:"product_id"` |
| Quantity int `json:"quantity"` |
| UnitPrice float64 `json:"unit_price"` |
| } |
| type Service struct { |
| orders OrderStore |
| inventory InventoryClient |
| publisher messaging.Publisher |
| } |
| func NewService(orders OrderStore, inv InventoryClient, pub messaging.Publisher) *Service { |
| return &Service{ |
| orders: orders, |
| inventory: inv, |
| publisher: pub, |
| } |
| } |
| func (s *Service) CreateOrder(ctx context.Context, req CreateOrderRequest) (*Order, error) { |
| if req.Quantity < 1 { |
| return nil, fmt.Errorf("quantity must be >= 1") |
| } |
| if req.ProductID == "" { |
| return nil, fmt.Errorf("product_id is required") |
| } |
| // Step 1: Synchronous inventory reservation |
| if err := s.inventory.ReserveInventory(ctx, req.ProductID, req.Quantity); err != nil { |
| return nil, fmt.Errorf("reserve inventory: %w", err) |
| } |
| order := &Order{ |
| ID: uuid.NewString(), |
| UserID: req.UserID, |
| ProductID: req.ProductID, |
| Quantity: req.Quantity, |
| Amount: float64(req.Quantity) * req.UnitPrice, |
| Status: "pending", |
| Version: 1, |
| CreatedAt: time.Now().UTC(), |
| } |
| // Step 2: Write order to repository |
| if err := s.orders.Create(ctx, order); err != nil { |
| // Mini-Saga compensation: release reserved inventory with detached timeout context |
| releaseCtx, cancel := concurrency.Detach(ctx, 5*time.Second) |
| defer cancel() |
| _ = s.inventory.ReleaseReservation(releaseCtx, req.ProductID, req.Quantity) |
| return nil, fmt.Errorf("create order: %w", err) |
| } |
| // Step 3: Publish domain event to broker |
| event := domain.OrderCreated{ |
| OrderID: order.ID, |
| UserID: order.UserID, |
| ProductID: order.ProductID, |
| Quantity: order.Quantity, |
| Amount: order.Amount, |
| CreatedAt: order.CreatedAt, |
| } |
| data, err := json.Marshal(event) |
| if err == nil && s.publisher != nil { |
| _ = s.publisher.Publish(ctx, "orders.created", data) |
| } |
| return order, nil |
| } |
| func (s *Service) HandlePaymentFailed(ctx context.Context, event domain.PaymentFailed) error { |
| order, err := s.orders.GetByID(ctx, event.OrderID) |
| if err != nil { |
| return fmt.Errorf("get order: %w", err) |
| } |
| // Idempotency check: if not pending, already handled |
| if order.Status != "pending" { |
| return nil |
| } |
| // 1. Cancel order |
| if err := s.orders.UpdateStatus(ctx, order.ID, "cancelled", order.Version); err != nil { |
| return fmt.Errorf("cancel order: %w", err) |
| } |
| // 2. Compensate inventory |
| if err := s.inventory.ReleaseReservation(ctx, order.ProductID, order.Quantity); err != nil { |
| slog.ErrorContext(ctx, "failed to release inventory on payment failure", "error", err, "order_id", order.ID) |
| } |
| // 3. Emit orders.cancelled event |
| cancelEvent := domain.OrderCancelled{ |
| OrderID: order.ID, |
| UserID: order.UserID, |
| Reason: event.Reason, |
| Cancelled: time.Now().UTC(), |
| } |
| data, _ := json.Marshal(cancelEvent) |
| if s.publisher != nil { |
| _ = s.publisher.Publish(ctx, "orders.cancelled", data) |
| } |
| return nil |
| } |
| func (s *Service) GetOrder(ctx context.Context, id string) (*Order, error) { |
| return s.orders.GetByID(ctx, id) |
| } |