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 = ;
} service Gateway {
rpc Echo(StringMessage) returns (StringMessage) {
option (google.api.http) = {
post: "/v1/example/echo"
body: "*"
};
}
}
执行 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)
}
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)
}
}
四、开启服务
先开启 service,再开启 gateway

Golang gRPC 和 gRPC-gateway 结合使用的更多相关文章
- golang下的grpc
facebook的thrift也是开源rpc库,性能高出grpc一倍以上,grpc发展的较晚,期待以后有长足的进步.简单来说thrift = grpc + protobuf gRPC基于HTTP/2标 ...
- Go gRPC进阶-gRPC转换HTTP(十)
前言 我们通常把RPC用作内部通信,而使用Restful Api进行外部通信.为了避免写两套应用,我们使用grpc-gateway把gRPC转成HTTP.服务接收到HTTP请求后,grpc-gatew ...
- 带入gRPC:gRPC Streaming, Client and Server
带入gRPC:gRPC Streaming, Client and Server 原文地址:带入gRPC:gRPC Streaming, Client and Server 前言 本章节将介绍 gRP ...
- 带入gRPC:gRPC Deadlines
带入gRPC:gRPC Deadlines 原文地址:带入gRPC:gRPC Deadlines项目地址:https://github.com/EDDYCJY/go... 前言 在前面的章节中,已经介 ...
- gRPC Motivation and Design Principles | gRPC https://grpc.io/blog/principles/
gRPC Motivation and Design Principles | gRPC https://grpc.io/blog/principles/
- 如何在golang中打印grpc详细日志
最近捣鼓fabric,在一个tls证书问题上纠结挺久,连接orderer服务时候,grpc日志总是冷冰冰的显示这个信息 Orderer Client Status Code: (2) CONNECTI ...
- grpc(二)记一次grpc debug--io.grpc.StatusRuntimeException: UNKNOWN
1.起初是dingding一直报错: instance:服务器名 err:GrpcClient#placeOrder: io.grpc.StatusRuntimeException: UNKNOWN ...
- grpc:gRPC Concepts
本文介绍一些主要的gRPC概念. 服务定义 gRPC支持4种方法: 1.Unary RPCs where the client sends a single request to the server ...
- gRPC helloworld service, RESTful JSON API gateway and swagger UI
概述 本篇博文完整讲述了如果通过 protocol buffers 定义并启动一个 gRPC 服务,然后在 gRPC 服务上提供一个 RESTful JSON API 的反向代理 gateway,最后 ...
随机推荐
- Codeforces 632D Longest Subsequence 2016-09-28 21:29 37人阅读 评论(0) 收藏
D. Longest Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- [Zend Mail]发送中文名附件出现乱码解决方案
Zend Framework 1.0.* “=?UTF-8?B?”.base64_encode($title).“?=” 发送中文名附件,结果如图: 英文名附件,结果截图: 解决办法就是将中文文件名拼 ...
- [CentOS]Centos设置PATH全局变量
PATH确认方法 $ echo $PATH 根据优先级先后顺序用:分割,因此可以复数指定 PATH设定方法(临时) $ export PATH=$PATH:/usr/local/scala/bin P ...
- 基于MVC框架Aspose.Words打印到Word中写法
控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726 //前端打印写法 @{ ViewBag.Title = " ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- 使用filter进行登录验证,并解决多次重定向问题
最近在做关于filter登录验证的功能,防止未登录的用户直接通过地址进入系统 LoginFilter类:继承Filter接口 package com.ss.filter; import java.io ...
- netty网络通信中的tcp拆包问题
工作中的一个项目,我们的一个应用与银行系统进行tcp通信的时候,银行下送的报文有时会分多次返回.在tcp中这种数据包分多次小数据包发送的情况成为拆包问题. 其中一个,也是最常见的思路就是在报文的报文头 ...
- Tomcat 7.x/8.x 优化
一.优化Connector http://www.aikaiyuan.com/8466.html tomcat的运行模式有3种 1)bio 默认的模式,性能非常低下,没有经过任何优化处理和支持. 2) ...
- android界面
自定义控件EditText 在src中的drawable新建myeditext.xml文件<?xml version="1.0" encoding="utf-8&q ...
- 给XCode安装Alcatraz(包管理工具)!!
Alcatraz官方描述: Alcatraz is an open-source package manager for Xcode. It lets you discover and instal ...