在系统中,组件(服务、客户端)通过APIs和各种协议进行通信——同步(REST、gRPC)和异步(消息传递/队列)。理解组件如何进行通信是设计由多个部分组成的系统的基础。
同步通信(请求/响应)
text
The caller WAITS for a response (blocking):
REST (HTTP) → most common; resources over HTTP (JSON) → simple, ubiquitous, web-friendly
gRPC → high-performance RPC (binary, HTTP/2) → fast, typed; good for internal services
GraphQL → flexible queries (client requests exactly what it needs)
→ for: direct request/response where the caller needs an answer now
异步通信(消息传递)
text
The caller does NOT wait (decoupled, non-blocking):
MESSAGE QUEUES (RabbitMQ, SQS) → send a message; a consumer processes it later
EVENT STREAMING (Kafka) → publish events; consumers react
PUB/SUB → broadcast events to subscribers
→ for: decoupling, background work, handling spikes, event-driven systems
✓ resilience (queue buffers), scalability, decoupling ✗ more complexity, eventual consistency
