gRPC框架學習

安裝Protobuf

下載安裝:https://github.com/protocolbuffers/protobuf/releases
配置環(huán)境變量:$GOPATH/bin(解壓后的程序文件放到goPath目錄,否則會出現protoc命令不存在或者protoc-gen-go不存在等問題)

安裝gRPC核心庫

# 安裝protocol編譯器
go get google.golang.org/grpc
# 安裝各語言的代碼生成工具
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

編寫proto文件

// 聲明使用的是proto3語法
syntax = "proto3";

// 表示最后生成的go文件處于哪個目錄哪個包中,【.】代表當前目錄生成,service代表了生成的go文件的包名是service
option go_package = ".;service";

// 定義了一個服務,服務中需要一個方法,這個方法可以接收客戶端的參數,再返回服務端的響應
// 這里定義了一個名為SayHello的service,這個服務中有一個rpc方法,名為SayHello,這個方法會發(fā)送一個HelloRequest,返回HelloResponse
service SayHello {
  rpc SayHello(HelloRequest) returns (HelloResponse) {}
}

message HelloRequest {
  string requestName = 1;
  int64 age = 2;
}

message HelloResponse {
  string responseMsg = 1;
}

生成對應文件

protoc --go_out=. test.proto # 對應文件名
protoc --go-grpc_out=. test.proto # 對應文件名

服務端編寫

1. 創(chuàng)建gRPC Server對象,可以理解為它是Server端的抽象對象
2. 將server(其包含需要被調用的服務端接口)注冊到gRPC Server的內部注冊中心,這樣可以在接受到請求時,通過內部的服務發(fā)現,發(fā)現該端口并轉接進行邏輯處理
3. 創(chuàng)建Listen,監(jiān)聽TCP端口
4. gRPC Server開始lis.Accept,直到Stop

package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    pb "grpc/server/proto"
    "net"
)

// hello server
type server struct {
    pb.UnimplementedSayHelloServer
}

func (s *server) SayHello(ctx context.Context,req *pb.HelloRequest) (*pb.HelloResponse, error) {
    fmt.Println("打印:" + req.RequestName)
    return &pb.HelloResponse{ResponseMsg: "hello" + req.RequestName}, nil
}

func main() {
    // 開啟端口
    listen,_  := net.Listen("tcp", ":9090")
    // 創(chuàng)建grpc服務
    grpcServer := grpc.NewServer()
    // 在grpc服務端中去注冊我們自己編寫的服務
    pb.RegisterSayHelloServer(grpcServer, &server{})

    // 啟動服務
    err := grpcServer.Serve(listen)
    if err != nil {
        fmt.Printf("啟動失?。?v", err)
        return
    }
}

客戶端編寫

package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    pb "grpc/server/proto"
)

func main() {
    conn, err := grpc.Dial("127.0.0.1:9090", grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        fmt.Printf("連接失敗:%v", err)
        return
    }
    defer conn.Close()

    // 建立連接
    client := pb.NewSayHelloClient(conn)

    resp, _ := client.SayHello(context.Background(), &pb.HelloRequest{RequestName: "張三"})
    fmt.Println(resp.GetResponseMsg())
}

TLS認證

## 服務端
    // TLS認證
    creds, _ := credentials.NewServerTLSFromFile("pem file path","key file path")
    // 開啟端口
    listen,_  := net.Listen("tcp", ":9090")
    // 創(chuàng)建grpc服務
    grpcServer := grpc.NewServer(grpc.Creds(creds))

## 客戶端
    // TLS認證
    creds, _ := credentials.NewClientTLSFromFile("pem file path","*.test.com")
    conn, err := grpc.Dial("127.0.0.1:9090", grpc.WithTransportCredentials(creds))

Token認證

gRPC提供了一個接口,這個接口有2個方法,接口位于credentials包下,這個接口需要客戶端來實現

type PerRPCCredentials interface {
    // 第一個方法用于獲取元數據,也就是客戶端提供的key-value對,context用于控制超時和取消,uri是請求入口處的uri
    GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
    // 第二個定義是否需要基于TLS認證進行安全傳輸,返回true則必須加上TLS驗證,false則不用
    RequireTransportSecurity() bool
}

示例代碼

## 客戶端
type ClientTokenAuth struct {}

func (c ClientTokenAuth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
    return map[string]string{
        "appId":  "zhangsan",
        "appKey": "123123",
    }, nil
}

func (c ClientTokenAuth) RequireTransportSecurity() bool {
    return false
}

// 連接到server端代碼,此處禁用安全傳輸,沒有加密和驗證
var opts []grpc.DialOption
opts = append(opts,grpc.WithTransportCredentials(insecure.NewCredentials()))
opts = append(opts,grpc.WithPerRPCCredentials(new(ClientTokenAuth)))

conn, err := grpc.Dial("127.0.0.1:9090", opts...)

#-------------------------------------------------------------------------------------#
## 服務端
func (s *server) SayHello(ctx context.Context,req *pb.HelloRequest) (*pb.HelloResponse, error) {
    // 獲取元數據的信息
    md, ok := metadata.FromIncomingContext(ctx)
    if !ok {
        return nil,errors.New("未傳輸token")
    }
    
    var appId string
    var appKey string
    if val, ok := md["appId"];ok{
        appId = val[0]
    }
    if val, ok := md["appKey"];ok{
        appKey = val[0]
    }
    
    if appId != "zhangsan" || appKey != "123123" {
        return nil,errors.New("token異常")
    }
    
    return &pb.HelloResponse{ResponseMsg: "hello" + req.RequestName}, nil
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容