sequence

This paper mainly studies DubboProtocol of Dubbo-Go

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
}
Copy the code
  • DubboProtocol nested protocol.BaseProtocol defines the serverMap and serverLock attributes

GetProtocol

Dubbo – go – v1.4.2 / protocol/dubbo dubbo_protocol. Go

// GetProtocol ...
func GetProtocol() protocol.Protocol {
	if dubboProtocol == nil {
		dubboProtocol = NewDubboProtocol()
	}
	return dubboProtocol
}
Copy the code
  • The GetProtocol method creates a dubboProtocol using NewDubboProtocol

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),
	}
}
Copy the code
  • The NewDubboProtocol method instantiates 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
}
Copy the code
  • The Export method creates a exporter through NewDubboExporter, updates it to the exporterMap of DubboProtocol, and then executes the openServer of DubboProtocol

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() } }Copy the code
  • The openServer method first obtains a Server from serverMap based on url.location. Otherwise, run 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
}
Copy the code
  • The Refer method gets a requestTimeout, then creates an Invoker with NewDubboInvoker, and then executes 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()
	}
}
Copy the code
  • Destroy: dp.baseprotocol.destroy (), dp.servermap: delete(dp.servermap, key), server.stop ()

summary

DubboProtocol nested protocol.BaseProtocol defines serverMap and serverLock attributes. The Export method creates a exporter through NewDubboExporter, updates it to the exporterMap of DubboProtocol, and executes the openServer of DubboProtocol. The Refer method gets a requestTimeout, creates an Invoker with NewDubboInvoker, and then executes dp.setInvokers (Invoker). Destroy: dp.baseprotocol.destroy (), dp.servermap: delete(dp.servermap, key), server.stop ()

doc

  • dubbo_protocol