zoukankan      html  css  js  c++  java
  • Golang gRPC 和 gRPC-gateway 结合使用

    一、安装

    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
    go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
    go get -u github.com/golang/protobuf/protoc-gen-go

    二、proto 文件

    syntax = "proto3";
    package gateway;
    
    import "google/api/annotations.proto";
    
    message StringMessage {
        string value = 1;
    }
    
    service Gateway {
       rpc Echo(StringMessage) returns (StringMessage) {
           option (google.api.http) = {
               post: "/v1/example/echo"
               body: "*"
           };
       }
    }
    View Code

    执行 protoc 编译,生成两个 go 文件,一个是提供 service 的,一个是 gateway 的:

    protoc --proto_path=../ -I/usr/local/include -I. -I/home/go-plugin/src -I/home/go-plugin/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --go_out=plugins=grpc:. gateway.proto
    protoc --proto_path=../ -I/usr/local/include -I. -I/home/go-plugin/src -I/home/go-plugin/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --grpc-gateway_out=logtostderr=true:. gateway.proto

     生成的文件如下:

    第一个是 service,第二个是 gateway

    三、编写 go 程序

    1、service

    package main
    
    import (
        "log"
        "net"
    
        pb "test_grpc/gateway"
        "google.golang.org/grpc"
        "golang.org/x/net/context"
    )
    
    const (
        PORT = ":9192"
    )
    
    type server struct {}
    
    func (s *server) Echo(ctx context.Context, in *pb.StringMessage) (*pb.StringMessage, error) {
        log.Println("request: ", in.Value)
        return &pb.StringMessage{Value: "Hello " + in.Value}, nil
    }
    
    func main() {
        lis, err := net.Listen("tcp", PORT)
    
        if err != nil {
            log.Fatalf("failed to listen: %v", err)
        }
    
        s := grpc.NewServer()
        pb.RegisterGatewayServer(s, &server{})
        log.Println("rpc服务已经开启")
        s.Serve(lis)
    }
    View Code

    2、gateway

    package main
    
    import (
        "flag"
        "net/http"
        "log"
    
        "github.com/golang/glog"
        "golang.org/x/net/context"
        "github.com/grpc-ecosystem/grpc-gateway/runtime"
        "google.golang.org/grpc"
        gw "test_grpc/gateway"
    )
    
    var (
        echoEndpoint = flag.String("echo_endpoint", "localhost:9192", "endpoint of Gateway")
    )
    
    func run() error {
        ctx := context.Background()
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
    
        mux := runtime.NewServeMux()
        opts := []grpc.DialOption{grpc.WithInsecure()}
        err := gw.RegisterGatewayHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)
        if err != nil {
            return err
        }
    
        log.Println("服务开启")
        return http.ListenAndServe(":8080", mux)
    }
    
    func main() {
        flag.Parse()
        defer glog.Flush()
    
        if err := run(); err != nil {
            glog.Fatal(err)
        }
    }
    View Code

    四、开启服务

    先开启 service,再开启 gateway

  • 相关阅读:
    简单命令行总结
    [大餐]开发摘记1--我的Fragment通信的框架 | 卖牙膏的芖口钉
    DZNEmptyDataSet的使用
    Java笔记(一)
    mingster.com
    2014新年福利,居然有人将Ext JS 4.1的文档翻译了
    【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之三
    【翻译】在Ext JS应用程序中使用自定义图标
    【翻译】Siesta事件记录器入门
    【翻译】使用新的Sencha Cmd 4命令app watch
  • 原文地址:https://www.cnblogs.com/linguoguo/p/10148467.html
Copyright © 2011-2022 走看看