Documentation
¶
Overview ¶
Package grpc 提供通用的 gRPC 服务器基础设施
本包为 apiserver 提供 gRPC 服务器的通用能力,包括:
- Server: gRPC 服务器封装,支持 TLS、健康检查、反射服务
- Config: 服务器配置,包括绑定地址、消息大小限制、连接管理等
- 拦截器: 日志、恢复、请求 ID 等通用拦截器
使用示例:
config := grpc.NewConfig()
config.BindPort = 9090
config.EnableHealthCheck = true
server, err := config.Complete().New()
if err != nil {
log.Fatal(err)
}
// 注册服务
pb.RegisterMyServiceServer(server.Server, myService)
// 启动服务器
server.Run()
对于 mTLS、ACL 等高级安全功能,可使用 pkg/grpc/mtls 和 pkg/grpc/interceptors 中的组件来扩展。
Package grpc 提供 IAM 特定的 gRPC 拦截器配置 ¶
本包基于 pkg/grpc/interceptors 提供的通用拦截器, 集成 component-base 的日志和追踪功能。
Index ¶
- func LoggingInterceptor() grpc.UnaryServerInterceptor
- func RecoveryInterceptor() grpc.UnaryServerInterceptor
- func RequestIDInterceptor() grpc.UnaryServerInterceptor
- type ACLConfig
- type AuditConfig
- type AuthConfig
- type CompletedConfig
- type Config
- type MTLSConfig
- type Server
- func (s *Server) Address() string
- func (s *Server) Close()
- func (s *Server) Config() *Config
- func (s *Server) IsMTLSEnabled() bool
- func (s *Server) MarkAllServicesServing()
- func (s *Server) RegisterService(service Service)
- func (s *Server) Run() error
- func (s *Server) RunWithContext(ctx context.Context) error
- func (s *Server) SetServingStatus(service string, status healthpb.HealthCheckResponse_ServingStatus)
- type Service
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoggingInterceptor ¶
func LoggingInterceptor() grpc.UnaryServerInterceptor
LoggingInterceptor 返回集成 component-base 日志的拦截器
func RecoveryInterceptor ¶
func RecoveryInterceptor() grpc.UnaryServerInterceptor
RecoveryInterceptor 返回集成 component-base 日志的恢复拦截器
func RequestIDInterceptor ¶
func RequestIDInterceptor() grpc.UnaryServerInterceptor
RequestIDInterceptor 返回集成 component-base 的请求ID拦截器
Types ¶
type ACLConfig ¶
type ACLConfig struct {
Enabled bool // 是否启用 ACL
ConfigFile string // ACL 配置文件路径
DefaultPolicy string // 默认策略:deny 或 allow
}
ACLConfig ACL 权限控制配置
type AuthConfig ¶
type AuthConfig struct {
Enabled bool // 是否启用应用层认证
EnableBearer bool // 启用 Bearer Token 认证
EnableHMAC bool // 启用 HMAC 签名认证
EnableAPIKey bool // 启用 API Key 认证
HMACTimestampValidity time.Duration // HMAC 时间戳有效期
RequireIdentityMatch bool // 是否要求 mTLS 身份与凭证身份一致
}
AuthConfig 应用层认证配置
type CompletedConfig ¶
type CompletedConfig struct {
*Config
}
CompletedConfig GRPC 服务器的完成配置
func (CompletedConfig) New ¶
func (c CompletedConfig) New() (*Server, error)
New 从给定的配置创建一个新的 GRPC 服务器实例
type Config ¶
type Config struct {
BindAddress string
BindPort int
HealthzPort int
MaxMsgSize int
MaxConnectionAge time.Duration
MaxConnectionAgeGrace time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
// TLS 配置(单向 TLS)
TLSCertFile string
TLSKeyFile string
// mTLS 配置(双向认证)
MTLS MTLSConfig
// 应用层认证配置
Auth AuthConfig
// ACL 权限控制配置
ACL ACLConfig
// 审计日志配置
Audit AuditConfig
EnableReflection bool
EnableHealthCheck bool
Insecure bool // 是否使用不安全连接
}
Config GRPC 服务器配置
func (*Config) Complete ¶
func (c *Config) Complete() CompletedConfig
Complete 填充任何未设置的字段,这些字段是必需的,并且可以从其他字段派生出来
type MTLSConfig ¶
type MTLSConfig struct {
Enabled bool // 是否启用 mTLS
CAFile string // CA 证书文件(用于验证客户端证书)
CADir string // CA 证书目录(支持多级 CA)
RequireClientCert bool // 是否强制要求客户端证书
AllowedCNs []string // 允许的客户端证书 CN 列表
AllowedOUs []string // 允许的客户端证书 OU 列表
AllowedSANs []string // 允许的客户端证书 DNS SAN 列表
MinTLSVersion string // 最低 TLS 版本 (1.0, 1.1, 1.2, 1.3),默认 1.2
EnableAutoReload bool // 启用证书自动重载
ReloadInterval time.Duration // 证书重载检查间隔
}
MTLSConfig mTLS 双向认证配置
type Server ¶
Server GRPC 服务器结构体
func (*Server) MarkAllServicesServing ¶
func (s *Server) MarkAllServicesServing()
MarkAllServicesServing 将所有已注册的服务标记为 SERVING 状态 应在所有服务注册完成后调用
func (*Server) RegisterService ¶
RegisterService 注册 GRPC 服务
func (*Server) RunWithContext ¶
RunWithContext 使用上下文启动 GRPC 服务器
func (*Server) SetServingStatus ¶
func (s *Server) SetServingStatus(service string, status healthpb.HealthCheckResponse_ServingStatus)
SetServingStatus 设置指定服务的健康状态 service: 服务名称(如 "iam.authn.v1.AuthService"),空字符串表示整体服务 status: 健康状态(healthpb.HealthCheckResponse_SERVING, NOT_SERVING, UNKNOWN, SERVICE_UNKNOWN)