gRPC-gateway 源码阅读

栏目: 服务器 · 发布时间: 7年前

内容简介:首先看一下要怎么用这个库,README里写着:搜索

https://github.com/grpc-ecosystem/grpc-gateway

首先看一下要怎么用这个库,README里写着:

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:. \
    path/to/your_service.proto

搜索 protoc plugin 可以得到: https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.plugin

可以得到这几个结论:

  • protoc --plugin=protoc-gen-NAME=path/to/mybinary --NAME_out=OUT_DIR 其中NAME是插件的名字, = 后边接的是二进制的路径
  • plugin 接受一个 CodeGeneratorRequest ,返回一个 CodeGeneratorResponse

https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/plugin.proto

从README中可以看到我们是这样安装 grpc-gateway 的: go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

然后我们就到 protoc-gen-grpc-gateway 中看 main.go :

func main() {
    flag.Parse()
    defer glog.Flush()

    reg := descriptor.NewRegistry()

    glog.V(1).Info("Parsing code generator request")
    req, err := codegenerator.ParseRequest(os.Stdin)
    if err != nil {
        glog.Fatal(err)
    }
    glog.V(1).Info("Parsed code generator request")
    if req.Parameter != nil {
        for _, p := range strings.Split(req.GetParameter(), ",") {
            spec := strings.SplitN(p, "=", 2)
            if len(spec) == 1 {
                if err := flag.CommandLine.Set(spec[0], ""); err != nil {
                    glog.Fatalf("Cannot set flag %s", p)
                }
                continue
            }
            name, value := spec[0], spec[1]
            if strings.HasPrefix(name, "M") {
                reg.AddPkgMap(name[1:], value)
                continue
            }
            if err := flag.CommandLine.Set(name, value); err != nil {
                glog.Fatalf("Cannot set flag %s", p)
            }
        }
    }

    g := gengateway.New(reg, *useRequestContext, *registerFuncSuffix, *pathType)

    if *grpcAPIConfiguration != "" {
        if err := reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil {
            emitError(err)
            return
        }
    }

    reg.SetPrefix(*importPrefix)
    reg.SetImportPath(*importPath)
    reg.SetAllowDeleteBody(*allowDeleteBody)
    if err := reg.Load(req); err != nil {
        emitError(err)
        return
    }

    var targets []*descriptor.File
    for _, target := range req.FileToGenerate {
        f, err := reg.LookupFile(target)
        if err != nil {
            glog.Fatal(err)
        }
        targets = append(targets, f)
    }

    out, err := g.Generate(targets)
    glog.V(1).Info("Processed code generator request")
    if err != nil {
        emitError(err)
        return
    }
    emitFiles(out)
}

其中:

  • req, err := codegenerator.ParseRequest(os.Stdin) 跳进去看,是从标准输入读取参数,然后返回一个 *plugin.CodeGeneratorRequest 对象。就是上面的proto文件中的CodeGeneratorRequest
  • g := gengateway.New(reg, *useRequestContext, *registerFuncSuffix, *pathType) 生成一个 gen.Generator 对象
  • out, err := g.Generate(targets) 生成代码
  • 跟进去, func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGeneratorResponse_File, error) 函数的实现, 发现调用了 code, err := g.generate(file)
  • 调用了 func (g *generator) generate(file *descriptor.File) (string, error) , 调用了 protoc-gen-grpc-gateway/gengateway/template.go 中的代码
  • template.go 下面就是我们要生成的代码的模板

如果生成一个demo看看,就知道,模板做的事情就是每接受一个HTTP请求,就生成一个gRPC请求。


以上所述就是小编给大家介绍的《gRPC-gateway 源码阅读》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Fluent Python

Fluent Python

Luciano Ramalho / O'Reilly Media / 2015-8-20 / USD 39.99

Learn how to write idiomatic, effective Python code by leveraging its best features. Python's simplicity quickly lets you become productive with it, but this often means you aren’t using everything th......一起来看看 《Fluent Python》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具