Documentation
¶
Index ¶
- Constants
- Variables
- func AddBeforeStopHook(handler func())
- func DoStopHook()
- func LogMiddleware(next endpoint.Endpoint) endpoint.Endpoint
- func Marshaller(ctx context.Context) marshal.Marshaller
- func MustLoadConfig[T any](files ...string) T
- func MustNewEtcdV3Client(address []string) etcdv3.Client
- func NewDefaultEtcdV3Client() etcdv3.Client
- func RecoverHTTPMiddleware(next http.Handler) http.Handler
- func Start(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 UseGlobalGRPCMiddleware(m ...Middleware)
- func UseGlobalHTTPMiddleware(m ...Middleware)
- func WithTraceID(ctx context.Context, traceID string) context.Context
- func WriteError(ctx context.Context, w http.ResponseWriter, err error)
- type AccessLog
- type AccessLogOpt
- type Balancer
- type Endpoint
- type EndpointDefine
- type Errno
- func (e *Errno) Error() string
- func (e *Errno) Format(s fmt.State, verb rune)
- func (e *Errno) GetDetail() string
- func (e *Errno) IsClientError() bool
- func (e *Errno) IsServerError() bool
- func (e *Errno) Unwrap() error
- func (e *Errno) WithCause(err error) *Errno
- func (e *Errno) WithDetail(detail string) *Errno
- func (e *Errno) WithMsg(msg string) *Errno
- type EtcdV3Registrar
- type EtcdV3Selector
- type GRPCClient
- type GRPCServer
- type GRPCTransportServer
- type GetValueFromContext
- type HTTPServer
- type HTTPTransportServer
- type Header
- type LogGRPCErrorHandler
- type Middleware
- type Protocol
- type RegisterHandler
- type RoundRobinBalancer
- type Selector
- type SelectorBuilder
- type Server
- type ServerOption
- type ServerOptions
- type ServiceInfo
Constants ¶
const (
HeaderRspMeta = "X-Rsp-Meta"
)
const (
// TraceIDHeader traceID header key
TraceIDHeader = "X-Trace-ID"
)
Variables ¶
var ( // ErrSystem 系统错误 ErrSystem = NewErrnoWithHttpCode(http.StatusInternalServerError, "") // ErrNotImplemented 未实现 ErrNotImplemented = NewErrnoWithHttpCode(http.StatusNotImplemented, "") // ErrBadGateway 网关错误 ErrBadGateway = NewErrnoWithHttpCode(http.StatusBadGateway, "") ErrServiceUnavailable = NewErrnoWithHttpCode(http.StatusServiceUnavailable, "") // ErrGatewayTimeout 网关超时 ErrGatewayTimeout = NewErrnoWithHttpCode(http.StatusGatewayTimeout, "") // ErrBadRequest 参数错误 ErrBadRequest = NewErrnoWithHttpCode(http.StatusBadRequest, "") ErrUnauthorized = NewErrnoWithHttpCode(http.StatusUnauthorized, "") // ErrForbidden 没有权限访问 ErrForbidden = NewErrnoWithHttpCode(http.StatusForbidden, "") // ErrNotFound 资源不存在 ErrNotFound = NewErrnoWithHttpCode(http.StatusNotFound, "") )
var ( // ErrNoServer 没有可用的服务节点 ErrNoServer = errors.New("no server available") )
var GlobalGRPCMiddlewares []Middleware
GlobalGRPCMiddlewares 全局 GRPC 中间件
var GlobalHTTPMiddlewares []Middleware
GlobalHTTPMiddlewares 全局 HTTP 中间件
var (
// TraceIDCtxKey traceID context key
TraceIDCtxKey = traceIDKey{}
)
Functions ¶
func LogMiddleware ¶ added in v1.1.0
LogMiddleware 错误日志堆栈打印,放在第一个执行
func Marshaller ¶ added in v1.1.0
func Marshaller(ctx context.Context) marshal.Marshaller
Marshaller 返回当前请求的 marshaller
func MustNewEtcdV3Client ¶
MustNewEtcdV3Client 创建 etcdv3.Client
func NewDefaultEtcdV3Client ¶
NewDefaultEtcdV3Client 创建默认 etcdv3.Client etcd 地址通过 GetDefaultEtcdAddress 方法获得
func RecoverHTTPMiddleware ¶ added in v1.1.0
RecoverHTTPMiddleware 恢复 panic
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,取不到则创建
func UseGlobalGRPCMiddleware ¶ added in v1.1.0
func UseGlobalGRPCMiddleware(m ...Middleware)
UseGlobalGRPCMiddleware 注册全局 GRPC 中间件 中间件的执行顺序与注册顺序相同,先注册的中间件先执行
func UseGlobalHTTPMiddleware ¶ added in v1.1.0
func UseGlobalHTTPMiddleware(m ...Middleware)
UseGlobalHTTPMiddleware 注册全局 HTTP 中间件 中间件的执行顺序与注册顺序相同,先注册的中间件先执行
func WithTraceID ¶
WithTraceID context 注入 traceID
func WriteError ¶ added in v1.1.0
func WriteError(ctx context.Context, w http.ResponseWriter, err error)
WriteError write error
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 Endpoint ¶
Endpoint alias for endpoint.Endpoint
func EndpointChain ¶ added in v1.1.0
func EndpointChain(e Endpoint, middlewares ...Middleware) Endpoint
EndpointChain Endpoint 中间件包装
type EndpointDefine ¶ added in v1.1.0
type EndpointDefine struct { Name string // 端点名称 Path string // http 请求路径 ReqType reflect.Type // 请求类型 RspType reflect.Type // 响应类型 Endpoint Endpoint // 端点处理器 Middlewares []Middleware // 中间件 }
EndpointDefine 端点定义信息
type Errno ¶
type Errno struct { HttpCode int `json:"http_code"` // http 状态码 Code int `json:"code"` // 错误码 Msg string `json:"msg"` // 错误信息 Detail string `json:"detail"` // 详细错误信息 Cause error `json:"-"` // 原始错误,不序列化 }
Errno 错误编码定义 Code: 0 - 500,使用 http 状态码规范,大于 1000 的错误码,表示自定义错误码
func NewErrnoWithHttpCode ¶ added in v1.1.0
NewErrnoWithHttpCode 根据 http 状态码创建错误编码
func (*Errno) IsClientError ¶ added in v1.1.0
IsClientError 是否是客户端错误
func (*Errno) IsServerError ¶ added in v1.1.0
IsServerError 是否是服务端系统错误
func (*Errno) WithDetail ¶ added in v1.1.0
WithDetail 设置详细错误信息
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(desc *grpc.ServiceDesc, impl any)
RegisterService 注册 grpc 接口实现
type GRPCTransportServer ¶
type GRPCTransportServer = grpctransport.Server
GRPCTransportServer grpc transport server
func NewGRPCTransportServer ¶ added in v1.0.3
func NewGRPCTransportServer( def *EndpointDefine, options ...grpctransport.ServerOption, ) *GRPCTransportServer
NewGRPCTransportServer grpc handler 绑定 endpoint
type GetValueFromContext ¶
GetValueFromContext 从 context 中获取值
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) Handle ¶ added in v1.1.0
func (s *HTTPServer) Handle(def *EndpointDefine)
Handle 注册 http 路由
type HTTPTransportServer ¶
type HTTPTransportServer = httptransport.Server
HTTPTransportServer go-kit http transport server
func NewHTTPTransportServer ¶ added in v1.0.3
func NewHTTPTransportServer( def *EndpointDefine, options ...httptransport.ServerOption, ) *HTTPTransportServer
NewHTTPTransportServer http handler 绑定 endpoint
type Header ¶ added in v1.1.0
type LogGRPCErrorHandler ¶
type LogGRPCErrorHandler struct { }
LogGRPCErrorHandler grpc 接口错误处理器
func NewLogGRPCErrorHandler ¶
func NewLogGRPCErrorHandler() *LogGRPCErrorHandler
NewLogGRPCErrorHandler 创建 LogGRPCErrorHandler
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 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 选项