Protobuf 安装

  1. 安装 protobuf
brew install protobuf
  1. 确认安装成功
protoc --version
  1. 安装代理(可选,先跳过,4超时再执行3)
go env -w GOPROXY=https://goproxy.cn
  1. 安装代码生成插件 protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

通信协议定义

  1. 定义通信协议
syntax = "proto3";

package hello;

option go_package = "./;hello";

message HelloRequest {
  string name = 1;
}
message HelloResponse {
  string message = 1;
}
service HelloService {
  rpc Say (HelloRequest) returns (HelloResponse);
}
  1. 生成代码,执行完毕后生成hello.pb.go 和 hello_grpc.pb.go
protoc --go_out=. hello.proto
protoc --go-grpc_out=. hello.proto

服务端

  1. 实现服务端
package main

import (
	"context"
	hi "github.com/hxchen/EasyGo/api/protobuf-spec"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"log"
	"net"
)

const (
	port = ":1234"
)

type server struct {
	hi.HelloServiceServer
}

func (s *server) Say(ctx context.Context, in *hi.HelloRequest) (*hi.HelloResponse, error) {
	log.Printf("Received: %s", in.Name)
	return &hi.HelloResponse{Message: "Hello " + in.Name}, nil
}

func listenAndService() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen, %v", err)
	}
	s := grpc.NewServer()
	hi.RegisterHelloServiceServer(s, &server{})
	reflection.Register(s)
	log.Print("the rpc server is started up\n")
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve %v", err)
	}
}

func main() {
	listenAndService()
}
  1. 启动服务端
go run main.go

客户端

  1. 实现客户端
package main

import (
	"context"
	rpc "github.com/hxchen/EasyGo/api/protobuf-spec"
	"google.golang.org/grpc"
	"log"
)

const PORT = ":1234"

func main() {
	conn, err := grpc.Dial(PORT, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("get an error : %v\n", err)
	}
	defer conn.Close()

	client := rpc.NewHelloServiceClient(conn)

	resp, err := client.Say(context.Background(), &rpc.HelloRequest{
		Name: "this is client",
	})
	if err != nil {
		log.Fatalf("invoke error \n")
	}

	log.Printf("resp : %s\n", resp.GetMessage())
}
  1. 启动客户端
go run main.go