无处不在的发布订阅模式(TBD)
订阅一份报纸,到时候就会投递给你。
订阅一个新闻频道,一有新闻就会推送给你。
本质上,它是一种异步通信的架构模式
与之对应的代码模式就是观察者模式,其注册 register 对应于 subscribe, notify 对应于 publishe
- subscribe
subscriber -> router: subscribe router-->subscriber: ack
- publisher
publisher -> router: event router-->publisher: ack router --> subscriber: event subscviber-->router: ack
典型用例有
-
Ingestion user interaction and server events.
To use user interaction events from end-user apps or server events from your system, you might forward them to Pub/Sub. You can then use a stream processing tool, such as Dataflow, which delivers the events to databases. Examples of such databases are BigQuery, Cloud Bigtable, and Cloud Storage. Pub/Sub lets you gather events from many clients simultaneously. -
Real-time event distribution.
Events, raw or processed, may be made available to multiple applications across your team and organization for real- time processing. Pub/Sub supports an "enterprise event bus" and event-driven application design patterns. -
Replicating data among databases.
Pub/Sub is commonly used to distribute change events from databases. These events can be used to construct a view of the database state and state history in BigQuery and other data storage systems.
- Parallel processing and workflows.
You can efficiently distribute many tasks among multiple workers by using Pub/Sub messages to connect to Cloud Functions. Examples of such tasks are compressing text files, sending email notifications, evaluating AI models, and reformatting images.
- Enterprise event bus.
You can create an enterprise-wide real-time data sharing bus, distributing business events, database updates, and analytics events across your organization.
- Data streaming from applications, services, or IoT devices.
For example, a SaaS application can publish a real-time feed of events. Or, a residential sensor can stream data to Pub/Sub for use in other Google Cloud products through a Dataflow pipeline.
-
Refreshing distributed caches.
For example, an application can publish invalidation events to update the IDs of objects that have changed. -
Load balancing for reliability.
For example, instances of a service may be deployed on Compute Engine in multiple zones but subscribe to a common topic. When the service fails in any zone, the others can pick up the load automatically.
Reference
- https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
- https://cloud.google.com/pubsub/docs/overview
- https://learn.microsoft.com/en-us/azure/architecture/patterns/publisher-subscriber