dongmeng9048 2019-04-22 21:25
浏览 107
已采纳

如何在Go中创建通用GRPC服务器启动功能

I'm trying to abstract the start of a GRPC server

The original main function is the following:

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }

    s := grpc.NewServer()
    pb.RegisterCollectionServer(s, &server.Server{})
    // Register reflection service on gRPC server.
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

My goal is to have something like this:

func startService(sr func(*grpc.Server, interface{}), srv interface{}) error {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        return err
    }
    s := grpc.NewServer()
    sr(s, srv)
    reflection.Register(s)
    return s.Serve(lis)
}

func main() {
    err := startService(pb.RegisterCollectionServer, &server.Server{})

    if err != nil {
        log.Fatalf("failed to start Service: %v", err)
    }
}

But this gives me the following error:

cannot use collection_api.RegisterCollectionServer (type func(*grpc.Server, collection_api.CollectionServer)) as type func(*grpc.Server, interface {}) in argument to startServicego

It seems that collection_api.CollectionServer is not a valid interface{} type.

Any idea how to make this work?

  • 写回答

1条回答 默认 最新

  • doraemon0769 2019-04-23 11:00
    关注

    In your setup the function signature of collector_api.RegisterCollectionServer must match exactly func(*grpc.Server, interface{}), there is no "is-kind-of" in Go as there is in other languages.

    If you want to keep the startService function independent of the collection_api types, you can use a anonymous function in main(). If you know that you what ever you are registering is always dependent on the implementation of the anonymous function (in this case &server.Server{}), then you can leave this detail out of the function signature of startService and put it into the anonymous function:

    func startService(sr func(*grpc.Server)) error {
        lis, err := net.Listen("tcp", port)
        if err != nil {
            return err
        }
        s := grpc.NewServer()
        sr(s)
        reflection.Register(s)
        return s.Serve(lis)
    }
    
    func main() {
        err := startService(func(grpcServer *grpc.Server) {
                pb.RegisterCollectionServer(grpcServer, &server.Server{})
            }, &server.Server{})
    
        if err != nil {
            log.Fatalf("failed to start Service: %v", err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵