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

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)
}

  

  

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目录下。

7、最后运行如下代码进行演示即可

go run server.go
go run client.go

  

时间: 2024-07-31 00:34:59

Golang gRPC 示例的相关文章

golang grpc UnaryServerInterceptor用法

有的时候,当进行grpc调用的时候,并不希望客户端与服务端建立连接后直接就进入对应的方法体内.比如需要验证签名来确认客户端的身份,再执行相应的方法.这个时候就可以哟拿到Interceptor. golang grpc的拦截器(Interceptor)为UnaryServerInterceptor,为一个指向函数的指针. UnaryServerInterceptor在服务端对于一次RPC调用进行拦截.UnaryServerInterceptor是一个函数指针,当客户端进行grpc调用的时候,首先并

golang grpc demo

grpm安装: git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc proto,protoc-gen-go安装: go get -u github.com/golang/protobuf/{proto,protoc-gen-go} 原文地址:https://www.cnblogs.com/wangjq19920210/p/11572283.html

golang grpc 双向

使用grpc双向模式,可以实现客户端随时发送消息给服务端,服务端也可以随时发送消息到客户端,不再是一问一答的模式. grpc_stream/hellowoldstream/helloworldstream.proto syntax = "proto3"; package pb; message HelloRequest { string username = 1; } message HelloResponse { string message = 1; } // + message

python的gRPC示例

参考URL: https://segmentfault.com/a/1190000015220713?utm_source=channel-hottest gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支持. gRPC 基于 HTT

Golang gRPC中间件:拦截器链接,验证,日志记录,重试等

gRPC Go Middleware: interceptors, helpers, utilities. Middleware gRPC Go recently acquired support for Interceptors, i.e. middleware that is executed either on the gRPC Server before the request is passed onto the user's application logic, or on the

golang json 示例

jsonStr, err := client.Get( deviceIdKey ).Result() if err == redis.Nil { deviceIds = []string{deviceId} fmt.Println("nil" ) } else if err != nil { //error r.status = -2 fmt.Println( "error ",err  ) return c.JSON(http.StatusOK, r) } els

Golang枚举示例之Day

GOPATH\src\day\day.go package day type Day uint const ( Sunday Day = iota Monday Tuseday Wednesday Thursday Friday Saturday ) func (day Day) String() string { switch day { case Sunday: return "Sunday" case Monday: return "Monday" case

golang server示例

一个简单的web服务器 package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe("localhost:8888", nil)) } func handler(w http.ResponseWriter, r *http.Re

golang语言示例

package main import "fmt" /* my first programmer in go */ func fib(n int) int{ if n<2{ return n } return fib(n-1)+fib(n-2) } func max(x, y int) int{ if x>y{ return x } return y } func main(){ var i int fmt.Println("Hello, world!"