go Grpc 入門

前言

網(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • date: 2019-04-25 22:16:01title: tech| 再探 grpc 折騰 grpc 過幾次...
    daydaygo閱讀 15,378評論 2 17
  • 寫在前面 首先聲明這篇文章只是介紹gRPC在Go語言中的使用入門級的文章,不包含多少深入的內(nèi)容,讀者對象是g...
    foundwei閱讀 2,753評論 0 5
  • 該篇文章介紹了golang的grpc編程。 通過下面的例子,你將會學(xué)到:1. 在一個.proto文件里define...
    曉_7611閱讀 1,432評論 0 3
  • 1,關(guān)于grpc-go golang 可以可以做grpc的服務(wù)端和客戶端。 官網(wǎng)的文檔: http://www.g...
    ebayboy閱讀 1,156評論 0 1
  • 原文出處:gRPC gRPC分享 概述 gRPC 一開始由 google 開發(fā),是一款語言中立、平臺中立、開源的遠(yuǎn)...
    小波同學(xué)閱讀 7,689評論 0 18

友情鏈接更多精彩內(nèi)容