client

package
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GrpcClient

type GrpcClient[T any] struct {
	Stub T // 具体业务客户端(由 ctor 构造)
	// contains filtered or unexported fields
}

func NewGrpcClient

func NewGrpcClient[T any](ctor func(cc *grpc.ClientConn) T, opts Options) (*GrpcClient[T], error)

NewGrpcClient 通过 ctor 构造具体的 pb 客户端(例如 pb.NewDemoClient)

func (*GrpcClient[T]) Close

func (c *GrpcClient[T]) Close() error

func (*GrpcClient[T]) Conn

func (c *GrpcClient[T]) Conn() *grpc.ClientConn

func (*GrpcClient[T]) Do

func (c *GrpcClient[T]) Do(ctx context.Context, defaultTimeout time.Duration, fn func(ctx context.Context, stub T) error) error

Do 包一层默认超时,统一上下文

func (*GrpcClient[T]) WaitReady

func (c *GrpcClient[T]) WaitReady(ctx context.Context, svc string) error

WaitReady 使用 gRPC Health Check 等待服务就绪

type Options

type Options struct {
	// 基础拨号
	Target    string   // 形如 "dns:///host:port" 或 "ipv4:port";本地可直接 "127.0.0.1:9090"
	Endpoints []string // 可选:["10.0.0.1:9090","10.0.0.2:9090"],内部自动 round_robin
	Authority string   // 可选:HTTP/2 :authority

	// 安全
	Insecure bool        // true 则明文;否则走 TLS
	TLSCfg   *tls.Config // 可选:自定义 TLS(可含 ServerName、RootCAs、mTLS 等)

	// 调用默认值
	DefaultTimeout time.Duration // 默认每次调用超时(建议 2~5s)

	// 拦截器
	UnaryInts  []grpc.UnaryClientInterceptor
	StreamInts []grpc.StreamClientInterceptor

	// 低级定制
	ExtraDialOptions []grpc.DialOption
	// gRPC ServiceConfig JSON:可用于开启官方重试/负载均衡策略等
	// 如:`{"loadBalancingPolicy":"round_robin","methodConfig":[{...retryPolicy...}]}`
	ServiceConfigJSON string
}

Options 配置

Jump to

Keyboard shortcuts

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