关于dubbo:聊聊dubbogo的DubboProtocol

28次阅读

共计 3299 个字符,预计需要花费 9 分钟才能阅读完成。

本文次要钻研一下 dubbo-go 的 DubboProtocol

DubboProtocol

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

// dubbo protocol constant
const (
    // DUBBO ...
    DUBBO = "dubbo"
)

func init() {extension.SetProtocol(DUBBO, GetProtocol)
}

var (dubboProtocol *DubboProtocol)

// DubboProtocol ...
type DubboProtocol struct {
    protocol.BaseProtocol
    serverMap  map[string]*Server
    serverLock sync.Mutex
}
  • DubboProtocol 嵌套了 protocol.BaseProtocol,定义了 serverMap、serverLock 属性

GetProtocol

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

// GetProtocol ...
func GetProtocol() protocol.Protocol {
    if dubboProtocol == nil {dubboProtocol = NewDubboProtocol()
    }
    return dubboProtocol
}
  • GetProtocol 办法通过 NewDubboProtocol 创立 dubboProtocol

NewDubboProtocol

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

// NewDubboProtocol ...
func NewDubboProtocol() *DubboProtocol {
    return &DubboProtocol{BaseProtocol: protocol.NewBaseProtocol(),
        serverMap:    make(map[string]*Server),
    }
}
  • NewDubboProtocol 办法实例化了 DubboProtocol

Export

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

// Export ...
func (dp *DubboProtocol) Export(invoker protocol.Invoker) protocol.Exporter {url := invoker.GetUrl()
    serviceKey := url.ServiceKey()
    exporter := NewDubboExporter(serviceKey, invoker, dp.ExporterMap())
    dp.SetExporterMap(serviceKey, exporter)
    logger.Infof("Export service: %s", url.String())

    // start server
    dp.openServer(url)
    return exporter
}
  • Export 办法通过 NewDubboExporter 创立 exporter,而后更新到 DubboProtocol 的 exporterMap 中,之后执行 DubboProtocol 的 openServer

openServer

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

func (dp *DubboProtocol) openServer(url common.URL) {_, ok := dp.serverMap[url.Location]
    if !ok {_, ok := dp.ExporterMap().Load(url.ServiceKey())
        if !ok {panic("[DubboProtocol]" + url.Key() + "is not existing")
        }

        dp.serverLock.Lock()
        _, ok = dp.serverMap[url.Location]
        if !ok {srv := NewServer()
            dp.serverMap[url.Location] = srv
            srv.Start(url)
        }
        dp.serverLock.Unlock()}
}
  • openServer 办法先依据 url.Location 从 serverMap 获取 Server,获取不到则执行 dp.ExporterMap().Load(url.ServiceKey()),之后再次应用 dp.serverMap[url.Location] 获取,获取不到则执行 NewServer,放到 dp.serverMap 中,而后执行 srv.Start(url)

Refer

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

// Refer ...
func (dp *DubboProtocol) Refer(url common.URL) protocol.Invoker {
    //default requestTimeout
    var requestTimeout = config.GetConsumerConfig().RequestTimeout

    requestTimeoutStr := url.GetParam(constant.TIMEOUT_KEY, config.GetConsumerConfig().Request_Timeout)
    if t, err := time.ParseDuration(requestTimeoutStr); err == nil {requestTimeout = t}

    invoker := NewDubboInvoker(url, NewClient(Options{ConnectTimeout: config.GetConsumerConfig().ConnectTimeout,
        RequestTimeout: requestTimeout,
    }))
    dp.SetInvokers(invoker)
    logger.Infof("Refer service: %s", url.String())
    return invoker
}
  • Refer 办法先获取 requestTimeout,之后通过 NewDubboInvoker 创立 invoker,而后执行 dp.SetInvokers(invoker)

Destroy

dubbo-go-v1.4.2/protocol/dubbo/dubbo_protocol.go

// Destroy ...
func (dp *DubboProtocol) Destroy() {logger.Infof("DubboProtocol destroy.")

    dp.BaseProtocol.Destroy()

    // stop server
    for key, server := range dp.serverMap {delete(dp.serverMap, key)
        server.Stop()}
}
  • Destroy 办法先执行 dp.BaseProtocol.Destroy(),之后遍历 dp.serverMap,执行 delete(dp.serverMap, key) 及 server.Stop()

小结

DubboProtocol 嵌套了 protocol.BaseProtocol,定义了 serverMap、serverLock 属性;Export 办法通过 NewDubboExporter 创立 exporter,而后更新到 DubboProtocol 的 exporterMap 中,之后执行 DubboProtocol 的 openServer;Refer 办法先获取 requestTimeout,之后通过 NewDubboInvoker 创立 invoker,而后执行 dp.SetInvokers(invoker);Destroy 办法先执行 dp.BaseProtocol.Destroy(),之后遍历 dp.serverMap,执行 delete(dp.serverMap, key) 及 server.Stop()

doc

  • dubbo_protocol

正文完
 0