温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

grpcurl如何通过命令行访问gRPC服务

发布时间:2022-06-16 10:34:21 来源:亿速云 阅读:330 作者:iii 栏目:开发技术

本篇内容主要讲解“grpcurl如何通过命令行访问gRPC服务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“grpcurl如何通过命令行访问gRPC服务”吧!

    gRPC Server

    首先来写一个简单的 gRPC Server:

    helloworld.proto:

    syntax = "proto3";
    package proto;
    // The greeting service definition.
    service Greeter {
        // Sends a greeting
        rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    // The request message containing the user's name.
    message HelloRequest {
        string name = 1;
    }
    // The response message containing the greetings
    message HelloReply {
        string message = 1;
    }

    main.go

    package main
    import (
        "context"
        "fmt"
        "grpc-hello/proto"
        "log"
        "net"
        "google.golang.org/grpc"
        "google.golang.org/grpc/reflection"
    )
    func main() {
        lis, err := net.Listen("tcp", ":50051")
        if err != nil {
            log.Fatalf("failed to listen: %v", err)
        }
        server := grpc.NewServer()
        // 注册 grpcurl 所需的 reflection 服务
        reflection.Register(server)
        // 注册业务服务
        proto.RegisterGreeterServer(server, &greeter{})
        fmt.Println("grpc server start ...")
        if err := server.Serve(lis); err != nil {
            log.Fatalf("failed to serve: %v", err)
        }
    }
    type greeter struct {
    }
    func (*greeter) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloReply, error) {
        fmt.Println(req)
        reply := &proto.HelloReply{Message: "hello"}
        return reply, nil
    }

    运行服务:

    go run main.go
    server start ...

    grpcurl 安装

    这里我介绍三种方式:

    Mac

    brew install grpcurl

    Docker

    # Download image
    docker pull fullstorydev/grpcurl:latest
    # Run the tool
    docker run fullstorydev/grpcurl api.grpc.me:443 list

    go tool

    如果有 Go 环境的话,可以通过 go tool 来安装:

    go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest

    grpcurl 使用

    查看服务列表:

    grpcurl -plaintext 127.0.0.1:50051 list

    输出:

    grpc.reflection.v1alpha.ServerReflection
    proto.Greeter

    查看某个服务的方法列表:

    grpcurl -plaintext 127.0.0.1:50051 list proto.Greeter

    输出:

    proto.Greeter.SayHello

    查看方法定义:

    grpcurl -plaintext 127.0.0.1:50051 describe proto.Greeter.SayHello

    输出:

    proto.Greeter.SayHello is a method:
    rpc SayHello ( .proto.HelloRequest ) returns ( .proto.HelloReply );

    查看请求参数:

    grpcurl -plaintext 127.0.0.1:50051 describe proto.HelloRequest

    输出:

    proto.HelloRequest is a message:
    message HelloRequest {
      string name = 1;
    }

    请求服务:

    grpcurl -d '{"name": "zhangsan"}' -plaintext 127.0.0.1:50051 proto.Greeter.SayHello

    输出:

    {
      "message": "hello"
    }

    可能遇到的错误

    可能会遇到两个报错:

    1、gRPC Server 未启用 TLS:

    报错信息:

    Failed to dial target host "127.0.0.1:50051": tls: first record does not look like a TLS handshake

    解决:

    请求时增加参数:-plaintext,参考上面的命令。

    2、参数格式错误:

    报错信息:

    Error invoking method "greet.Greeter/SayHello": error getting request data: invalid character 'n' looking for beginning of object key string

    解决:

    -d 后面参数为 json 格式,并且需要使用 '' 包裹起来。

    到此,相信大家对“grpcurl如何通过命令行访问gRPC服务”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI