grpcserver

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Server)

func WithAutoServiceDiscovery

func WithAutoServiceDiscovery(registries []discover.Registry, sdOpts ...ServiceDiscoveryOption) Option

WithAutoServiceDiscovery 启用自动服务发现,为每个protobuf服务单独注册。 可通过 sdOpts 传入 WithInternalServices() 等服务发现选项。

func WithEnvironment

func WithEnvironment(env string) Option

WithEnvironment 设置环境信息

func WithGracefulStopTimeout

func WithGracefulStopTimeout(d time.Duration) Option

WithGracefulStopTimeout 设置 GracefulStop 的最长等待时间,超时后强制 Stop。 默认 30s。

func WithGrpcServerOptions

func WithGrpcServerOptions(opts ...grpc.ServerOption) Option

func WithGrpcServerStreamInterceptor

func WithGrpcServerStreamInterceptor(interceptors ...grpc.StreamServerInterceptor) Option

func WithGrpcServerUnaryInterceptor

func WithGrpcServerUnaryInterceptor(interceptors ...grpc.UnaryServerInterceptor) Option

func WithMetadata

func WithMetadata(md map[string]string) Option

func WithPriority

func WithPriority(priority int) Option

WithPriority 设置服务优先级

func WithRegionInfo

func WithRegionInfo(region, zone, campus string) Option

WithRegionInfo 设置地域信息,兼容Polaris

func WithServiceName

func WithServiceName(name string) Option

func WithTLS

func WithTLS(certFile, keyFile string) Option

WithTLS 通过证书文件启用 TLS。certFile 和 keyFile 为 PEM 格式文件路径。

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) Option

WithTLSConfig 通过自定义 tls.Config 启用 TLS,适合需要 mTLS 或自定义 CA 的场景。

func WithVersion

func WithVersion(version string) Option

WithVersion 将服务版本写入 metadata,注册到注册中心后可见,灰度发布时可用于流量路由。

func WithWeight

func WithWeight(weight int) Option

WithWeight 设置服务权重

type ProtoServiceInfo

type ProtoServiceInfo struct {
	ServiceName string            `json:"service_name"`
	Methods     []string          `json:"methods"`
	Metadata    map[string]string `json:"metadata"`
	ServerAddr  string            `json:"server_addr"`
}

ProtoServiceInfo protobuf服务信息

type ProtoServiceWrapper

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

ProtoServiceWrapper 实现discover.Service接口

func (*ProtoServiceWrapper) Addr

func (w *ProtoServiceWrapper) Addr() string

func (*ProtoServiceWrapper) ID

func (w *ProtoServiceWrapper) ID() string

func (*ProtoServiceWrapper) Kind

func (w *ProtoServiceWrapper) Kind() string

func (*ProtoServiceWrapper) Metadata

func (w *ProtoServiceWrapper) Metadata() map[string]string

func (*ProtoServiceWrapper) Name

func (w *ProtoServiceWrapper) Name() string

type Server

type Server struct {
	Server *grpc.Server
	// contains filtered or unexported fields
}

Server ..

func New

func New(addr string, handler func(*grpc.Server), opts ...Option) *Server

New create a grpc service with the name

func (*Server) Addr

func (s *Server) Addr() string

func (*Server) ID

func (s *Server) ID() string

func (*Server) Kind

func (s *Server) Kind() string

func (*Server) Metadata

func (s *Server) Metadata() map[string]string

func (*Server) Name

func (s *Server) Name() string

func (*Server) Ready

func (s *Server) Ready() <-chan struct{}

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start ..

func (*Server) String

func (s *Server) String() string

String ..

type ServiceDiscovery

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

ServiceDiscovery 从gRPC Server中读取已注册的服务

func NewServiceDiscovery

func NewServiceDiscovery(server *grpc.Server, registries []discover.Registry, opts ...ServiceDiscoveryOption) *ServiceDiscovery

NewServiceDiscovery 创建服务发现器

func (*ServiceDiscovery) DiscoverServices

func (sd *ServiceDiscovery) DiscoverServices(serverAddr string, baseMetadata map[string]string) error

DiscoverServices 发现gRPC Server中已注册的服务

func (*ServiceDiscovery) RegisterAllServices

func (sd *ServiceDiscovery) RegisterAllServices(ctx context.Context) (wait func(), err error)

RegisterAllServices 注册所有发现的服务,返回一个 wait 函数, 调用方可在 ctx 取消后调用 wait() 确保所有注册 goroutine 已退出。

type ServiceDiscoveryOption

type ServiceDiscoveryOption func(*ServiceDiscovery)

ServiceDiscoveryOption 服务发现选项

func WithInternalServices

func WithInternalServices() ServiceDiscoveryOption

WithInternalServices 将 health check、reflection 等内部服务也注册到服务注册中心

Jump to

Keyboard shortcuts

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