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}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R