開始前準(zhǔn)備
安裝gprc
go get -u google.golang.org/grpc
安裝protocol buffers
protoc編譯器這個用于生成gRPC服務(wù)代碼的。
下載解壓后放入PATH
路徑乘碑,供后續(xù)使用钝鸽。接下來安裝protoc
的go語言的插件go get -u github.com/golang/protobuf/protoc-gen-go
。
注意:這邊插件也必須要在
PATH
路徑下
栗子 采用$GOPATH/src/google.golang.org/grpc/examples/helloworld
的栗子
新建helloworld.proto,這個文件可以供多人使用。
syntax = "proto3"; //語法聲明
package helloworld; //包名
// Greeter 微服務(wù)
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// HelloRequest 請求數(shù)據(jù)格式
message HelloRequest {
string name = 1;
}
// HelloReply 響應(yīng)數(shù)據(jù)格式
message HelloReply {
string message = 1;
}
生成golang的服務(wù)代碼
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
這個指令支持*.proto
模糊匹配。如果有許多文件可以使用helloworld/*.proto
來作為PROTO_FILES
服務(wù)端代碼
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/reflection"
)
const (
port = ":50051"
)
type server struct{} //服務(wù)對象
// SayHello 實現(xiàn)服務(wù)的接口 在proto中定義的所有服務(wù)都是接口
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.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer() //起一個服務(wù)
pb.RegisterGreeterServer(s, &server{})
// 注冊反射服務(wù) 這個服務(wù)是CLI使用的 跟服務(wù)本身沒有關(guān)系
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
客戶端代碼
package main
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
//建立鏈接
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
// 1秒的上下文
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
總結(jié):grpc所構(gòu)建的服務(wù)可以作為微服務(wù)使用辱挥,供接口或者其他模塊使用。