1.安裝
1. 安裝protoc編譯器
它可以把我們編寫的 .proto文件編譯成不同語言的代碼
下載
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip
解壓
unzip protoc-3.11.4-linux-x86_64.zip
拷貝
將 protoc-3.11.4-linux-x86_64/bin
下的protoc
文件拷貝到 GOPATH
下
將protoc-3.11.4-linux-x86_64/include
下的google
文件夾拷貝到/usr/local/include
目錄
2.安裝grpc
直接go get安裝會報錯,被牆了,所以換成以下方式
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
安裝上面最後一條時可能會報錯,cannot find package "golang.org/x/sys/unix" in any of:golang.org
也需要翻牆,可以用下面這種方式解決(如果沒報錯則跳過下面這一步就好了)
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/sys.git
安裝剩下的
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
cd $GOPATH/src/
go install google.golang.org/grpc
沒輸出什麼的話就安裝成功
2.編寫proto文件,編譯成需要的語言代碼(golang)
1.創建一個hello.proto
其實就是定義一個服務,然後後面需要實現它的接口
syntax = "proto3"
package hello;
service HelloService {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
int age = 2;
}
message HelloReply {
string time = 1;
}
2.編譯
protoc --go_out=plugins=grpc:. *.proto
會在當前文件夾生成hello.pb.go
3.目錄樹
grpctest/
├── client
│ ├── go.mod
│ ├── go.sum
│ ├── main.go
│ └── proto
│ └── hello
│ ├── hello.pb.go
│ └── hello.proto
└── server
├── go.mod
├── go.sum
├── handler
│ └── handler.go
├── proto
│ └── hello
│ ├── hello.pb.go
│ └── hello.proto
└── service.go
4.實現並啓動定義的服務
1. 在server/handler/handler.go
文件中定義一個結構體,然後實現剛剛的SayHello
方法
package handler
import (
"context"
"fmt"
hellopb "server/proto/hello"
"time"
)
type hellosrv struct {}
func NewHelloSrv() hellopb.HelloServiceServer {
return &hellosrv{}
}
func (*hellosrv)SayHello(ctx context.Context, in *hellopb.HelloRequest) (*hellopb.HelloReply, error) {
fmt.Println("姓名:", in.Name, "年齡:", in.Age)
var time string = time.Now().String()
return &hellopb.HelloReply{Time: time}, nil
}
2. 在server/service.go
文件中編寫啓動服務
package main
import (
"fmt"
"net"
hellopb "server/proto/hello"
"server/handler"
"google.golang.org/grpc"
)
const (
PORT = ":7778"
)
func main() {
lis, err := net.Listen("tcp", PORT)
if err != nil {
fmt.Println(err)
}
grpcService := grpc.NewServer()
//註冊服務
hellopb.RegisterHelloServiceServer(grpcService, handler.NewHelloSrv())
if err := grpcService.Serve(lis); err != nil {
fmt.Println(err)
}
}
3. 啓動服務
go run service.go
5.客戶端調用服務
1. 需要將上面編譯生成的hello.pb.go
文件copy過來先,接着在client/main.go
中調用服務
package main
import (
hellopb "client/proto/hello"
"google.golang.org/grpc"
"context"
"fmt"
"time"
)
const (
ADDRESS = "127.0.0.1:7778"
)
func main() {
conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close()
client := hellopb.NewFirstServiceClient(conn)
resp, err := client.SayHello(context.Background(), &hellopb.HelloRequest{Name: "lubenwei", Age: "21"})
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Time)
}
2. 測試一下
go run main.go
client輸出:
2020-03-31 16:46:04.018755957 +0800 CST m=+5.300707152
server
輸出:
姓名: lubenwei 年齡: 21