Go gRPC

tags: Go RPC Protocol buffer
category: Back-End
description: Go gRPC
created_at: 2021/08/07 22:00:00

cover image


事前準備

  • 裝好 DockerGo 環境
  • 會寫 Go

前言

Docker 是因為方便,不然用 LocalGo 也行,但是這邊用 Docker 當範例,這樣比較不會碰到一堆奇怪的環境問題。


拉下 Image

$ docker pull golang

建立並進入 Container,離開後自動刪除

$ docker run -it --rm golang

再來因為我們是在 Container 中要做這個實驗(?),而我們又沒有掛 volume,勢必要在裡面編輯文件,假設我使用 Vim


更新套件清單

$ apt update

安裝 Vim

$ apt install -y vim

安裝 protobuf-compiler

可參考: https://grpc.io/docs/protoc-installation/

$ apt install -y protobuf-compiler

安裝 protocol compiler 相關套件

$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]

如果你需要更新 PATH 的話

$ export PATH="$PATH:$(go env GOPATH)/bin"

切換目錄與建立目錄

cd /go/src
mkdir server
mkdir client
mkdir helloworld

初始化 go mod

$ go mod init example.com.tw

建立 helloworld/helloworld.proto

syntax = "proto3";

option go_package = "example.com.tw/helloworld/helloworld";

package helloworld;

service MyService {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

protobuf 語法可參考: https://developers.google.com/protocol-buffers/docs/proto3

編譯 proto

$ protoc helloworld/helloworld.proto --go_out=. --go-grpc_out=. --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative

建立 server/server.go

package main

import (
    "context"
    "log"
    "net"

    pb "example.com.tw/helloworld"
    "google.golang.org/grpc"
)

const (
    port = ":50051"
)

type server struct {
    pb.UnimplementedMyServiceServer
}

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    log.Printf("Received: %v", in.GetName())
    return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    return &pb.HelloReply{Message: "Hello again " + in.GetName()}, nil
}

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterMyServiceServer(s, &server{})
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

建立 client/client.go

package main

import (
    "context"
    "log"
    "os"
    "time"

    pb "example.com.tw/helloworld"
    "google.golang.org/grpc"
)

const (
    address     = "localhost:50051"
    defaultName = "world"
)

func main() {
    conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    c := pb.NewMyServiceClient(conn)

    name := defaultName
    if len(os.Args) > 1 {
        name = os.Args[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.GetMessage())


    r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})
    if err != nil {
        log.Fatalf("could not greet: %v", err)
    }
    log.Printf("Greeting: %s", r.GetMessage())
}

執行 Server

$ go run server/server.go

這時候會噴一些錯,跟你說少裝一些套件,就照他說的裝一裝

helloworld/helloworld_grpc.pb.go:7:2: no required module provides package google.golang.org/grpc; to add it:
        go get google.golang.org/grpc
helloworld/helloworld_grpc.pb.go:8:2: no required module provides package google.golang.org/grpc/codes; to add it:
        go get google.golang.org/grpc/codes
helloworld/helloworld_grpc.pb.go:9:2: no required module provides package google.golang.org/grpc/status; to add it:
        go get google.golang.org/grpc/status
helloworld/helloworld.pb.go:10:2: no required module provides package google.golang.org/protobuf/reflect/protoreflect; to add it:
        go get google.golang.org/protobuf/reflect/protoreflect
helloworld/helloworld.pb.go:11:2: no required module provides package google.golang.org/protobuf/runtime/protoimpl; to add it:
        go get google.golang.org/protobuf/runtime/protoimpl

裝好之後再跑一次

$ go run server/server.go

會發現沒有任何反應,但其實他已經執行了,所以我們在後面加一個 & 讓他在背景執行

$ go run server/server.go &

然後執行 Client

$ go run client/client.go

會得到輸出

2021/08/07 15:07:47 Received: world
2021/08/07 15:07:47 Greeting: Hello world
2021/08/07 15:07:47 Greeting: Hello again world

第一行是 Server 輸出,其餘是 Client 輸出的


總結

這個基本上就是照著官方 Quick start 整理一下然後稍微小改一點點的範例。




最後更新時間: 2021年08月07日.