Skip to content

nilorg/ngrpc

Repository files navigation

ngrpc

Go Report Card GoDoc License

一个功能丰富的 gRPC 服务端/客户端包装库,提供简化的 gRPC 服务开发体验。

功能特性

  • 🚀 简化的 gRPC 服务端/客户端创建
  • 🔧 灵活的配置选项
  • 🌐 内置服务发现支持(基于 etcd)
  • 🔍 服务健康检查
  • 🛡️ 拦截器支持
  • 📝 自定义日志接口
  • 🎯 反射服务支持
  • Keep-alive 连接管理
  • 🔄 随机端口分配

安装

go get github.com/nilorg/ngrpc/v2

快速开始

创建 gRPC 服务端

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()
}

创建 gRPC 客户端

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 文件了解详情。

贡献

欢迎贡献代码!请先查看贡献指南。

相关项目

About

grpc server/client 包装一些grpc功能。

Resources

License

Stars

Watchers

Forks

Packages

No packages published