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条回答 默认 最新

相关推荐 更多相似问题