golang gRPC示例

golang gRPC示例,第1张

概述gRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节

gRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于http/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、AndroID和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省cpu使用、和电池寿命。
1、普通帐号安装protobuf

unzip protobuf-cpp-3.0.0-Alpha-3.zipcd protobuf-3.0.0-Alpha-3/./configuremake && sudo make installgo 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/infprotoc --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.gopackage mainimport (    "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 mainimport (    "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

总结

以上是内存溢出为你收集整理的golang gRPC示例全部内容,希望文章能够帮你解决golang gRPC示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1209847.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存