Golang 学习手册
  • 主页
  • 安装
  • 参考资料
  • Q&A
  • 基础
    • Hello,world!
    • 包
    • 变量
    • 常量
    • 类型
      • 整型
      • 浮点型
      • 复数
      • 布尔型
      • 字符
      • 字符串
      • 数组
      • 切片
      • 字典
      • 指针
      • 方法
      • 结构体
      • 接口
      • 自定义类型
    • 语法
      • 判断
      • 循环
      • 错误处理
    • 协程
      • 锁
      • 等待组
      • 通道
      • 上下文
    • 测试与分析
      • 单元测试
      • 性能测试
      • 性能分析
    • 编译
      • 条件编译
      • 交叉编译
  • 高级
    • 存储
      • 键值存储
        • Etcd
    • HTTP
      • http服务基础
      • Websocket
      • 开源框架
        • Beego
        • Echo
        • Gin
        • Iris
        • Revel
        • 框架对比
      • Kubernetes风格API框架
    • RPC
      • RPCX
      • GRPC
      • grpc-gateway
    • 连接分发器
    • TLS加密
    • 链路追踪
      • skywalking
  • 项目管理
    • 代码规范
    • 包管理
    • 文档
      • GoDoc
      • Markdown
      • Swagger
    • 仓库管理
      • 分支管理
      • 问题管理
      • 里程碑管理
      • 发布管理
    • 持续集成
      • CircleCI
      • TravisCI
由 GitBook 提供支持
在本页
  • 项目地址
  • 下载与安装
  • 例子:简单的grpc-gateway服务

这有帮助吗?

在Git上编辑
  1. 高级
  2. RPC

grpc-gateway

上一页GRPC下一页连接分发器

最后更新于5年前

这有帮助吗?

grpc服务的代理网关,将grpc服务接口转为restful接口对外提供服务。

项目地址

下载与安装

接下来安装相关的包

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

例子:简单的grpc-gateway服务

package main

import (
	"context" // Use "golang.org/x/net/context" for Golang version <= 1.6
	"flag"
	"net/http"

	"github.com/golang/glog"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
	"google.golang.org/grpc"

	gw "demo/http/frameworks/grpc-gateway/simple/protos" // Update
)

var (
	// command-line options:
	// gRPC server endpoint
	grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:9090", "gRPC server endpoint")
)

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

	// Register gRPC server endpoint
	// Note: Make sure the gRPC server is running properly and accessible
	mux := runtime.NewServeMux()
	opts := []grpc.DialOption{grpc.WithInsecure()}
	err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
	if err != nil {
		return err
	}

	// Start HTTP server (and proxy calls to gRPC server endpoint)
	return http.ListenAndServe("localhost:8080", mux)
}

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

	if err := run(); err != nil {
		glog.Fatal(err)
	}
}
syntax = "proto3";
package example;

import "google/api/annotations.proto";

message StringMessage {
  string value = 1;
}

service YourService {
  rpc Echo(StringMessage) returns (StringMessage) {
      option (google.api.http) = {
          post: "/v1/example/echo"
          body: "*"
      };
  }
}

执行命令

protoc -I/usr/local/include -I. \
  -I$GOPATH/src \
  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  --go_out=plugins=grpc:. \
  protos/helloworld.proto

这时protos/目录中会生成文件helloworld.pb.go,内容为grpc接口相关的定义

执行命令

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:. \
  protos/helloworld.proto

这时protos/目录中会生成文件helloworld.pb.gw.go,内容为http代理接口相关的定义

执行命令

protoc -I/usr/local/include -I. \
  -I$GOPATH/src \
  -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  --swagger_out=logtostderr=true:. \
  protos/helloworld.proto

这时protos/目录中会生成文件helloworld.swagger.json,内容为swagger api文档描述

执行命令go run main.go启动grpc代理网关服务

启动以下例子中的grpc服务端

开启一个新的控制台,执行

curl -X POST "http://127.0.0.1:8080/v1/example/echo" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"value\": \"string\"}"

响应结果

{"value":"Hello string"}

首先完成

grpc安装
GitHub - grpc-ecosystem/grpc-gateway: gRPC to JSON proxy generator following the gRPC HTTP specGitHub
https://golang-2.gitbook.io/handbook/wang-luo-bian-cheng/rpc/grpc#jian-dan-grpc-fu-wugolang-2.gitbook.io
Logo