douqiao6563 2017-06-09 14:28
浏览 38

使用protobuf简化对瘦golang api的grpc调用

I am currently playing around with grpc on golang and trying to figure out the best way to make an extensible 'thin' api that i can use to pass all requests to their relevant services.

I currently have the structure of:

  • Service 1
    • routes.go
    • rpc.go
  • Service 2
    • routes.go
    • rpc.go

Within the routes files, it creates a new routeGroup that i then register in main, then each handler for a route parses any params etc into a protobuf request and calls a function within RPC:

func showFeed(c *gin.Context) {
    message, err := RpcFeedGet(&social.ShowRequest{LocationId: 1})

    resp := http.HttpResponse{c, "feed", err, message} 
    http.ParseResponse(&resp) //Checks if there is an error and handles response headers etc.
}

Now within rpc im currently defining the below for each service which seems to be really inefficient to me as there is a lot of code reuse, how would i optimize/reduce the code reuse within this method

func connect() (*grpc.ClientConn, pb.Service1Client) {

    //Consul is being used for discovery, this just gets the address to dial
    consul := discovery.Init()
    service, err := consul.Lookup("service1", "")

    if err != nil {
        log.Fatalf("Unable to get service definition: %w", err)
    }

    //Dial the connection to the service
    conn, err := grpc.Dial(service.Address(), grpc.WithInsecure())

    if err != nil {
        log.Fatalf("Unable to connect to client service: %v", err)
    }

    //Creates the service client (service1) and returns it
    client := pb.NewService1Client(conn)

    return conn, client
}

func RpcFeedGet(data *pb.ShowRequest) (string, error) {
    conn, client := connect()
    defer conn.Close()

    //This line calls the protobuf GetFeed rpc service
    resp, err := client.GetFeed(context.Background(), data)

    return rpcHelp.CheckResponse(resp, err) //Handles any errors returned
}

Is there a way i can pass a function to a sub function that calls the connect/defer within the Rpc function and also normalise the connect() method so i dont have to redeclare it for each service? bearing in mind it returns a different client on each instance?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

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