通过同一连接访问多个gRPC服务

时间:2018-10-03 19:16:57

标签: go grpc

我正在运行一个grpc服务器,它在localhost:6000上进行侦听,公开了2个grpc服务:RegisterSubscriberServiceServerRegisterDropperServiceServer。由于localhost:6000都可以访问这两项服务,因此我只想从存根拨打该地址。

服务器如下:

func main() {
    grpcServer := grpc.NewServer()
    pb.RegisterSubscriberServiceServer(grpcServer, &subscriberServer{})
    pb.RegisterDropperServiceServer(grpcServer, &dropperServer{})

    l, err := net.Listen("tcp", ":6000")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }

    log.Println("Listening on tcp://localhost:6000")
    grpcServer.Serve(l)
}

为什么每个grpc服务都需要拨不同的套接字?

type DropperRPC struct {
    conn      *grpc.ClientConn
    client pb.DropperServiceClient
    chunkSize int
}

type SubscriberRPC struct {
    conn      *grpc.ClientConn
    client pb.SubscriberServiceClient
    chunkSize int
}

func NewSubscriber() (c SubscriberRPC, err error) {
    c.conn, err = grpc.Dial("localhost:6000", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    c.client = pb.NewSubscriberServiceClient(c.conn)
    return
}

func NewDropper() (c DropperRPC, err error) {
    c.conn, err = grpc.Dial("localhost:6000", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    c.client = pb.NewDropperServiceClient(c.conn)
    return
}

由于代码基本上是重复的,以适应每种服务,所以我不能只使用Interface来减少代码吗?

func main() {
    c1, err := NewSubscriber()
    if err != nil {
        log.Fatal(err)
    }

    c2, err := NewDropper()
    if err != nil {
        log.Fatal(err)
    }

    cc1 := &c1
    cc2 := &c2
}

然后使用此Interface为每个服务实现客户端grpc功能,而不是为每个服务创建新的struct。我已经找到cmux,但是必须有一种无需使用外部库即可完成此操作的方法。

1 个答案:

答案 0 :(得分:3)

  

为什么每个grpc服务都需要拨一个不同的插座?

你不知道。您可以创建一个grpc.ClientConn并将其传递给多个pb.New*Client()函数,它们将共享相同的连接。

func main() {
    cc, err := grpc.Dial("localhost:6000", grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    c1 := pb.NewSubscriberServiceClient(cc)
    c2 := pb.NewDropperServiceClient(cc)
}
  

然后使用此接口为每个服务实现客户端grpc函数,而不是为每个服务创建新的结构

pb.go文件中生成的代码完成了执行RPC所需的一切。除非您有特殊的逻辑要在每次执行呼叫时自动发生,否则您不必实现任何客户端功能。

如果这两个服务具有唯一的方法名称,您可以将它们放入相同的结构中,这样就不必为了一个次要便利而单独使用它们:

type SubscriberDropper struct {
    pb.SubscriberServiceClient
    pb.DropperServiceClient
}

func main() {
    // ... as above ...
    sd := &SubscriberDropper{c1, c2}
}