kratosx

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const MaxShowBodyLen = 1024

Variables

View Source
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

func ProtoMarshal(m proto.Message) ([]byte, error)

ProtoMarshal 使用默认的 MarshalOptions (枚举为数字) 编码 proto.Message

func ProtoMarshalEnumAsString

func ProtoMarshalEnumAsString(m proto.Message) ([]byte, error)

ProtoMarshalEnumAsString 使用 MarshalOptionsEnumAsString (枚举为字符串) 编码 proto.Message

func ProtoUnmarshal

func ProtoUnmarshal(data []byte, v interface{}) error

ProtoUnmarshal 使用 UnmarshalOptions 解析到目标 v,支持 *T / **T 形式的 proto.Message。

func ServerLogger

func ServerLogger(logger log.Logger) middleware.Middleware

func TruncateBytes

func TruncateBytes(s string, maxLen int) string

Types

type ConnFactory

type ConnFactory struct {
	// contains filtered or unexported fields
}

func NewConnFactory

func NewConnFactory(
	ctx context.Context,
	d *nacosx.Registry,
) (*ConnFactory, func(), error)

NewConnFactory 使用默认的 WRR 负载均衡

func (*ConnFactory) Close

func (f *ConnFactory) Close()

Close 在进程退出时调用

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 ServerConf interface {
	GetAddr() string // 监听地址,如 ":11002" / "0.0.0.0:11002" / "10.0.0.1:10002"

	GetPublicEndpoint() string // 手动公开地址,如 "grpc://172.28.1.1:7411" 或 "172.28.1.1:7411"
}

type ServiceID

type ServiceID string

type ServiceName

type ServiceName = string
const (
	ServiceNameAppLayout ServiceName = "app_layout" // 项目模板

	ServiceNameProbeApi ServiceName = "probe-api" // 可用性探测服务

	ServiceNameProbeCenter ServiceName = "probe_center" // 控制面服务

	ServiceNameProbeRegin ServiceName = "probe_region" // 控制面服务
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL