关于golang:聊聊dubbogoproxy的remoteFilter

61次阅读

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

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

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,}
}

clientFilter 往 extension 注册了名为 dgp.filters.remote_call 的 filter func;该 func 执行的是 Do 办法

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")
    }
}

Do 办法执行的是 doRemoteCall,该办法辉县判断是否是 mock,如果是则返回 mock success;之后依据 RequestType 判断是否反对该申请类型并返回对应的 client,最初通过 cli.Call 执行申请

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)
}

Client 接口定义了 Init、Close、Call、MapParams 办法,它有两个实现类,别离是 dubbo.Client 及 http.Client

小结

dubbo-go-proxy 的 clientFilter 往 extension 注册了名为 dgp.filters.remote_call 的 filter func;该 func 执行的是 Do 办法,该办法依据 RequestType 判断是否反对该申请类型并返回对应的 client,最初通过 cli.Call 执行申请。

doc

  • dubbo-go-proxy

正文完
 0