gRPC+gRPC Gateway+swagger小记

Foreword

This article documents the simple use of grpc-gateway.

Definition of proto

Let's look at the most general

syntax = "proto3";

package protos;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}
 
message HelloRequest {
  string name = 1;
}
 
message HelloReply {
  string message = 1;
}

Then join gateway and other related content

syntax = "proto3";

package protos;


// 1 导入 gateway 相关的proto 以及 swagger 相关的 proto
import "google/api/annotations.proto";
import "protoc-gen-swagger/options/annotations.proto";

// 2 定义 swagger 相关的内容
option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = {
  info: {
        title: "grpc gateway sample";
        version: "1.0"; 
        license: {
            name: "MIT";            
        };
  };
  schemes: HTTP;
};

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {
      // 3 标识接口路由
      option (google.api.http) = {
                post: "/hello_world"
                body: "*"
            };
  }
}
 
message HelloRequest {
  string name = 1;
}
 
message HelloReply {
  string message = 1;
}

From the proto generated files

Execute the following three commands.

protoc -I . --go_out=plugins=grpc:. hello.proto
protoc -I . --grpc-gateway_out=logtostderr=true:. hello.proto  
protoc -I . --swagger_out=logtostderr=true:. hello.proto  

Achieve service and start service

achieve

package services

import (
    "context"
    pb "grpc-sample/protos"
    "log"
)

type server struct{}

func NewServer() *server {
    return &server{}
}

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    log.Println("request: ", in.Name)
    return &pb.HelloReply{Message: "hello, " + in.Name}, nil
}

start up

package main

import (
    "google.golang.org/grpc"
    pb "grpc-sample/protos"
    "grpc-sample/services"
    "log"
    "net"
)

const (
    PORT = ":9192"
)

func main() {
    lis, err := net.Listen("tcp", PORT)

    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }

    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, services.NewServer())
    log.Println("rpc services started, listen on localhost:9192")
    s.Serve(lis)
}

Integration swagger

First download swagger ui static files.

The package them to go file.

go-bindata --nocompress -pkg swagger -o gateway/swagger/datafile.go third_party/swagger-ui/...

9M ...

写 gateway

package main

import (
    "github.com/elazarl/go-bindata-assetfs"
    "log"
    "net/http"
    "path"
    "strings"

    "github.com/golang/glog"
    "github.com/grpc-ecosystem/grpc-gateway/runtime"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    swagger "grpc-sample/gateway/swagger"
    gw "grpc-sample/protos"
)

func run() error {
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    defer cancel()

    gwmux, err := newGateway(ctx)
    if err != nil {
        panic(err)
    }

    mux := http.NewServeMux()
    mux.Handle("/", gwmux)
    mux.HandleFunc("/swagger/", serveSwaggerFile)
    serveSwaggerUI(mux)

    log.Println("grpc-gateway listen on localhost:8080")
    return http.ListenAndServe(":8080", mux)
}

func newGateway(ctx context.Context) (http.Handler, error) {
    opts := []grpc.DialOption{grpc.WithInsecure()}

    gwmux := runtime.NewServeMux()
    if err := gw.RegisterGreeterHandlerFromEndpoint(ctx, gwmux, ":9192", opts); err != nil {
        return nil, err
    }

    return gwmux, nil
}

func serveSwaggerFile(w http.ResponseWriter, r *http.Request) {
    if !strings.HasSuffix(r.URL.Path, "swagger.json") {
        log.Printf("Not Found: %s", r.URL.Path)
        http.NotFound(w, r)
        return
    }

    p := strings.TrimPrefix(r.URL.Path, "/swagger/")
    p = path.Join("../protos", p)

    log.Printf("Serving swagger-file: %s", p)

    http.ServeFile(w, r, p)
}

func serveSwaggerUI(mux *http.ServeMux) {
    fileServer := http.FileServer(&assetfs.AssetFS{
        Asset:    swagger.Asset,
        AssetDir: swagger.AssetDir,
        Prefix:   "third_party/swagger-ui",
    })
    prefix := "/swagger-ui/"
    mux.Handle(prefix, http.StripPrefix(prefix, fileServer))
}

func main() {
    defer glog.Flush()

    if err := run(); err != nil {
        glog.Fatal(err)
    }
}

The results are as follows:

to sum up

Grpc-gateway to access by way grpc service is very convenient, but performance will be sacrificed.

reference

https://segmentfault.com/a/1190000013513469

Guess you like

Origin www.cnblogs.com/catcher1994/p/11869532.html