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

通过同一连接访问多个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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失