gRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用、和电池寿命。
1、普通帐号安装protobuf
unzip protobuf-cpp-3.0.0-alpha-3.zip
cd protobuf-3.0.0-alpha-3/
./configure
make && sudo make install
go get -u github.com/golang/protobuf/protoc-gen-go #golang 插件
2、定义grpc.proto文件
syntax = "proto3"; //protobuf3协议
package inf;
//请求
message UserRq {
int32 id = 1;
}
//响应
message UserRp {
string name = 1;
}
//服务
service Data {
rpc GetUser(UserRq) returns (UserRp);
}
然后编译
cd ~/src/inf
protoc --go_out=plugins=grpc:. grpc.proto
3、get 所引用的包
go get -u google.golang.org/grpc
由于墙的原因,我们一些依赖的包文件可以通过下面方式下载到:
在 github 可以找到源码,下载后复制到对应目录即可的:
google.golang.org/grpc 对应的代码地址在: https://github.com/grpc/grpc-go
google.golang.org/cloud/compute/metadata 对应的代码地址在: https://github.com/GoogleCloudPlatform/gcloud-golang
golang.org/x/oauth2 对应的代码地址在: https://github.com/golang/oauth2
golang.org/x/net/context 对应的代码地址在: https://github.com/golang/net
这些包的源码也可以通过 http://gopm.io/ 或者 http://golangtc.com/download/package 进行下载.
4、服务端代码
// grpc project main.go
package main
import (
"inf"
"log"
"net"
"runtime"
"strconv"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
port = "41005"
)
type Data struct{}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
//起服务
lis, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
inf.RegisterDataServer(s, &Data{})
s.Serve(lis)
log.Println("grpc server in: %s", port)
}
// 定义方法
func (t *Data) GetUser(ctx context.Context, request *inf.UserRq) (response *inf.UserRp, err error) {
response = &inf.UserRp{
Name: strconv.Itoa(int(request.Id)) + ":test",
}
return response, err
}
5、客户端代码
package main
import (
"inf"
"log"
"runtime"
"strconv"
"strings"
"sync"
"time"
"math/rand"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
wg sync.WaitGroup
)
const (
networkType = "tcp"
server = "127.0.0.1"
port = "41005"
parallel = 50 //连接并行度
times = 100000 //每连接请求次数
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
currTime := time.Now()
//并行请求
for i := 0; i < int(parallel); i++ {
wg.Add(1)
go func() {
defer wg.Done()
exe()
}()
}
wg.Wait()
log.Printf("time taken: %.2f ", time.Now().Sub(currTime).Seconds())
}
func exe() {
//建立连接
conn, _ := grpc.Dial(server + ":" + port)
defer conn.Close()
client := inf.NewDataClient(conn)
for i := 0; i < int(times); i++ {
getUser(client)
}
}
func getUser(client inf.DataClient) {
var request inf.UserRq
r := rand.Intn(parallel)
request.Id = int32(r)
response, _ := client.GetUser(context.Background(), &request) //调用远程方法
//判断返回结果是否正确
if id, _ := strconv.Atoi(strings.Split(response.Name, ":")[0]); id != r {
log.Printf("response error %#v", response)
}
}
引用
http://www.infoq.com/cn/news/2015/03/grpc-google-http2-protobuf
https://github.com/google/protobuf
https://github.com/golang/protobuf
http://studygolang.com/articles/3192
有疑问加站长微信联系(非本文作者)