用途:
封装用于微服务之间的通信
接口:
type CMqttComm interface {
Connect(isReConnect bool)
SetMessageBus(host string, port int, username string, userpwd string)
Subscribe(action string, topic string, qos int, handler CHandler, user interface{}) error
UnSubscribe(action string, topic string) error
Send(action string, topic string, request string, qos int, timeout int) (response string, err error)
Get(topic string, request string, qos int, timeout int) (response string, err error)
Post(topic string, request string, qos int, timeout int) (response string, err error)
Put(topic string, request string, qos int, timeout int) (response string, err error)
Delete(topic string, request string, qos int, timeout int) (response string, err error)
Updated(topic string, request string, qos int) error
Deleted(topic string, request string, qos int) error
}
模拟的是 Restful 的风格
各个微服务之间仅仅需要关注自己所关注的请求, 再 CHandler 中处理
type CHandler interface {
Handle(topic string, request string, mc CMqttComm, user interface{}) (response string, err error)
}
所有请求 struct 都实现这个接口, 然后调用 Subscribe, 就可以接收到其他微服务发送过来的消息
每一个 handler 都是一个 goroutine
Send, Get, Post, 等接口都是同步接口, 可以直接拿到回复结果
源码位置: