前言
網(wǎng)上很多文章不是新版的,有問題,自己嘗試了一把,在此記錄。
grpc安裝:
//protobuf的安裝
PB_REL="https://github.com/protocolbuffers/protobuf/releases" && curl -LO $PB_REL/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip
//protoc-gen-go proto生成go的代碼工具
go get github.com/golang/protobuf/protoc-gen-go
//注意設(shè)置環(huán)境變量即可
參考文檔:https://www.grpc.io/docs/quickstart/go/
proto書寫:
syntax = "proto3"; //版本
option go_package = ".;hello"; //生成包名hello
service Greeter { //定義service
rpc SayHello(HelloRequest)returns(HelloReply){}
rpc SayHelloAgain(HelloRequest) returns(HelloReply){}
}
message HelloRequest{ //定義請求
string name = 1;
}
message HelloReply{ //定義返回
string message = 1;
}
參考文檔:
https://developers.google.com/protocol-buffers
proto文件生成go代碼
//gprc使用
protoc --proto_path=proto --go_out=plugins=grpc:hello --go_opt=paths=source_relative proto/hello.proto
//僅包含proto生成為go代碼
protoc --proto_path=proto --go_out=hello --go_opt=paths=source_relative proto/hello.proto
參考文檔:https://www.grpc.io/docs/quickstart/go/#whats-next
代碼書寫
服務(wù)端:
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"grpc_demo/hello"
"net"
)
type Server struct {
}
func main() {
g := grpc.NewServer()
s := Server{}
hello.RegisterGreeterServer(g,&s)
lis, err := net.Listen("tcp", fmt.Sprintf(":8080"))
if err != nil {
panic("failed to listen: "+err.Error())
}
g.Serve(lis)
}
func (s *Server) SayHello(ctx context.Context,request *hello.HelloRequest)(*hello.HelloReply,error){
return &hello.HelloReply{Message:"Hello "+request.Name},nil
}
func (s *Server) SayHelloAgain(ctx context.Context,request *hello.HelloRequest)(*hello.HelloReply,error){
return &hello.HelloReply{Message:"Hello Again "+request.Name},nil
}
客戶端:
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"grpc_demo/hello"
)
func main() {
conn,err := grpc.Dial("127.0.0.1:8080",grpc.WithInsecure())
if err!=nil{
panic(err)
}
defer conn.Close()
c := hello.NewGreeterClient(conn)
r,err := c.SayHello(context.Background(),&hello.HelloRequest{Name:"ucan"})
if err!=nil{
panic(err)
}
fmt.Println(r.Message)
}
demo地址:http://gitee.com/ucanme/grpc_demo
帶證書版本代碼
util.go
package util
import (
"crypto/tls"
"golang.org/x/net/http2"
"io/ioutil"
"log"
)
func GetTLSConfig(certPemPath, certKeyPath string) *tls.Config {
var certKeyPair *tls.Certificate
cert, _ := ioutil.ReadFile(certPemPath)
key, _ := ioutil.ReadFile(certKeyPath)
pair, err := tls.X509KeyPair(cert, key)
if err != nil {
log.Println("TLS KeyPair err: %v\n", err)
}
certKeyPair = &pair
return &tls.Config{
Certificates: []tls.Certificate{*certKeyPair},
NextProtos: []string{http2.NextProtoTLS},
}
}
server.go
package main
import (
"context"
"crypto/tls"
"fmt"
"google.golang.org/grpc"
"grpc_demo/hello"
"grpc_demo/util"
"net"
)
type Server struct {
}
func main() {
g := grpc.NewServer()
s := Server{}
hello.RegisterGreeterServer(g, &s)
conn, err := net.Listen("tcp", fmt.Sprintf(":8080"))
if err != nil {
panic("failed to listen: " + err.Error())
}
//tls
g.Serve(tls.NewListener(conn, util.GetTLSConfig("./cert/server.pem", "./cert/server.key")))
}
func (s *Server) SayHello(ctx context.Context, request *hello.HelloRequest) (*hello.HelloReply, error) {
return &hello.HelloReply{Message: "Hello " + request.Name}, nil
}
func (s *Server) SayHelloAgain(ctx context.Context, request *hello.HelloRequest) (*hello.HelloReply, error) {
return &hello.HelloReply{Message: "Hello Again " + request.Name}, nil
}
client.go
/**
* @author: wuji
* @file: main
* @desc:
* @date: 2020/4/23 7:31 下午
*/
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"grpc_demo/hello"
)
func main() {
//證書
creds,err := credentials.NewClientTLSFromFile("../cert/server.pem","ucan")
if err!=nil{
panic(err)
}
conn,err := grpc.Dial("127.0.0.1:8080",grpc.WithTransportCredentials(creds))
if err!=nil{
panic(err)
}
defer conn.Close()
c := hello.NewGreeterClient(conn)
r,err := c.SayHello(context.Background(),&hello.HelloRequest{Name:"ucan"})
if err!=nil{
panic(err)
}
fmt.Println(r.Message)
}
proto編譯參考
編譯器使用
使用protoc命令編譯.proto文件,不同語言支持需要指定輸出參數(shù),如:
protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --javanano_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path/to/file.proto
這里詳細(xì)介紹golang的編譯姿勢:
-I 參數(shù):指定import路徑,可以指定多個-I參數(shù),編譯時按順序查找,不指定時默認(rèn)查找當(dāng)前目錄
--go_out :golang編譯支持,支持以下參數(shù)
plugins=plugin1+plugin2 - 指定插件,目前只支持grpc,即:plugins=grpc
M 參數(shù) - 指定導(dǎo)入的.proto文件路徑編譯后對應(yīng)的golang包名(不指定本參數(shù)默認(rèn)就是.proto文件中import語句的路徑)
import_prefix=xxx - 為所有import路徑添加前綴,主要用于編譯子目錄內(nèi)的多個proto文件,這個參數(shù)按理說很有用,尤其適用替代一些情況時的M參數(shù),但是實際使用時有個蛋疼的問題導(dǎo)致并不能達到我們預(yù)想的效果,自己嘗試看看吧
import_path=foo/bar - 用于指定未聲明package或go_package的文件的包名,最右面的斜線前的字符會被忽略
末尾 :編譯文件路徑 .proto文件路徑(支持通配符)
完整示例:
protoc -I . --go_out=plugins=grpc,Mfoo/bar.proto=bar,import_prefix=foo/,import_path=foo/bar:. ./*.proto