如何用Golang实现观察者与发布订阅模式_Golang 发布订阅模式应用技巧

观察者模式通过主题维护观察者列表并在状态变化时通知,发布订阅模式利用channel和EventBus解耦发布与订阅;两者均适用于消息广播、状态同步等场景。

观察者模式和发布订阅模式在事件驱动系统中非常常见,Golang 虽然没有内置的事件机制,但通过接口、channel 和 goroutine 可以轻松实现这两种模式。它们常用于解耦模块、实现消息广播、状态同步等场景。

观察者模式的基本实现

观察者模式定义了一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖它的对象都会收到通知。

核心结构包含两个角色:主题(Subject)和观察者(Observer)。主题维护观察者列表,并在状态变化时通知它们。

示例代码:

type Observer interface {
    Update(message string)
}

type Subject struct { observers []Observer state string }

func (s *Subject) Attach(o Observer) { s.observers = append(s.observers, o) }

func (s *Subject) Notify() { for _, o := range s.observers { o.Update(s.state) } }

func (s *Subject) SetState(state string) { s.state = state s.Notify() }

type ConcreteObserver struct { name string }

func (co *ConcreteObserver) Update(message string) { fmt.Printf("[%s] 收到通知: %s\n", co.name, message) }

使用方式:创建主题,注册多个观察者,调用 SetState 后所有观察者自动更新。

发布订阅模式的实现(基于 Channel)

发布订阅模式更进一步,通过中间的“事件总线”解耦发布者和订阅者。Golang 的 channel 非常适合这种异步通信。

与观察者模式不同,发布者不直接维护订阅者列表,而是将消息发送到某个 topic 的 channel 中,由调度器转发给订阅者。

实现思路:

  • 定义一个 EventBus,管理多个 topic 对应的 channel
  • 订阅者通过 Subscribe 注册到指定 topic
  • 发布者通过 Publish 向 topic 发送消息
  • 使用 goroutine 异步处理消息,避免阻塞

type EventBus struct {
    subscribers map[string][]chan string
    mutex       sync.RWMutex
}

func NewEventBus() *EventBus { return &EventBus{ subscribers: make(map[string][]chan string), } }

func (eb *EventBus) Subscribe(topic string) <-chan string { ch := make(chan string, 10) // 缓冲 channel 避免阻塞 eb.mutex.Lock() eb.subscribers[topic] = append(eb.subscribers[topic], ch) eb.mutex.Unlock() return ch }

func (eb *EventBus) Publish(topic, message string) { eb.mutex.RLock() subs := eb.subscribers[topic] eb.mutex.RUnlock()

for _, ch := range subs {
    select {
    case ch <- message:
    default:
        // 防止因消费者慢导致阻塞
    }
}

}

实际应用场景与技巧

这类模式广泛应用于日志系统、配置热更新、微服务通信、UI 状态同步等。

技巧一:使用 goroutine 异步通知
避免在主线程中同步调用所有观察者,影响性能。可以为每个通知启动 goroutine:

for _, o := range s.observers {
    go o.Update(s.state) // 异步执行
}

技巧二:支持 topic 匹配(通配符)
高级消息系统常支持模糊订阅,如 "user.*" 匹配 "user.create" 和 "user.delete"。可通过正则或前缀匹配扩展 EventBus。

技巧三:资源清理
长时间运行的服务需注意 channel 泄漏。可提供 Unsubscribe 方法,并使用 context 控制生命周期:

func (eb *EventBus) Unsubscribe(topic string, ch <-chan string) {
    eb.mutex.Lock()
    defer eb.mutex.Unlock()
    // 移除对应 channel
}

技巧四:结合 context 实现超时控制
在发布消息时加入 context,防止某些订阅者处理过慢拖累整体:

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

select { case ch <- message: case <-ctx.Done(): // 跳过慢速订阅者 }

基本上就这些。观察者模式适合对象间紧耦合的通知,发布订阅更适合跨模块、异步、松耦合的通信。Golang 凭借 channel 和并发模型,实现起来简洁高效。关键是根据业务选择合适粒度的 topic 和合理的缓冲策略,避免阻塞和泄漏。