How apache/servicecomb-service-center works
Project Type and Purpose
• Service-Center is a distributed, full-stack micro-service registry and management platform.
• It exposes REST (and WebSocket) APIs, a CLI (scctl), a cluster syncer, an event engine, and a web portal/UX.
• It supports pluggable authentication/TLS, metrics (Prometheus), tracing (Zipkin), multiple data sources (etcd, MongoDB), and multi-cluster sync.
Break Down the Main Components
A. User-Facing Components
1. Web Portal (ux/ and frontend/):
– Angular (ux/src) or AngularJS (frontend/app) UI
– Communicates via HTTP/REST and WebSocket to the server.
2. Command-Line Client (scctl/ and cmd/scctl):
– CLI binary
– Talks over HTTP/REST or gRPC to the server.
3. Go Client Library (client/):
– A reusable Go SDK for REST/gRPC consumption by third-party services.
B. Server (server/):
1. API Layer (server/rest/ and server/handler/):
– HTTP routers, controllers, request/response marshalling
– WebSocket subscription handlers for “subscribe” events
2. Interceptor Chain (server/interceptor/ and pkg/chain/):
– Pre/post filters for auth, CORS, tracing, logging, rate limiting
3. Core Services (server/service/, server/core/):
– Business logic for service registration, discovery, metadata, RBAC, governance, quota, heartbeat
4. Plugin Manager (server/plugin/ and pkg/plugin/):
– Dynamically loadable modules for auth, TLS, tracing, metrics, UUID, etc.
5. Event & Pub/Sub Engine (server/event/, server/pubsub/):
– Internal event bus, WebSocket broker, subscription model
6. Metrics & Tracing (pkg/metrics/, pkg/prometheus/, pkg/log/, pkg/tracing):
– Exposes Prometheus endpoints, integrates with Zipkin, logs via Zap
C. Persistence & State Layer (datasource/):
1. Pluggable Data Sources (datasource/etcd/ and datasource/mongo/):
– Adapter, engine, state management, key generation, cache, watchers
2. Common Abstractions (datasource/common.go, datasource/sdcommon/):
– Registry interfaces, cache, list-watch patterns
3. Stateful Store (etcd cluster or MongoDB cluster)
D. Cluster Synchronization (syncer/):
– A separate process that streams events across clusters via gRPC or REST,
keeping multiple Service-Center instances in sync.
E. Event Base (eventbase/):
– Lightweight event dispatcher for background tasks and tombstone cleanup.
F. Infrastructure & Examples:
– Dockerfiles, Helm charts (examples/infrastructures/k8s), docker-compose.yaml
Sketching the Relationships & Data Flows
• “Users & Tools” at the top: Web browsers (UI), CLI (scctl), custom clients (Go SDK).
• API Layer → Interceptor Chain → Core Service Layer → Data Source Adapter
• Core Service publishes events to the Event Bus → Pub/Sub module → WebSocket
• Metrics & Tracing modules hooked into Interceptors and Core Services.
• Plugin Manager sits beside Interceptor and Core layers, invoked dynamically.
• At the bottom or side, show external systems: Prometheus scraping metrics endpoint, Zipkin receiving spans.
Highlighting Architectural Patterns & Technologies
• Layered architecture: Presentation → API → Interceptors → Business Logic → Data Access
• Interceptor/Chain of Responsibility pattern (pkg/chain)
• Plugin architecture (pkg/plugin, server/plugin)
• Pub/Sub and Observer patterns (server/event, server/pubsub)
• MVC-ish division in the frontend (controllers, views, directives)
• Go-based microservices: gRPC (protobuf in api/, syncer/api), HTTP/REST (server/rest, pkg/rest), WebSocket (server/pubsub/ws)
• Caching layer in front of the datastore for performance
• Watch/List patterns for data sync from etcd/Mongo to in-memory cache.
Essential Elements
• Clear labels on each component (e.g., “REST Controller”, “Auth Interceptor”, “Registry Service”, “etcd cluster”)
• Plugin extension points in Server (Plugin Loader → Auth/TLS/Tracing)
• Metrics/tracing integration to Prometheus/Zipkin.