通过同一连接访问多个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个回答

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}
}
dongmo9996
dongmo9996 我相信这就是http2的重点。 通过单个连接多路复用多个请求。
大约一年之前 回复
douzi2778
douzi2778 是的,使用单个ClientConn进行多个并发RPC是安全的。
一年多之前 回复
dongzhi8487
dongzhi8487 这如何影响并发性? 例如,假设c1和c2`都具有流gRPC-都可以在同一单个连接上同时调用它们,并且两者将同时流化吗?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐