加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang gRPC 示例

发布时间:2020-12-16 18:09:13 所属栏目:大数据 来源:网络整理
导读: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下载安装包,例如:pr

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,分别用于服务器和客户端的实现。

packagemain
 
// server.go
 
import(
    "log"
    "net"
 
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    pb"helloworld/helloworld"
)
 
const(
    port =":50051"
)
 
typeserverstruct {}
 
func(s *server) SayHello(ctx context.Context,in *pb.HelloRequest) (*pb.HelloReply,error) {
    return&pb.HelloReply{Message:"Hello "+ in.Name},nil
}
 
funcmain() {
    lis,err := net.Listen("tcp",port)
    iferr != nil {
        log.Fatal("failed to listen: %v",err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s,&server{})
    s.Serve(lis)
}

packagemain
 
//client.go
 
import(
    "log"
    "os"
 
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    pb"helloworld/helloworld"
)
 
const(
    address     ="localhost:50051"
    defaultName ="world"
)
 
funcmain() {
    conn,err := grpc.Dial(address,grpc.WithInsecure())
    iferr != nil {
        log.Fatal("did not connect: %v",err)
    }
    deferconn.Close()
    c := pb.NewGreeterClient(conn)
 
    name := defaultName
    iflen(os.Args) >1 {
        name = os.Args[1]
    }
    r,err := c.SayHello(context.Background(),&pb.HelloRequest{Name: name})
    iferr != 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读