我所在公司的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,就希望可以做成用rpc来做内部的通讯,对外依然用Restful。于是就想到了google的grpc。

使用grpc的优点很多,二进制的数据可以加快传输速度,基于http2的多路复用可以减少服务之间的连接次数,和函数一样的调用方式也有效的提升了开发效率。

不过使用grpc也会面临一个问题,我们的微服务对外一定是要提供Restful接口的,如果内部调用使用grpc,在某些情况下要同时提供一个功能的两套API接口,这样就不仅降低了开发效率,也增加了调试的复杂度。于是就想着有没有一个转换机制,让Restful和gprc可以相互转化。

在网上看到一个解决方案,https://github.com/grpc-ecosystem/grpc-gateway,简单的说就是有一个网关服务器负责转化和代理转发。

如下图:

安装

首先要安装ProtocolBuffers 3.0及以上版本。

mkdir tmp
cd tmp
git clone https://github.com/google/protobuf
cd protobuf
./autogen.sh
./configure
make
make check
sudo make install

然后使用go get获取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

这里最好把编译生成的二进制文件的目录放在$PATH中,可以把$GOPATH/bin放入$PATH中。

示例

本示例是基于我的上一篇博客《google的grpc在glang中的使用》中的示例,如果有必要请先了解上一篇博客。

示例代码获取地址:https://github.com/andyidea/go-example

代码文件结构如下

└── src
└── grpc-helloworld-gateway
├── gateway
│   └── main.go
├── greeter_server
│   └── main.go
└── helloworld
├── helloworld.pb.go
├── helloworld.pb.gw.go
└── helloworld.proto

我们还是先看一下协议文件。helloworld.proto有一些变动,引入了google官方的api相关的扩展,为grpc的http转换提供了支持。

具体改动如下:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto"; package helloworld; import "google/api/annotations.proto"; // The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
post: "/v1/example/echo"
body: "*"
};
}
} // The request message containing the user's name.
message HelloRequest {
string name = 1;
} // The response message containing the greetings
message HelloReply {
string message = 1;
}

和之前的proto文件比较,新的文件增了

import "google/api/annotations.proto";

option (google.api.http) = {
post: "/v1/example/echo"
body: "*"

这里增加了对http的扩展配置。

然后编译proto文件,生成对应的go文件

cd src/grpc-helloworld-gateway

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

这里生成了helloworld/helloworld.pb.go文件。

helloworld.pb.go是server服务需要的,下一步我们需要使用protoc生成gateway需要的go文件。

cd src/grpc-helloworld-gateway

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

这里生成了helloworld/helloworld.pb.gw.go文件。这个文件就是gateway用来的协议文件,用来做grpc和http的协议转换。

协议文件处理完毕,就需要写gateway代码了。

gateway代码如下:

package main

import (
"flag"
"net/http" "github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"golang.org/x/net/context"
"google.golang.org/grpc" gw "grpc-helloworld-gateway/helloworld"
) var (
echoEndpoint = flag.String("echo_endpoint", "localhost:50051", "endpoint of YourService")
) func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel() mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := gw.RegisterGreeterHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)
if err != nil {
return err
} return http.ListenAndServe(":8080", mux)
} func main() {
flag.Parse()
defer glog.Flush() if err := run(); err != nil {
glog.Fatal(err)
}
}

首先echoEndpoint存储了需要连接的server信息,然后将这些信息和新建的server用gw.go中的RegisterGreeterHandlerFromEndpoint进行一个注册和绑定,这时低层就会连接echoEndpoint提供的远程server地址,这样gateway就作为客户端和远程server建立了连接,之后用http启动新建的server,gateway就作为服务器端对外提供http的服务了。

代码到此就完成了,我们测试一下。

先启动greeter_server服务,再启动gateway,这时gatway连接上greeter_server后,对外建立http的监听。

然后我们用curl发送http请求

curl -X POST -k http://localhost:8080/v1/example/echo -d '{"name": " world"}'

{"message":"Hello  world"}

流程如下:curl用post向gateway发送请求,gateway作为proxy将请求转化一下通过grpc转发给greeter_server,greeter_server通过grpc返回结果,gateway收到结果后,转化成json返回给前端。

这样,就通过grpc-gateway完成了从http json到内部grpc的转化过程。

grpc-gateway:grpc转换为http协议对外提供服务的更多相关文章

  1. grpc gateway 使用以及docker compose 集成

    1. grpc gateway 安装 参考,比较简单,有需要的依赖可以参考相资料 mkdir tmp cd tmp git clone https://github.com/google/protob ...

  2. gRPC+gRPC Gateway+swagger小记

    前言 本文记录了grpc-gateway的简单使用. 定义proto 先来看看最常规的 syntax = "proto3"; package protos; service Gre ...

  3. Go gRPC进阶-gRPC转换HTTP(十)

    前言 我们通常把RPC用作内部通信,而使用Restful Api进行外部通信.为了避免写两套应用,我们使用grpc-gateway把gRPC转成HTTP.服务接收到HTTP请求后,grpc-gatew ...

  4. 带入gRPC:gRPC Streaming, Client and Server

    带入gRPC:gRPC Streaming, Client and Server 原文地址:带入gRPC:gRPC Streaming, Client and Server 前言 本章节将介绍 gRP ...

  5. 带入gRPC:gRPC Deadlines

    带入gRPC:gRPC Deadlines 原文地址:带入gRPC:gRPC Deadlines项目地址:https://github.com/EDDYCJY/go... 前言 在前面的章节中,已经介 ...

  6. gRPC Motivation and Design Principles | gRPC https://grpc.io/blog/principles/

    gRPC Motivation and Design Principles | gRPC https://grpc.io/blog/principles/

  7. gRPC官方文档(通讯协议)

    文章来自gRPC 官方文档中文版 HTTP2 协议上的 gRPC 本文档作为 gRPC 在 HTTP2 草案17框架上的实现的详细描述,假设你已经熟悉 HTTP2 的规范.产品规则采用的是ABNF 语 ...

  8. grpc(二)记一次grpc debug--io.grpc.StatusRuntimeException: UNKNOWN

    1.起初是dingding一直报错: instance:服务器名 err:GrpcClient#placeOrder: io.grpc.StatusRuntimeException: UNKNOWN ...

  9. grpc:gRPC Concepts

    本文介绍一些主要的gRPC概念. 服务定义 gRPC支持4种方法: 1.Unary RPCs where the client sends a single request to the server ...

随机推荐

  1. android--实现通过点击链接打开apk(应用图标在桌面消失)

    首先在AndroidManifest.xml的MAIN Activity下追加以下内容.(启动Activity时给予) ※必须添加项 <intent-filter> <action ...

  2. 【CSAPP笔记】2. 整型运算

    现在想补补推荐这本书的理由. Most books on systems-computer architecture, compilers, operating systems, and networ ...

  3. P4安装

    P4安装篇 ubuntu 14.04为例子 一.首先要fork到自己的github里面 源码目录 https://github.com/p4lang/p4factory 然后fork到自己的githu ...

  4. Alpha版本冲刺(六)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  5. scrapy-yield scrapy.Request()不执行、失效、Filtered offsite request to错误 [转]

    scrapy错误:yield scrapy.Request()不执行.失效.Filtered offsite request to错误.首先我们在Request()方法里面添加这么一个东东: yiel ...

  6. SQLSERVER 使用XP开头的系统默认存储过程

    1. 根据官网上面的内容进行执行命令 EXEC xp_cmdshell 'dir *.exe'; 但是会报错 消息 ,级别 ,状态 ,过程 xp_cmdshell,行 [批起始行 ] SQL Serv ...

  7. poj 3067 Japan(树状数组求逆序数)

    链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...

  8. 【Linux笔记】ldconfig、ldd

    一.ldconfig ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig. ldconfig 命令的用途,主要是在默认搜寻目录(/ ...

  9. Day22-Django之信号

    1. 如果往数据库中增加数据的时候,希望生成一个日志.在数据保存之前以及保存之后. Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去 ...

  10. 【刷题】BZOJ 2157 旅游

    Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...