golang GRPC文档(1)

mac2022-06-30  60

go语言小白,最近开始接触grpc,特此记录一下。

1.grpc安装

GRPC是一个高性能、开源、通用的RPC框架,面向移动和HTTP/2设计,是由谷歌发布的首款基于Protocol Buffers的RPC框架。 目前grpc提供C、JAVA、GO语言版本,其代码都托管于github上,分别是:grpc, grpc-java, grpc-go。其中C版本支持C,C++,Node.js,Python,Ruby,Objective-C,PHP 和 C#。 本文只介绍grpc-go的安装及使用。 1.1 检查go语言版本


$go version go version go1.6.3 linux/amd64

grpc要求go语言版本至少为1.5+,版本过低的请先更新go语言版本:Getting Started - The Go Programming Language 1.2安装grpc

$ go get google.golang.org/grpc

1.3安装Protocol Buffers v3

protocol buffer是用来生成gRPC服务代码的。 安装GO的protoc插件:

$ go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

编译器插件protoc-gen-go安装在\(GOBIN目录下,为了确保编译器protoc能找到protoc-gen-go插件,所以将\)GOPATH导入\(PATH路径下: [plain] view plain copy export PATH=\)PATH:$GOPATH/bin [plain] view plain copy echo \(PATH //查看\)PATH

*****gRPC安装完成!

2.测试grpc

2.1定义service

一个RPC service就是一个能够通过参数和返回值进行远程调用的method,我们可以简单地将它理解成一个函数。因为gRPC是通过将数据编码成protocal buffer来实现传输的。因此,我们通过protocal buffers interface definitioin language(IDL)来定义service method,同时将参数和返回值也定义成protocal buffer message类型。具体实现如下所示,创建一个helloworld文件夹,编辑helloworld.proto文件: [plain] view plain copy 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; }

2.2利用protocal buffer compiler生成对应的Golang代码

根据上述定义的service,我们可以利用protocal buffer compiler ,即protoc生成相应的服务器端和客户端的GoLang代码。生成的代码中包含了客户端能够进行RPC的方法以及服务器端需要进行实现的接口。

假设现在所在的目录是$GOPATH/src/helloworld/helloworld,我们将通过如下命令生成gRPC对应的GoLang代码:

[plain] view plain copy protoc --go_out=plugins=grpc:. helloworld.proto 此时,将在目录下生成helloworld.pb.go文件。

2.3测试gRPC服务

在目录$GOPATH/src/helloworld/下创建server.go 和client.go,分别用于服务器和客户端的实现。 server.go

[plain] view plain copy 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) } client.go

[plain] view plain copy 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目录下。

2.4运行server/client.go [plain] view plain copy $ go run greeter_client/main.go

[plain] view plain copy go run server.go [plain] view plain copy //重新打开一个终端 go run client.go

2.5

注意:如果你是通过go get google.golang.org/grpc安装的grpc,在$GOPATH/src/google.golang.org/grpc/examples目录下已经包含该测试用例

切换到examples目录下 [plain] view plain copy $ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld 运行server/client.go [plain] view plain copy $ go run greeter_server/main.go [plain] view plain copy $ go run greeter_client/main.go 运行成功,会在client的终端输出:Greeting: Hello world

转载于:https://www.cnblogs.com/Dennis-mi/articles/7647016.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)