Documentation ¶
Index ¶
- Constants
- Variables
- func AddBeforeStopHook(handler func())
- func ClientIP(ctx context.Context) string
- func ClientIPHTTPMiddleware(next http.Handler) http.Handler
- func DecodeHTTPJSONRequest[T any](ctx context.Context, r *http.Request) (interface{}, error)
- func DecodeHTTPParamRequest[T any](ctx context.Context, r *http.Request) (interface{}, error)
- func DecodePB[T any](_ context.Context, req interface{}) (interface{}, error)
- func Dir(root string, listDirectory bool) fs.FS
- func DoStopHook()
- func EncodeHTTPJSONResponse(wrapper DataWrapper) httptransport.EncodeResponseFunc
- func EncodePB[T any](_ context.Context, resp interface{}) (interface{}, error)
- func FileHandler(prefix string, fs fs.FS) http.Handler
- func HandlerChain(h http.Handler, middlewares ...HTTPMiddleware) http.Handler
- func MustLoadConfig[T any](files ...string) T
- func MustNewEtcdV3Client(address []string) etcdv3.Client
- func NewDefaultEtcdV3Client() etcdv3.Client
- func OnlyFilesFS(fs fs.FS, listDirectory bool, root string) fs.FS
- func RequestEndpoint(ctx context.Context) string
- func RequestMethod(ctx context.Context) string
- func RequestProtocol(ctx context.Context) string
- func RequestStartTime(ctx context.Context) time.Time
- func ShouldBind(r *http.Request, obj any) error
- func ShouldBindJSON(r *http.Request, obj any) error
- func Start(svrs ...Server)
- func StartWithContext(ctx context.Context, svrs ...Server)
- func Stop()
- func TraceGRPC(ctx context.Context, md metadata.MD) (context.Context, string)
- func TraceGRPCClient(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, ...) error
- func TraceHTTPMiddleware(next http.Handler) http.Handler
- func TraceHTTPRequest(r *http.Request) (*http.Request, string)
- func TraceID(ctx context.Context) string
- func TraceIDOrNew(ctx context.Context) string
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type AccessLog
- type AccessLogOpt
- type Balancer
- type DataWrapper
- type Endpoint
- type Errno
- type EtcdV3Registrar
- type EtcdV3Selector
- type GRPCClient
- type GRPCServer
- type GRPCTransportServer
- type Gateway
- type GatewayConfig
- type GatewayOption
- type GatewayOptions
- type GatewayPlugin
- type GetValueFromContext
- type HTTPClient
- type HTTPHandler
- type HTTPMiddleware
- type HTTPRequest
- type HTTPResponse
- type HTTPServeMux
- type HTTPServer
- func (s HTTPServer) GetServiceInfo() *ServiceInfo
- func (s *HTTPServer) Handler(handlers ...HTTPHandler) *HTTPServer
- func (s *HTTPServer) Start() error
- func (s *HTTPServer) Static(pattern string, root string) *HTTPServer
- func (s *HTTPServer) StaticFS(pattern string, fs fs.FS) *HTTPServer
- func (s *HTTPServer) Stop() error
- func (s *HTTPServer) Use(middlewares ...HTTPMiddleware) *HTTPServer
- type HTTPTransportServer
- type HostPattern
- type LogGRPCErrorHandler
- type Middleware
- type Pattern
- type PrefixPattern
- type Protocol
- type RegisterHandler
- type RoundRobinBalancer
- type Route
- type Selector
- type SelectorBuilder
- type Server
- type ServerOption
- type ServerOptions
- type ServiceInfo
- type TraceGatewayPlugin
- type UnimplementedGatewayPlugin
- func (p *UnimplementedGatewayPlugin) AfterRoute(ctx context.Context, req *http.Request) (*http.Request, error)
- func (p *UnimplementedGatewayPlugin) BeforeRoute(ctx context.Context, req *http.Request) (*http.Request, error)
- func (p *UnimplementedGatewayPlugin) ErrorHandler(ctx context.Context, w http.ResponseWriter, req *http.Request, err error)
- func (p *UnimplementedGatewayPlugin) GetContextErr(ctx context.Context) error
- func (p *UnimplementedGatewayPlugin) ModifyResponse(ctx context.Context, res *http.Response) error
Constants ¶
const (
// TraceIDHeader traceID header key
TraceIDHeader = "X-Trace-ID"
)
Variables ¶
var ( // HTTPRequestHeaderCtxKey context http header HTTPRequestHeaderCtxKey = httpRequestHeaderKey{} // HTTPRequestURLCtxKey context http url HTTPRequestURLCtxKey = httpRequestURLKey{} // NopHTTPRequestDecoder 不需要解析请求参数 NopHTTPRequestDecoder = httptransport.NopRequestDecoder )
var ( // ErrNoServer 没有可用的服务节点 ErrNoServer = errors.New("no server available") )
var ( // ErrSystem 系统错误 ErrSystem = &Errno{Code: 500, HTTPCode: 500, Msg: "系统错误"} )
var (
// TraceIDCtxKey traceID context key
TraceIDCtxKey = traceIDKey{}
)
Functions ¶
func ClientIPHTTPMiddleware ¶ added in v1.0.5
ClientIPHTTPMiddleware 获取客户端IP
func DecodeHTTPJSONRequest ¶
DecodeHTTPJSONRequest 解析 http request body json 参数
func DecodeHTTPParamRequest ¶
DecodeHTTPParamRequest 解析 http request query 和 form 参数
func Dir ¶ added in v1.0.4
Dir 返回 http.FileSystem 实现 listDirectory = true,返回 http.Dir() listDirectory = false,返回 onlyFilesFS,不会显示目录
func EncodeHTTPJSONResponse ¶
func EncodeHTTPJSONResponse(wrapper DataWrapper) httptransport.EncodeResponseFunc
EncodeHTTPJSONResponse http 返回json数据 wrapper 对数据重新包装
func FileHandler ¶ added in v1.0.4
FileHandler 处理静态文件请求 参考 http.StripPrefix
func HandlerChain ¶
func HandlerChain(h http.Handler, middlewares ...HTTPMiddleware) http.Handler
HandlerChain 使用中间件包装 handler
func MustNewEtcdV3Client ¶
MustNewEtcdV3Client 创建 etcdv3.Client
func NewDefaultEtcdV3Client ¶
NewDefaultEtcdV3Client 创建默认 etcdv3.Client etcd 地址通过 GetDefaultEtcdAddress 方法获得
func OnlyFilesFS ¶ added in v1.0.4
OnlyFilesFS 将 fs.FS 包装为 onlyFilesFS root 可以设置根路径,文件将会从根路径开始查找
func RequestStartTime ¶
RequestStartTime 请求开始时间
func ShouldBindJSON ¶
ShouldBindJSON 从body解析json
func StartWithContext ¶
StartWithContext 启动服务
func TraceGRPCClient ¶
func TraceGRPCClient(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error
TraceGRPCClient grpc client 携带 traceID
func TraceHTTPMiddleware ¶
TraceHTTPMiddleware 链路跟踪
func TraceHTTPRequest ¶
TraceHTTPRequest 返回 traceID http 请求携带 traceID 处理
func TraceIDOrNew ¶
TraceIDOrNew 从 context 获得 TraceID,取不到则创建
Types ¶
type AccessLogOpt ¶
type AccessLogOpt struct { ContextFields map[string]GetValueFromContext PrintResp bool AccessLog AccessLog MaxDay int }
type Balancer ¶
type Balancer[T any] interface { // Refresh 刷新服务节点 Refresh(services []*T) // Pick 选择服务节点 Pick() (*T, error) }
Balancer 选择器负载策略
type EtcdV3Registrar ¶
type EtcdV3Registrar struct {
// contains filtered or unexported fields
}
EtcdV3Registrar 基于 etcd 实现的服务注册器
func NewEtcdV3Registrar ¶
func NewEtcdV3Registrar(servers ...Server) *EtcdV3Registrar
NewEtcdV3Registrar 创建一个服务注册器 servers 需要注册的服务
func (*EtcdV3Registrar) Deregister ¶
func (r *EtcdV3Registrar) Deregister()
Deregister 摘除服务注册信息并停止服务
type EtcdV3Selector ¶
type EtcdV3Selector struct {
// contains filtered or unexported fields
}
EtcdV3Selector 基于etcd3的节点查询器
func GetEtcdV3Selector ¶
func GetEtcdV3Selector(serviceName string) *EtcdV3Selector
GetEtcdV3Selector 创建 EtcdV3Selector
type GRPCClient ¶
type GRPCClient struct {
// contains filtered or unexported fields
}
GRPCClient 支持服务发现的 grpc client
func GetGRPCClient ¶
func GetGRPCClient(serviceName string, opts ...grpc.DialOption) *GRPCClient
GetGRPCClient 创建grpc客户端
func (*GRPCClient) Invoke ¶
func (c *GRPCClient) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error
Invoke 实现 Invoke 接口,支持服务发现
func (*GRPCClient) NewStream ¶
func (c *GRPCClient) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)
NewStream begins a streaming RPC.
type GRPCServer ¶
type GRPCServer struct {
// contains filtered or unexported fields
}
GRPCServer grpc server 实现
func NewGRPCServer ¶
func NewGRPCServer(opts ...ServerOption) *GRPCServer
NewGRPCServer 创建 grpc server opts 查看 ServerOptions
func (GRPCServer) GetServiceInfo ¶
func (s GRPCServer) GetServiceInfo() *ServiceInfo
func (*GRPCServer) RegisterService ¶
func (s *GRPCServer) RegisterService(reg RegisterHandler) *GRPCServer
RegisterService 注册 grpc 接口实现
type GRPCTransportServer ¶
type GRPCTransportServer = grpctransport.Server
GRPCTransportServer grpc transport server
func NewGRPCTransportServer ¶ added in v1.0.3
func NewGRPCTransportServer( e endpoint.Endpoint, dec grpctransport.DecodeRequestFunc, enc grpctransport.EncodeResponseFunc, options ...grpctransport.ServerOption, ) *GRPCTransportServer
NewGRPCTransportServer grpc handler 绑定 endpoint
type Gateway ¶
type Gateway struct { *httputil.ReverseProxy // contains filtered or unexported fields }
Gateway 网关服务
func NewGateway ¶
func NewGateway(cfg GatewayConfig, opts ...GatewayOption) *Gateway
NewGateway 创建 gateway 服务
func (Gateway) GetServiceInfo ¶
func (s Gateway) GetServiceInfo() *ServiceInfo
type GatewayConfig ¶
type GatewayConfig struct { ServerName string `json:"server-name"` // 服务名 Listen string `json:"listen"` // 监听地址 WebsocketPrefix string `json:"websocket-prefix-listen"` // websocket 连接路径 DiscoveryPrefix string `json:"discovery-prefix"` // 自动服务发现路由前缀 Routes []Route `json:"routes"` // 静态路由 }
GatewayConfig 网关配置
type GatewayOption ¶
type GatewayOption func(*GatewayOptions)
GatewayOption 网关选项赋值
func WithGatewayPlugin ¶
func WithGatewayPlugin(plugins ...GatewayPlugin) GatewayOption
WithGatewayPlugin 注册网关扩展插件
func WithGatewaySelectorBuilder ¶
func WithGatewaySelectorBuilder(b SelectorBuilder) GatewayOption
WithGatewaySelectorBuilder 自定义通过 serviceName 构造 Selector 的方式
func WithGatewayTransport ¶
func WithGatewayTransport(transport http.RoundTripper) GatewayOption
WithGatewayTransport 网关 http transport
type GatewayOptions ¶
type GatewayOptions struct {
// contains filtered or unexported fields
}
GatewayOptions 网关选项定义
type GatewayPlugin ¶
type GatewayPlugin interface { // BeforeRoute 路由匹配前 BeforeRoute(context.Context, *http.Request) (*http.Request, error) // AfterRoute 路由匹配后 AfterRoute(context.Context, *http.Request) (*http.Request, error) // ModifyResponse 响应阶段处理 ModifyResponse(context.Context, *http.Response) error // ErrorHandler 统一异常处理 ErrorHandler(context.Context, http.ResponseWriter, *http.Request, error) }
GatewayPlugin 网关插件接口
type GetValueFromContext ¶
GetValueFromContext 从 context 中获取值
type HTTPClient ¶
type HTTPClient struct {
// contains filtered or unexported fields
}
HTTPClient 支持服务发现的 http client
func GetHTTPClient ¶
func GetHTTPClient(serviceName string) *HTTPClient
GetHTTPClient 返回服务对应的 client
func (*HTTPClient) Call ¶
func (c *HTTPClient) Call(ctx context.Context, req *HTTPRequest) (*HTTPResponse, error)
Call 发起 http 请求
type HTTPHandler ¶
type HTTPHandler interface { // Bind 绑定路由 Bind(router *HTTPServeMux) }
HTTPHandler http 请求处理器接口
type HTTPRequest ¶
type HTTPRequest struct { Path string Method string Header http.Header Params url.Values Form url.Values Body []byte }
HTTPRequest http 请求参数
type HTTPServeMux ¶
HTTPServeMux http 路由
func (*HTTPServeMux) ServeHTTP ¶
func (mux *HTTPServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*HTTPServeMux) Sub ¶
func (mux *HTTPServeMux) Sub(prefix string, fn func(sub *HTTPServeMux))
Sub 注册子路由
func (*HTTPServeMux) Use ¶
func (mux *HTTPServeMux) Use(middlewares ...HTTPMiddleware) *HTTPServeMux
Use 注册中间件
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer http server 实现
func NewHTTPServer ¶
func NewHTTPServer(opts ...ServerOption) *HTTPServer
NewHTTPServer 创建 http server opts 查看 ServerOptions
func (HTTPServer) GetServiceInfo ¶
func (s HTTPServer) GetServiceInfo() *ServiceInfo
func (*HTTPServer) Handler ¶
func (s *HTTPServer) Handler(handlers ...HTTPHandler) *HTTPServer
Handler 请求处理
func (*HTTPServer) Static ¶
func (s *HTTPServer) Static(pattern string, root string) *HTTPServer
Static 注册静态文件服务 默认不显示文件目录
func (*HTTPServer) StaticFS ¶ added in v1.0.4
func (s *HTTPServer) StaticFS(pattern string, fs fs.FS) *HTTPServer
StaticFS 注册静态文件服务,自定义文件系统 fs 可以使用 luchen.Dir() 创建
func (*HTTPServer) Use ¶
func (s *HTTPServer) Use(middlewares ...HTTPMiddleware) *HTTPServer
Use 中间件
type HTTPTransportServer ¶
type HTTPTransportServer = httptransport.Server
HTTPTransportServer go-kit http transport server
func NewHTTPTransportServer ¶ added in v1.0.3
func NewHTTPTransportServer( e endpoint.Endpoint, dec httptransport.DecodeRequestFunc, enc httptransport.EncodeResponseFunc, options ...httptransport.ServerOption, ) *HTTPTransportServer
NewHTTPTransportServer http handler 绑定 endpoint
type HostPattern ¶
type HostPattern struct { }
HostPattern host 匹配
type LogGRPCErrorHandler ¶
type LogGRPCErrorHandler struct { }
LogGRPCErrorHandler grpc 接口错误处理器
func NewLogGRPCErrorHandler ¶
func NewLogGRPCErrorHandler() *LogGRPCErrorHandler
NewLogGRPCErrorHandler 创建 LogGRPCErrorHandler
type Pattern ¶
type Pattern interface { // Name 匹配模式名称 Name() string // Match 匹配路由 Match(req *http.Request, route *httpRoute) bool }
Pattern 路由匹配模式
type PrefixPattern ¶
type PrefixPattern struct { }
PrefixPattern 前缀匹配
type RegisterHandler ¶
RegisterHandler 注册 grpc handler
type RoundRobinBalancer ¶
type RoundRobinBalancer[T any] struct { // contains filtered or unexported fields }
RoundRobinBalancer 基于 RoundRobin 算法的 Balancer 实现
func NewRoundRobinBalancer ¶
func NewRoundRobinBalancer[T any]() *RoundRobinBalancer[T]
NewRoundRobinBalancer 创建 NewRoundRobinBalancer 实例
func (*RoundRobinBalancer[T]) Pick ¶
func (b *RoundRobinBalancer[T]) Pick() (*T, error)
Pick 选择一个服务节点
func (*RoundRobinBalancer[T]) Refresh ¶
func (b *RoundRobinBalancer[T]) Refresh(services []*T)
Refresh 刷新节点列表
type Route ¶
type Route struct { Protocol string `json:"protocol"` // 协议,暂时只支持 http Pattern string `json:"pattern"` // 匹配模式:path, host Prefix string `json:"prefix"` // 匹配前缀 Host string `json:"host"` // 匹配 host ServiceName string `json:"service-name"` // 注册的服务名 Upstream string `json:"upstream"` // 上游服务 RewriteRegex string `json:"rewrite-regex"` // url 重写正则 Weight int `json:"weight"` // 权重 Ext map[string]string `json:"ext"` // 扩展信息 }
Route 服务路由
type SelectorBuilder ¶
SelectorBuilder Selector 构造函数
type Server ¶
type Server interface { Start() error Stop() error GetServiceInfo() *ServiceInfo }
Server server 接口定义
type ServerOption ¶
type ServerOption func(*ServerOptions)
ServerOption grpc server 选项赋值
func WithServerMetadata ¶
func WithServerMetadata(md map[string]any) ServerOption
WithServerMetadata server 注册信息 metadata,单体服务无需关注
func WithServiceName ¶
func WithServiceName(serviceName string) ServerOption
WithServiceName server 名称,在微服务中作为一组服务名称标识,单体服务则无需关注
type ServerOptions ¶
type ServerOptions struct {
// contains filtered or unexported fields
}
ServerOptions server 选项
type ServiceInfo ¶
type ServiceInfo struct { ID string `json:"id"` Name string `json:"name"` Protocol Protocol `json:"protocol"` Addr string `json:"addr"` Metadata map[string]any `json:"metadata,omitempty"` }
ServiceInfo 服务节点信息
type TraceGatewayPlugin ¶
type TraceGatewayPlugin struct {
*UnimplementedGatewayPlugin
}
TraceGatewayPlugin 链路跟踪插件
func (*TraceGatewayPlugin) BeforeRoute ¶
func (p *TraceGatewayPlugin) BeforeRoute(ctx context.Context, req *http.Request) (*http.Request, error)
BeforeRoute context 和 http header 注入 traceID
func (*TraceGatewayPlugin) ModifyResponse ¶
ModifyResponse 从上下文获取 tradeID 并写入 response header
type UnimplementedGatewayPlugin ¶
type UnimplementedGatewayPlugin struct { }
UnimplementedGatewayPlugin 其他自定义插件如果不想实现所有接口,可以跟UnimplementedGatewayPlugin组合,只实现指定的方法即可
func (*UnimplementedGatewayPlugin) AfterRoute ¶
func (p *UnimplementedGatewayPlugin) AfterRoute(ctx context.Context, req *http.Request) (*http.Request, error)
AfterRoute nothing to do
func (*UnimplementedGatewayPlugin) BeforeRoute ¶
func (p *UnimplementedGatewayPlugin) BeforeRoute(ctx context.Context, req *http.Request) (*http.Request, error)
BeforeRoute nothing to do
func (*UnimplementedGatewayPlugin) ErrorHandler ¶
func (p *UnimplementedGatewayPlugin) ErrorHandler(ctx context.Context, w http.ResponseWriter, req *http.Request, err error)
ErrorHandler nothing to do
func (*UnimplementedGatewayPlugin) GetContextErr ¶
func (p *UnimplementedGatewayPlugin) GetContextErr(ctx context.Context) error
GetContextErr 从上下文获取错误
func (*UnimplementedGatewayPlugin) ModifyResponse ¶
ModifyResponse nothing to do
Source Files ¶
- accesslog.go
- alias.go
- binding.go
- client.go
- client_grpc.go
- client_grpc_pool.go
- client_http.go
- config.go
- discovery.go
- endpoint.go
- errno.go
- fs.go
- gateway.go
- gateway_config.go
- gateway_context.go
- gateway_plugin.go
- kit.go
- lock.go
- request_context.go
- server.go
- server_grpc.go
- server_http.go
- server_http_mux.go
- tracer.go