diff --git a/client/ehttp/component.go b/client/ehttp/component.go index 27a797fb..63ee1783 100644 --- a/client/ehttp/component.go +++ b/client/ehttp/component.go @@ -3,18 +3,15 @@ package ehttp import ( "net" "net/http" - "net/http/cookiejar" "net/url" "strings" "time" "github.com/go-resty/resty/v2" "github.com/gotomicro/ego/client/ehttp/resolver" - "github.com/gotomicro/ego/core/eregistry" - "golang.org/x/net/publicsuffix" - "github.com/gotomicro/ego/core/eapp" "github.com/gotomicro/ego/core/elog" + "github.com/gotomicro/ego/core/eregistry" ) // PackageName 设置包名 @@ -44,21 +41,20 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen addr = strings.ReplaceAll(config.Addr, egoTarget.Scheme+"://", "http://") } builder := resolver.Get(egoTarget.Scheme) - resolver, err := builder.Build(addr) + resolverBuild, err := builder.Build(addr) if err != nil { elog.Panic("build resolver error", elog.FieldErr(err), elog.FieldKey(config.Addr)) } // resty的默认方法,无法设置长连接个数,和是否开启长连接,这里重新构造http client。 - cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) interceptors := []interceptor{fixedInterceptor, logInterceptor, metricInterceptor, traceInterceptor} - cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: cookieJar}). + cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: config.cookieJar}). SetDebug(config.RawDebug). SetTimeout(config.ReadTimeout). SetHeader("app", eapp.Name()). SetBaseURL(addr) for _, interceptor := range interceptors { - onBefore, onAfter, onErr := interceptor(name, config, logger, resolver) + onBefore, onAfter, onErr := interceptor(name, config, logger, resolverBuild) if onBefore != nil { cli.OnBeforeRequest(onBefore) } diff --git a/client/ehttp/config.go b/client/ehttp/config.go index e34b2b0e..4b5d2a41 100644 --- a/client/ehttp/config.go +++ b/client/ehttp/config.go @@ -1,6 +1,7 @@ package ehttp import ( + "net/http" "regexp" "runtime" "time" @@ -10,20 +11,21 @@ import ( // Config HTTP配置选项 type Config struct { - Addr string // 连接地址 - Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据 - RawDebug bool // 是否开启原生调试,默认不开启 - ReadTimeout time.Duration // 读超时,默认2s - SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms - IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second - MaxIdleConns int // 设置最大空闲连接数 - MaxIdleConnsPerHost int // 设置长连接个数 - EnableTraceInterceptor bool // 是否开启链路追踪,默认开启 - EnableKeepAlives bool // 是否开启长连接,默认打开 - EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启 - EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启 - PathRelabel []Relabel // path 重命名 (metric 用) - EnableMetricsInterceptor bool // 是否开启监控,默认开启 + Addr string // 连接地址 + Debug bool // 是否开启调试,默认不开启,开启后并加上export EGO_DEBUG=true,可以看到每次请求,配置名、地址、耗时、请求数据、响应数据 + RawDebug bool // 是否开启原生调试,默认不开启 + ReadTimeout time.Duration // 读超时,默认2s + SlowLogThreshold time.Duration // 慢日志记录的阈值,默认500ms + IdleConnTimeout time.Duration // 设置空闲连接时间,默认90 * time.Second + MaxIdleConns int // 设置最大空闲连接数 + MaxIdleConnsPerHost int // 设置长连接个数 + EnableTraceInterceptor bool // 是否开启链路追踪,默认开启 + EnableKeepAlives bool // 是否开启长连接,默认打开 + EnableAccessInterceptor bool // 是否开启记录请求数据,默认不开启 + EnableAccessInterceptorRes bool // 是否开启记录响应参数,默认不开启 + PathRelabel []Relabel // path 重命名 (metric 用) + cookieJar http.CookieJar // 用于缓存cookie + EnableMetricsInterceptor bool // 是否开启监控,默认开启 } // Relabel ... diff --git a/client/ehttp/options.go b/client/ehttp/options.go index 49962683..d88cb575 100644 --- a/client/ehttp/options.go +++ b/client/ehttp/options.go @@ -1,6 +1,9 @@ package ehttp -import "time" +import ( + "net/http" + "time" +) // WithAddr 设置HTTP地址 func WithAddr(addr string) Option { @@ -99,3 +102,11 @@ func WithPathRelabel(match string, replacement string) Option { c.config.PathRelabel = append(c.config.PathRelabel, Relabel{Match: match, Replacement: replacement}) } } + +// WithJar 设置Cookie,设置后,请求第一次接口后获取Cookie,第二次请求会带上Cookie,适合一些登录场景 +// 例子:cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) +func WithJar(jar http.CookieJar) Option { + return func(c *Container) { + c.config.cookieJar = jar + } +}