美文网首页
grpc expose http

grpc expose http

作者: KevinL水杉 | 来源:发表于2021-02-03 16:09 被阅读0次

    Protocol Buffers🐣

    Open source: grpc-expose-http

    Dependent🔗

    Download HTTP plugin

    $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
    

    Run⛹️‍♂️

    Compile the ProtoBuf file

    1. Exec http_proto

    2. Start the gRPC service first

    go run -v grpc_server.go
    
    1. Then start the HTTP service
    go run -v http_server.go
    

    Test it🐞

    GET

    // request
    \
    curl -X GET \
      http://localhost:8080/get/hello%20gRpc \
      -H 'content-type: application/json' \
      | json_pp
    
    // response
    > curl -X GET \
    >   http://localhost:8080/get/hello%20gRpc \
    >   -H 'content-type: application/json' \
    >   | json_pp
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100    26  100    26    0     0   2363      0 --:--:-- --:--:-- --:--:--  2363
    {
       "value" : "Get=hello gRpc"
    }
    

    POST

    // request
    \
    curl -X POST \
      http://localhost:8080/post \
      -H 'content-type: application/json' \
      -d '{"value":"hello gRPC"}' \
     |  json_pp
    
    // response
    > curl -X POST \
    >   http://localhost:8080/post \
    >   -H 'content-type: application/json' \
    >   -d '{"value":"hello gRPC"}' \
    >  |  json_pp
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100    49  100    27  100    22   1500   1222 --:--:-- --:--:-- --:--:--  2722
    {
       "value" : "Post=hello gRPC"
    }
    

    Protocol Buffers🌽

    annotations.proto and http.proto copy from grpc-gateway, these two files are required to expose HTTP

    Compile🤖

    In the current directory, exec the command to compile annotations.proto and http.proto:

    protoc --go_out=plugins=grpc:./ ./http.proto
    protoc --go_out=plugins=grpc:./ ./annotations.proto
    

    generate these two files:

    -rw-r--r--  1 kevin  staff   2.4K Feb  3 14:19 annotations.pb.go
    -rw-r--r--  1 kevin  staff    20K Feb  3 14:19 http.pb.go
    

    continue compiling rest.proto:

    protoc -I/usr/local/include -I. \
        -I${GOPATH}/src \
        -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
        --grpc-gateway_out=. --go_out=plugins=grpc:.\
         rest.proto
    

    generate these two files:

    -rw-r--r--  1 kevin  staff   7.4K Feb  3 14:46 rest.pb.go
    -rw-r--r--  1 kevin  staff    10K Feb  3 14:46 rest.pb.gw.go
    

    👋👋👋

    Proto pre work is finished

    相关文章

      网友评论

          本文标题:grpc expose http

          本文链接:https://www.haomeiwen.com/subject/zjoptltx.html