我目前刚接触gRPC技术并且正在阅读它。
我目前的理解是gRPC
只是另一种协议,就像REST
是一样。现在假设我启动了一个我希望客户端使用的gRPC服务器,但是在那个gRPC服务器中,我希望能够从外部消费者RESTful Apis获取信息,(例如https://developer.riotgames.com/api-methods/)是还有可能吗?
答案 0 :(得分:2)
是的,有可能。您可以使用自己的gRPC服务代码调用其他API和服务。
让您的客户打电话给您的gRPC服务。然后,您的服务对外部API进行REST调用(可能使用客户端请求中的参数到您的gRPC服务)并对其进行处理。将结果返回给您的客户,但是您的gRPC服务会响应。
答案 1 :(得分:0)
由于grcp-gateway
go-grpc-tutorial
phuongdo
生成上可用的代理完整代码
安装代码:
$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}"
};
}
}
为服务器和clinet生成存根
$ 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
和reverse proxy
的{{1}}
REST API
服务器/ server.go:
$ 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
写rest api,server / server-rproxy.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)
}
}
构建客户端服务器/ client.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)
}
}
最后运行服务器,客户端,休息和呼叫休息:
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)
}
1:https://github.com/phuongdo/go-grpc-tutorial 2:https://i.stack.imgur.com/C4s7s.png