sequence

This paper mainly studies the remoteFilter of Dubo-Go-proxy

remoteFilter

dubbo-go-proxy/pkg/filter/remote/call.go

func Init() {
	extension.SetFilterFunc(constant.RemoteCallFilter, remoteFilterFunc())
}

func remoteFilterFunc() fc.FilterFunc {
	return New(defaultNewParams()).Do()
}

func defaultNewParams() mockLevel {
	mock := 1
	mockStr := os.Getenv(constant.EnvMock)
	if len(mockStr) > 0 {
		i, err := strconv.Atoi(mockStr)
		if err == nil {
			mock = i
		}
	}

	return mockLevel(mock)
}

type mockLevel int8

const (
	open = iota
	close
	all
)

// clientFilter is a filter for recover.
type clientFilter struct {
	level mockLevel
}

// New create timeout filter.
func New(level mockLevel) filter.Filter {
	if level < 0 || level > 2 {
		level = close
	}
	return &clientFilter{
		level: level,
	}
}
Copy the code

ClientFilter registers filter func with extension named dgp.filters. Remote_call; The func executes the Do method

Do

dubbo-go-proxy/pkg/filter/remote/call.go

func (f clientFilter) Do() fc.FilterFunc { return func(c fc.Context) { f.doRemoteCall(c.(*contexthttp.HttpContext)) } } func (f clientFilter) doRemoteCall(c *contexthttp.HttpContext) { api := c.GetAPI() if (f.level == open && api.Mock) || (f.level == all) { c.SourceResp = &filter.ErrResponse{ Message: "mock success", } c.Next() return } typ := api.Method.IntegrationRequest.RequestType cli, err := matchClient(typ) if err ! = nil { c.Err = err return } resp, err := cli.Call(client.NewReq(c.Ctx, c.Request, *api)) if err ! = nil { logger.Errorf("[dubbo-go-proxy] client call err:%v!" , err) c.Err = err return } logger.Debugf("[dubbo-go-proxy] client call resp:%v", resp) c.SourceResp = resp // response write in response filter. c.Next() } func matchClient(typ config.RequestType) (client.Client, error) { switch strings.ToLower(string(typ)) { case string(config.DubboRequest): return dubbo.SingletonDubboClient(), nil case string(config.HTTPRequest): return clienthttp.SingletonHTTPClient(), nil default: return nil, errors.New("not support") } }Copy the code

The Do method executes Do Remotecall, which determines whether it is a mock and returns mock Success if it is; The RequestType checks whether the RequestType is supported and returns the corresponding client. Finally, the cli.Call command is used to execute the request

Client

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

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)
}
Copy the code

The Client interface, which defines Init, Close, Call, and MapParams methods, has two implementation classes, dubo. Client and http.client

summary

The clientFilter of dubo-Go-proxy registers the filter func named dgp.filters. Remote_call with extension; The func performs the Do method, which determines whether the RequestType is supported based on the RequestType and returns the corresponding client. Finally, the func performs the request through cli.call.

doc

  • dubbo-go-proxy