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 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥15 我的显卡支持CUDA最高版本是12.3,这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8,需要下载旧版本的VS2022吗
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励