一个功能丰富的 gRPC 服务端/客户端包装库,提供简化的 gRPC 服务开发体验。
- 🚀 简化的 gRPC 服务端/客户端创建
- 🔧 灵活的配置选项
- 🌐 内置服务发现支持(基于 etcd)
- 🔍 服务健康检查
- 🛡️ 拦截器支持
- 📝 自定义日志接口
- 🎯 反射服务支持
- ⚡ Keep-alive 连接管理
- 🔄 随机端口分配
go get github.com/nilorg/ngrpc/v2
package main
import (
"context"
"github.com/nilorg/ngrpc/v2"
)
func main() {
ctx := context.Background()
// 创建服务端
server := ngrpc.NewGrpcServer(ctx,
ngrpc.WithServerName("my-service"),
ngrpc.WithServerAddress(":8080"),
)
// 注册你的服务
// pb.RegisterYourServiceServer(server.GetSrv(), &yourServiceImpl{})
// 启动服务
server.Run()
}
package main
import (
"context"
"github.com/nilorg/ngrpc/v2"
)
func main() {
ctx := context.Background()
// 创建客户端
client := ngrpc.NewGrpcClient(ctx,
ngrpc.WithClientAddress("localhost:8080"),
)
defer client.Close(ctx)
// 使用连接创建服务客户端
// serviceClient := pb.NewYourServiceClient(client.GetConn())
}
server := ngrpc.NewGrpcServer(ctx,
ngrpc.WithServerName("my-service"), // 服务名称
ngrpc.WithServerAddress(":8080"), // 监听地址
ngrpc.WithServerRandomPort(true), // 随机端口
ngrpc.WithServerLogger(customLogger), // 自定义日志
ngrpc.WithServerRegistry(etcdRegistry), // 服务注册
)
client := ngrpc.NewGrpcClient(ctx,
ngrpc.WithClientAddress("localhost:8080"), // 服务地址
ngrpc.WithClientLogger(customLogger), // 自定义日志
ngrpc.WithClientDiscovery(etcdDiscovery), // 服务发现
)
ngrpc 支持基于 etcd 的服务发现:
import (
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/nilorg/ngrpc/v2/resolver"
)
// 创建 etcd 客户端
etcdClient, err := clientv3.New(clientv3.Config{
Endpoints: []string{"localhost:2379"},
})
if err != nil {
panic(err)
}
// 服务注册
registry := resolver.NewEtcdRegistry(etcdClient, "my-domain")
server := ngrpc.NewGrpcServer(ctx,
ngrpc.WithServerRegistry(registry),
)
// 服务发现
discovery := resolver.NewEtcdDiscovery(etcdClient, "my-domain")
client := ngrpc.NewGrpcClient(ctx,
ngrpc.WithClientDiscovery(discovery),
)
项目包含标准的 gRPC 健康检查服务:
import "github.com/nilorg/ngrpc/v2/health/grpc_health_v1"
// 在服务端注册健康检查服务
grpc_health_v1.RegisterHealthServer(server.GetSrv(), healthImpl)
支持自定义拦截器:
// 创建上下文处理器
contextHandler := func(ctx context.Context) context.Context {
// 添加自定义逻辑
return ctx
}
// 应用拦截器
server := ngrpc.NewGrpcServer(ctx,
ngrpc.WithServerUnaryInterceptor(ngrpc.UnaryServerInterceptor(contextHandler)),
ngrpc.WithServerStreamInterceptor(ngrpc.StreamServerInterceptor(contextHandler)),
)
实现 Logger
接口来使用自定义日志:
type Logger interface {
Debugf(ctx context.Context, format string, args ...interface{})
Debugln(ctx context.Context, args ...interface{})
Infof(ctx context.Context, format string, args ...interface{})
Infoln(ctx context.Context, args ...interface{})
Warnf(ctx context.Context, format string, args ...interface{})
Warnln(ctx context.Context, args ...interface{})
Errorf(ctx context.Context, format string, args ...interface{})
Errorln(ctx context.Context, args ...interface{})
Fatalf(ctx context.Context, format string, args ...interface{})
Fatalln(ctx context.Context, args ...interface{})
}
type MyLogger struct{}
func (l *MyLogger) Debugf(ctx context.Context, format string, args ...interface{}) {
// 实现 Debug 级别日志
}
func (l *MyLogger) Debugln(ctx context.Context, args ...interface{}) {
// 实现 Debug 级别日志(换行)
}
func (l *MyLogger) Infof(ctx context.Context, format string, args ...interface{}) {
// 实现 Info 级别日志
}
func (l *MyLogger) Infoln(ctx context.Context, args ...interface{}) {
// 实现 Info 级别日志(换行)
}
func (l *MyLogger) Warnf(ctx context.Context, format string, args ...interface{}) {
// 实现 Warn 级别日志
}
func (l *MyLogger) Warnln(ctx context.Context, args ...interface{}) {
// 实现 Warn 级别日志(换行)
}
func (l *MyLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
// 实现 Error 级别日志
}
func (l *MyLogger) Errorln(ctx context.Context, args ...interface{}) {
// 实现 Error 级别日志(换行)
}
func (l *MyLogger) Fatalf(ctx context.Context, format string, args ...interface{}) {
// 实现 Fatal 级别日志
}
func (l *MyLogger) Fatalln(ctx context.Context, args ...interface{}) {
// 实现 Fatal 级别日志(换行)
}
// 在服务端使用自定义日志
server := ngrpc.NewGrpcServer(ctx,
ngrpc.WithServerLogger(&MyLogger{}),
)
// 在客户端使用自定义日志
client := ngrpc.NewGrpcClient(ctx,
ngrpc.WithClientLogger(&MyLogger{}),
)
本项目采用 Apache License 2.0 许可证 - 查看 LICENSE 文件了解详情。
欢迎贡献代码!请先查看贡献指南。
- nilorg - 更多开源项目