Golang gRPC 示例
1、安装gRPC runtime
go get google.golang.org/grpc
为了自动生成Golang的gRPC代码,需要安装protocal buffers compiler以及对应的GoLang插件
2、protocal buffer安装
从https://github.com/google/protobuf/releases下载安装包,例如:protobuf-cpp-3.0.0-beta-3.zip,解压后
./configure
make && make install
再添加环境变量:export LD_LIBRARY_PATH=/usr/local/lib,之后protoc命令即可运行
3、安装GoLang protoc 插件
go get -a github.com/golang/protobuf/protoc-gen-go
4、定义service
一个RPC service就是一个能够通过参数和返回值进行远程调用的method,我们可以简单地将它理解成一个函数。因为gRPC是通过将数据编码成protocal buffer来实现传输的。因此,我们通过protocal buffers interface definitioin language(IDL)来定义service method,同时将参数和返回值也定义成protocal buffer message类型。具体实现如下所示,包含下面代码的文件叫helloworld.proto:
syntax = "proto3"; option java_package = "io.grpc.examples"; package helloworld; // The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
} // The request message containing the user's name.
message HelloRequest {
string name = 1;
} // The response message containing the greetings
message HelloReply {
string message = 1;
}
5、接着,根据上述定义的service,我们可以利用protocal buffer compiler ,即protoc生成相应的服务器端和客户端的GoLang代码。生成的代码中包含了客户端能够进行RPC的方法以及服务器端需要进行实现的接口。
假设现在所在的目录是$GOPATH/src/helloworld/helloworld,我们将通过如下命令生成gRPC对应的GoLang代码:
protoc --go_out=plugins=grpc:. helloworld.proto
此时,将在目录下生成helloworld.pb.go文件。
6、接着,在目录$GOPATH/src/helloworld/下创建server.go 和client.go,分别用于服务器和客户端的实现。
package main // server.go import (
"log"
"net" "golang.org/x/net/context"
"google.golang.org/grpc"
pb "helloworld/helloworld"
) const (
port = ":50051"
) type server struct {} func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
} func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatal("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
s.Serve(lis)
}
package main //client.go import (
"log"
"os" "golang.org/x/net/context"
"google.golang.org/grpc"
pb "helloworld/helloworld"
) const (
address = "localhost:50051"
defaultName = "world"
) func main() {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatal("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn) name := defaultName
if len(os.Args) >1 {
name = os.Args[1]
}
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatal("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
这里需要注意的是包pb是我们之前生成的helloworld.pb.go所在的包,并非必须如上述代码所示在$GOPATH/src/helloworld/helloworld目录下。
7、最后运行如下代码进行演示即可
go run server.go
go run client.go
Golang gRPC 示例的更多相关文章
- Golang gRPC调试工具
目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...
- Golang爬虫示例包系列教程(一):pedaily.com投资界爬虫
Golang爬虫示例包 文件结构 自己用Golang原生包封装了一个爬虫库,源码见go get -u -v github.com/hunterhug/go_tool/spider ---- data ...
- Golang gRPC学习(03): grpc官方示例程序route_guide简析
代码主要来源于grpc的官方examples代码: route_guide https://github.com/grpc/grpc-go/tree/master/examples/route_gui ...
- Golang gRPC 和 gRPC-gateway 结合使用
一.安装 go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get -u github.com/g ...
- Golang gRPC 使用
一.概念 1.gRPC默认使用protocol buffers,这是google开源的一套成熟的结构数据序列化机制(当然也可以使用其他数据格式如JSON),可以用proto files创建gRPC服务 ...
- [golang grpc] 框架介绍
官方网站 http://www.grpc.io/ http://www.grpc.io/docs/quickstart/go.html grpc安装 • go安装 目前grpc需要go 1.5以上版本 ...
- Golang gRPC微服务02: helloworld
安装protobuf 在windows下,直接下载release版本https://github.com/protocolbuffers/protobuf/releases/tag/v3.9.0然后把 ...
- Golang gRPC微服务01: 介绍
gRPC 是什么 gRPC是goole开源的一个RPC框架和库,支持多语言之间的通信.底层通信采用的是 HTTP2 协议.gRPC在设计上使用了 ProtoBuf 这种接口描述语言.这种IDL语言可以 ...
- golang gRPC初探
gRPC使用protocol buffers作为Interface Definition Language (IDL). gRPC的底层信息交互格式也使用的是protocol buffers. 默认情 ...
随机推荐
- csharp: Speech
Speech SDK 5.1https://www.microsoft.com/en-us/download/details.aspx?id=10121 detects mobile devices ...
- csharp: Domain-Driven Design(领域驱动设计)
http://dddsample.sourceforge.net/ https://github.com/citerus/dddsample-core http://dddsamplenet.code ...
- 创建WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的win ...
- 一些经典===>>用SQL语句操作数据
用SQL语句操作数据 结构化查询语言(Structured Query Language)简称SQL(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言 ...
- vmware mysql报kernel: EXT4-fs (dm-0): Unaligned AIO/DIO on inode 1055943 by mysqld; performance will be poor
kernel: EXT4-fs (dm-0): Unaligned AIO/DIO on inode 1055943 by mysqld; performance will be poor
- docker 使用
https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-docker-application http ...
- AJAX请求中含有数组解决办法
当我们发送AJAX请求的数据中带有数组时,是不能像普通JSON数据一样,直接放在data里发送给后台,比如有这样一个数据需要发送给后台: { "orderId": 22, &quo ...
- all things are difficult before they are easy
刚开始接触一项新知识时,总是感觉很难,只要你用心钻研下去,它会慢慢变简单的.
- android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件
简介 XStream 是一个开源项目,一套简单实用的类库,用于序列化对象与 XML 对象之间的相互转换. 将 XML 文件内容解析为一个对象或将一个对象序列化为 XML 文件. 1.下载工具 xstr ...
- 安卓第四天笔记-Sqlite
安卓第四天笔记-Sqlite 1.数据库的创建运行与更新 1.1.创建一个类继承SqliteOpenHelper 1.2.创建构造方法 /** * 数据库创建类 * @author 刘楠 * * 20 ...