编程知识 cdmana.com

Talking about client of Dubbo go proxy

order

This paper mainly studies dubbo-go-proxy Of Client

Client

dubbo-go-proxy/pkg/client/client.go

// Client represents the interface of http/dubbo clients
type Client interface {
	Init() error
	Close() error

	// Call invoke the downstream service.
	Call(req *Request) (res interface{}, err error)

	// MapParams mapping param, uri, query, body ...
	MapParams(req *Request) (reqData interface{}, err error)
}

Client Interface defined Init、Close、Call、MapParams Method

Client

dubbo-go-proxy/pkg/client/dubbo/dubbo.go

// Client client to generic invoke dubbo
type Client struct {
	lock               sync.RWMutex
	GenericServicePool map[string]*dg.GenericService
}

Client Defined lock、GenericServicePool attribute , It has achieved Client Interface

Init

dubbo-go-proxy/pkg/client/dubbo/dubbo.go

// Init init dubbo, config mapping can do here
func (dc *Client) Init() error {
	dc.GenericServicePool = make(map[string]*dg.GenericService, 4)

	cls := config.GetBootstrap().StaticResources.Clusters

	// dubbogo comsumer config
	dgCfg = dg.ConsumerConfig{
		Check:      new(bool),
		Registries: make(map[string]*dg.RegistryConfig, 4),
	}
	dgCfg.ApplicationConfig = defaultApplication
	for i := range cls {
		c := cls[i]
		dgCfg.Request_Timeout = c.RequestTimeoutStr
		dgCfg.Connect_Timeout = c.ConnectTimeoutStr
		for k, v := range c.Registries {
			if len(v.Protocol) == 0 {
				logger.Warnf("can not find registry protocol config, use default type 'zookeeper'")
				v.Protocol = defaultDubboProtocol
			}
			dgCfg.Registries[k] = &dg.RegistryConfig{
				Protocol:   v.Protocol,
				Address:    v.Address,
				TimeoutStr: v.Timeout,
				Username:   v.Username,
				Password:   v.Password,
			}
		}
	}

	initDubbogo()

	return nil
}

func initDubbogo() {
	dg.SetConsumerConfig(dgCfg)
	dg.Load()
}

Init The main approach is to build ConsumerConfig, And then execute initDubbogo

Close

dubbo-go-proxy/pkg/client/dubbo/dubbo.go

// Close clear GenericServicePool.
func (dc *Client) Close() error {
	dc.lock.Lock()
	defer dc.lock.Unlock()
	for k := range dc.GenericServicePool {
		delete(dc.GenericServicePool, k)
	}
	return nil
}

Close Method traverses through locking dc.GenericServicePool perform delete operation

Call

dubbo-go-proxy/pkg/client/dubbo/dubbo.go

// Call invoke service
func (dc *Client) Call(req *client.Request) (res interface{}, err error) {
	types, values, err := dc.genericArgs(req)
	if err != nil {
		return nil, err
	}

	dm := req.API.Method.IntegrationRequest
	method := dm.Method

	logger.Debugf("[dubbo-go-proxy] dubbo invoke, method:%s, types:%s, reqData:%v", method, types, values)

	gs := dc.Get(dm)

	rst, err := gs.Invoke(req.Context, []interface{}{method, types, values})

	if err != nil {
		return nil, err
	}

	logger.Debugf("[dubbo-go-proxy] dubbo client resp:%v", rst)

	return rst, nil
}

Call Methods by dc.Get(dm) obtain GenericService, And then through GenericService.Invoke For the request

MapParams

dubbo-go-proxy/pkg/client/dubbo/dubbo.go

// MapParams params mapping to api.
func (dc *Client) MapParams(req *client.Request) (interface{}, error) {
	r := req.API.Method.IntegrationRequest
	if len(r.ParamTypes) != len(r.MappingParams) {
		return nil, errors.New("Numbers of param types and paramMappings are not the same")
	}
	var values []interface{}
	for _, mappingParam := range r.MappingParams {
		source, _, err := client.ParseMapSource(mappingParam.Name)
		if err != nil {
			return nil, err
		}
		if mapper, ok := mappers[source]; ok {
			if err := mapper.Map(mappingParam, req, &values, buildOption(mappingParam)); err != nil {
				return nil, err
			}
		}
	}
	return values, nil
}

MapParams Methods through MappingParams, adopt client.ParseMapSource obtain source, Re pass mappers[source] obtain mapper, Finally through mapper Of Map Method to convert

Summary

dubbo-go-proxy Of client.Client Interface defined Init、Close、Call、MapParams Method ; Its dubbo.Client Realized client.Client Interface ; It's mainly through mapper Parameter conversion , And then through GenericService.Invoke For the request .

doc

版权声明
本文为[go4it]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/01/20210131220731887v.html

Scroll to Top