序
本文主要研究一下dubbo-go-proxy的hostFilter
hostFilter
dubbo-go-proxy/pkg/filter/host/host.go
// hostFilter is a filter for host. type hostFilter struct { host string } // New create host filter. func New(host string) filter.Filter { return &hostFilter{host: host} }
hostFilter的New根据host创建hostFilter
Do
dubbo-go-proxy/pkg/filter/host/host.go
func (f hostFilter) Do() fc.FilterFunc { return func(c fc.Context) { f.doHostFilter(c.(*contexthttp.HttpContext)) } } func (f hostFilter) doHostFilter(c *contexthttp.HttpContext) { c.Request.Host = f.host c.Next() }
Do方法执行doHostFilter方法,该方法将f.host设置给c.Request.Host,然后执行c.Next()
httpFilter
dubbo-go-proxy/pkg/proxy/listener.go
func httpFilter(ctx *h.HttpContext, request fc.IntegrationRequest) { if len(request.Host) != 0 { ctx.AppendFilterFunc(host.New(request.Host).Do()) } if len(request.Path) != 0 { ctx.AppendFilterFunc(replacepath.New(request.Path).Do()) } }
httpFilter方法通过AppendFilterFunc将host.New(request.Host)追加到Filters中
小结
dubbo-go-proxy的hostFilter将f.host设置给c.Request.Host,然后执行c.Next()。
doc
- dubbo-go-proxy
有疑问加站长微信联系(非本文作者)