本文主要研究一下dubbo-go-proxy的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
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()
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()。