gRPC服务器如何调用REST端点

gRPC服务器如何调用REST端点,rest,http,go,server,grpc,Rest,Http,Go,Server,Grpc,我目前是gRPC技术的新手,一直在阅读它 我目前的理解是,gRPC只是另一个协议,就像REST是另一个协议一样。现在让我们假设我启动了一个我希望客户端使用的gRPC服务器,但是在该gRPC服务器中,我希望能够从外部的消费者RESTful API获取信息(例如),这是否仍然可行 由于Phoungdo 安装deps: $sudo apt install libprotobuf-dev go get google.golang.org/grpc go get -u github.com/golan

我目前是gRPC技术的新手,一直在阅读它


我目前的理解是,
gRPC
只是另一个协议,就像
REST
是另一个协议一样。现在让我们假设我启动了一个我希望客户端使用的gRPC服务器,但是在该gRPC服务器中,我希望能够从外部的消费者RESTful API获取信息(例如),这是否仍然可行

由于
Phoungdo

  • 安装deps:

    $sudo apt install libprotobuf-dev
    
    go get google.golang.org/grpc
    
    go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
    
    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
    
  • 定义服务,pb/service.proto:

    syntax = "proto3";
    
    option go_package = "echo";
    
    package echo;
    
    import "google/api/annotations.proto";
    
      //Message represents a simple message sent to the Echo service.
    
     message Message {
    
     string id = 1;
    
    string msg = 2;
    }
    
    
     //Echo service responds to incoming echo requests.
    
    service EchoService {
    
      //Echo method receives a simple message and returns it.
    
       //The message posted as the id parameter will also be returned.
    
    rpc Echo(Message) returns (Message) {
    
    option (google.api.http) = {
    
        post: "/v1/example/echo/{id}/{msg}"
    };
    }
    }
    
  • 为服务器和客户端生成存根

    $ protoc -I/usr/local/include -I. \
    -I$GOPATH/src \
    -I$GOPATH/src/github.com/grpc-ecosystem/grpc-
     gateway/third_party/googleapis \
     --go_out=google/api/annotations.proto=github.com/grpc-ecosystem/grpc-
     gateway/third_party/googleapis/google/api,plugins=grpc:. \
     pb/service.proto
    
  • 对于
    restapi

    $ protoc -I/usr/local/include -I. \
    -I$GOPATH/src \
    -I$GOPATH/src/github.com/grpc-ecosystem/grpc-
     gateway/third_party/googleapis \
    --grpc-gateway_out=logtostderr=true:. \
    pb/service.proto
    
  • server/server.go:

    package main
    
    import (
    "flag"
    
    "github.com/golang/glog"
    pb "github.com/go-grpc-tutorial/pb"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    "net"
    )
    
    // Implements of EchoServiceServer
    
    type echoServer struct{}
    
    func newEchoServer() pb.EchoServiceServer {
    return new(echoServer)
    }
    
    func (s *echoServer) Echo(ctx context.Context, msg *pb.Message) 
     (*pb.Message, error) {
    glog.Info(msg)
    return msg, nil
    }
    
    func Run() error {
    listen, err := net.Listen("tcp", ":50051")
    if err != nil {
        return err
    }
    server := grpc.NewServer()
    pb.RegisterEchoServiceServer(server, newEchoServer())
    server.Serve(listen)
    return nil
    }
    
    func main() {
    flag.Parse()
    defer glog.Flush()
    
    if err := Run(); err != nil {
        glog.Fatal(err)
    }
    }
    
  • 编写rest api,server/server-rproxy.go:

    package main
    
     import (
    "flag"
    "net/http"
    "github.com/grpc-ecosystem/grpc-gateway/runtime"
    "github.com/golang/glog"
    pb "github.com/go-grpc-tutorial/pb"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
     )
    
     var (
    echoEndpoint = flag.String("echo_endpoint", "localhost:50051", 
    "endpoint of EchoService")
     )
    
    func RunEndPoint(address string, opts ...runtime.ServeMuxOption) 
     error {
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    defer cancel()
    
    mux := runtime.NewServeMux(opts...)
    dialOpts := []grpc.DialOption{grpc.WithInsecure()}
    err := pb.RegisterEchoServiceHandlerFromEndpoint(ctx, mux, *echoEndpoint, dialOpts)
    if err != nil {
        return err
    }
    
    http.ListenAndServe(address, mux)
    return nil
     }
    
    func main() {
    flag.Parse()
    defer glog.Flush()
    
    if err := RunEndPoint(":8080"); err != nil {
        glog.Fatal(err)
    }
     }
    
  • 生成客户端服务器/client.go:

     package main
    
    import (
    "log"
    "os"
    
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    pb "github.com/go-grpc-tutorial/pb"
    )
    
     const (
    address = "localhost:50051"
     defaultName = "PhuongDV"
    )
    
    func main() {
    // Set up a connection to the server.
    conn, err := grpc.Dial(address, grpc.WithInsecure())
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    c := pb.NewEchoServiceClient(conn)
    
    // Contact the server and print out its response.
    name := defaultName
    if len(os.Args) > 1 {
        name = os.Args[1]
    }
    r, err := c.Echo(context.Background(), &pb.Message{Id: "1", Msg: name})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }
    log.Printf("Greeting: %s", r.Msg)
    }
    
  • 最后运行服务器、客户端、rest和调用rest:

    $ go run server/server.go
    
    $ go run client/client.go
    
    
    $go run server/server-rproxy.go
    
     $ curl -X POST "http://localhost:8080/v1/example/echo/1/PhuongDV"
    
    : :


  • 是的,这是可能的。您可以从自己的gRPC服务代码调用其他API和服务


    只需让您的客户致电您的gRPC服务即可。然后,您的服务对外部API进行REST调用(可能使用从客户端请求到gRPC服务的参数)并对其进行处理。将结果返回给您的客户端,但您的gRPC服务会做出响应。

    “这仍然可能吗?”当然,为什么不呢?我对类似服务器端的东西不太熟悉,所以我想如果它是gRPC服务器,它将无法对外部端点进行http调用,如(GET、POST)因此,下面答案中的网关仅在我希望允许客户端向gRPC服务器发出REST请求时才是必需的。这仅适用于用户向我的gRPC服务器请求REST调用的情况,不是吗?您可以使用它进行任何设计,因为这里有所有构建块,如果您的问题是:使用您的客户端从REST调用gRCP服务器