Go   发布时间:2019-10-06  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang gRPC示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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 (
    "strconv"
    "Strings"
    "sync"
    "time"

    "math/rand"

    "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.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,21)">":")[0]); id != r {
        log"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示例所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。