douduxia1551 2018-10-03 19:16
浏览 901
已采纳

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

I am running a grpc server listening on localhost:6000, exposing 2 grpc services: RegisterSubscriberServiceServer and RegisterDropperServiceServer. Since both of these services are reachable from localhost:6000, I'd like to only dial this address from the stub.

The server looks like this:

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

Why do I need to dial a different socket for each grpc service?

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
}

And since the code is basically duplicated to accommodate each service, can't I just use an Interface to reduce the code?

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

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

    cc1 := &c1
    cc2 := &c2
}

And then use this Interface to implement the client side grpc functions for each service, instead of creating a new struct for each service. I have found cmux , but there must be a way to do this without using external libraries.

  • 写回答

1条回答 默认 最新

  • dqsw7529 2018-10-03 21:23
    关注

    Why do I need to dial a different socket for each grpc service?

    You don't. You can create one grpc.ClientConn and pass it to multiple pb.New*Client() functions, and they will share the same connection(s).

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

    And then use this Interface to implement the client side grpc functions for each service, instead of creating a new struct for each service

    The generated code in the pb.go file does everything you need to perform RPCs. You shouldn't have to implement anything client-side unless you have special logic you want to happen automatically every time you perform a call.

    If the two services have unique method names, you could put them into the same struct so you don't have to use them separately, as a minor convenience:

    type SubscriberDropper struct {
        pb.SubscriberServiceClient
        pb.DropperServiceClient
    }
    
    func main() {
        // ... as above ...
        sd := &SubscriberDropper{c1, c2}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器