将信息传递给订阅者;
Publish & Subscribe Messaging Pattern
发布/订阅消息模式
Publish-Subscribe is a messaging pattern used to communicate messages between different components without these components knowing anything about each other’s identity.
发布订阅是一种消息传递模式,用于在不同组件之间传递消息,而不需要知道彼此身份的任何组件。
It is similar to the Observer behavioral design pattern. The fundamental design principals of both Observer and Publish-Subscribe is the decoupling of those interested in being informed about Event Messages from the informer (Observers or Publishers). Meaning that you don’t have to program the messages to be sent directly to specific receivers.
它类似于观察者的行为设计模式。观察者和发布订阅的基本设计原则是将那些有意从告密者(观察者或发布者)获知事件消息的人解耦。这意味着你不需要编程直接发送给特定接收者的消息。
To accomplish this, an intermediary, called a “message broker” or “event bus”, receives published messages, and then routes them on to subscribers.
为了实现这一点,中介称为“消息代理”或“事件总线”,接收已发布的消息,然后将它们路由到订阅服务器。
There are three components messages, topics, users.
有三个组件消息、主题、用户。
1 | // 消息 |
1 | // 话题 |
1 | // 用户 |
Improvements 改进
Events can be published in a parallel fashion by utilizing stackless goroutines.
事件可以利用Stackless Goroutines以并行方式发表。
Performance can be improved by dealing with straggler subscribers by using a buffered inbox and you stop sending events once the inbox is full.
性能可以通过处理离散的用户采用缓冲的收件箱,你停止发送事件一旦收件箱是满的改进。