Documentation
¶
Index ¶
- Constants
- Variables
- func GetGrpcClient[C any](ctx context.Context, factory *ConnFactory, service ServiceName, ...) (C, error)
- func GrpcServiceName(name ServiceName) string
- func HTTPServiceName(name ServiceName) string
- func NewGrpcConn(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
- func NewGrpcConnAndTest(ctx context.Context, target string, timeout time.Duration, ...) (*grpc.ClientConn, error)
- func NewGrpcConnWithSS(proxyAddr, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
- func ParseEndpoints(httpConf, grpcConf ServerConf) ([]*url.URL, error)
- func ParseStrEndpoints(httpConf, grpcConf ServerConf) ([]string, error)
- func ProtoMarshal(m proto.Message) ([]byte, error)
- func ProtoMarshalEnumAsString(m proto.Message) ([]byte, error)
- func ProtoUnmarshal(data []byte, v interface{}) error
- func ServerLogger(logger log.Logger) middleware.Middleware
- func TruncateBytes(s string, maxLen int) string
- type ConnFactory
- type LoggerFunc
- type NewClientFunc
- type ServerConf
- type ServiceID
- type ServiceName
Constants ¶
const MaxShowBodyLen = 1024
Variables ¶
var ( // MarshalOptions is a configurable JSON format marshaller MarshalOptions = protojson.MarshalOptions{ UseProtoNames: true, UseEnumNumbers: true, } // MarshalOptionsEnumAsString 针对 proto 的枚举使用字符串(name) MarshalOptionsEnumAsString = protojson.MarshalOptions{ UseProtoNames: true, UseEnumNumbers: false, } // UnmarshalOptions is a configurable JSON format parser UnmarshalOptions = protojson.UnmarshalOptions{ DiscardUnknown: true, } Codec = codec{} // 优先使用 proto json EnumStringCodec = enumStringCodec{} // proto enum 使用字符串 JSONCodec = jsonCodec{} // 纯 json codec )
Functions ¶
func GetGrpcClient ¶
func GetGrpcClient[C any](ctx context.Context, factory *ConnFactory, service ServiceName, fn NewClientFunc[C]) (C, error)
func GrpcServiceName ¶
func GrpcServiceName(name ServiceName) string
func HTTPServiceName ¶
func HTTPServiceName(name ServiceName) string
func NewGrpcConn ¶
func NewGrpcConn(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
func NewGrpcConnAndTest ¶
func NewGrpcConnAndTest(ctx context.Context, target string, timeout time.Duration, opts ...grpc.DialOption) (*grpc.ClientConn, error)
func NewGrpcConnWithSS ¶
func NewGrpcConnWithSS(proxyAddr, target string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
func ParseEndpoints ¶
func ParseEndpoints(httpConf, grpcConf ServerConf) ([]*url.URL, error)
ParseEndpoints 生成用于注册中心的 endpoints(URL 形式),顺序为 http、grpc(如提供)。 规则(按优先级选 host;端口一律取自 GetAddr()): 1) ADVERTISE_HOST 环境变量 2) GetPublicEndpoint(若包含 scheme 则使用该 scheme,否则用默认 scheme) 3) GetAddr 中的主机(排除 0.0.0.0/::/回环) 4) 本机非回环 IPv4 5) 127.0.0.1 兜底
func ParseStrEndpoints ¶
func ParseStrEndpoints(httpConf, grpcConf ServerConf) ([]string, error)
ParseStrEndpoints 生成用于注册中心的 endpoints(string 形式),顺序为 http、grpc(如提供)。 规则(按优先级选 host;端口一律取自 GetAddr()): 1) ADVERTISE_HOST 环境变量 2) GetPublicEndpoint(若包含 scheme 则使用该 scheme,否则用默认 scheme) 3) GetAddr 中的主机(排除 0.0.0.0/::/回环) 4) 本机非回环 IPv4 5) 127.0.0.1 兜底
func ProtoMarshal ¶
ProtoMarshal 使用默认的 MarshalOptions (枚举为数字) 编码 proto.Message
func ProtoMarshalEnumAsString ¶
ProtoMarshalEnumAsString 使用 MarshalOptionsEnumAsString (枚举为字符串) 编码 proto.Message
func ProtoUnmarshal ¶
ProtoUnmarshal 使用 UnmarshalOptions 解析到目标 v,支持 *T / **T 形式的 proto.Message。
func ServerLogger ¶
func ServerLogger(logger log.Logger) middleware.Middleware
func TruncateBytes ¶
Types ¶
type ConnFactory ¶
type ConnFactory struct {
// contains filtered or unexported fields
}
func NewConnFactory ¶
NewConnFactory 使用默认的 WRR 负载均衡
func (*ConnFactory) Conn ¶
func (f *ConnFactory) Conn( ctx context.Context, service string, ) (*grpc.ClientConn, error)
Conn 返回(或新建)到 service 的 *grpc.ClientConn
type LoggerFunc ¶
type LoggerFunc func(string, ...interface{})
type NewClientFunc ¶
type NewClientFunc[C any] func(grpc.ClientConnInterface) C
type ServerConf ¶
type ServiceName ¶
type ServiceName = string
const ( ServiceNameAppLayout ServiceName = "app_layout" // 项目模板 ServiceNameProbeApi ServiceName = "probe-api" // 可用性探测服务 ServiceNameProbeCenter ServiceName = "probe_center" // 控制面服务 ServiceNameProbeRegin ServiceName = "probe_region" // 控制面服务 )