gRPC使用簡介

2021-09-19 17:44:20 字數 4289 閱讀 2291

grpc是一款語言中立、平台中立、開源的遠端過程呼叫(rpc)系統。在grpc裡,客戶端應用程式可以像呼叫本地物件一樣直接呼叫另一台不同機器上,服務端應用的方法,使得你能夠更容易的建立分布式應用和服務。與許多rpc系統類似,grpc也是基於以下理念:定義乙個服務,指定其能夠被遠端呼叫的方法(包括引數和返回型別)。在服務端實現這個介面,並執行乙個grpc伺服器來處理客戶端呼叫。

要使用grpc,你的go版本必須高於1.6

安裝grpc,使用命令:$ go get -u google.golang.org/grpc

安裝protocol buffers.

安裝protoc的go外掛程式,命令$ go get -u github.com/golang/protobuf/protoc-gen-go

設定環境變數path,將$gopath/bin新增進去。

服務定義

與其他rpc系統一樣,grpc基於定義服務的思想來註冊服務。

syntax = "proto3";

package main;

// the greeting service definition.

service greeter

}// the request message containing the user's name.

message hellorequest

// the response message containing the greetings

message helloreply

grpc可以讓你定義四種服務方法:

rpc sayhello(hellorequest) returns (helloresponse) {}
rpc lostofreplies(hellorequest) returns (stream helloresponse) {}
rpc lostofgreetings(stream hellorequest) returns (helloresponse) {}
rpc bidihello(stream hellorequest) returns (stream helloresponse) {}
生成處理訊息的**
$ protoc -i=. ./helloworld.proto --go_out=plugins=grpc:.
命令執行完成後,會在當前目錄下生成helloworld.pb.go檔案。

在服務端啟用grpc

package main

import (

"context"

"log"

"net"

"google.golang.org/grpc"

)const (

port = ":50051"

)// server is used to implement helloworld.greeterserver.

type server struct{}

// sayhello implements helloworld.greeterserver

func (s *server) sayhello(ctx context.context, in *hellorequest) (*helloreply, error) , nil

}func main()

s := grpc.newserver()

registergreeterserver(s, &server{})

if err := s.serve(lis); err != nil

}

在客戶端啟用grpc
package main

import (

"context"

"log"

"os"

"time"

"google.golang.org/grpc"

)const (

address = "localhost:50051"

defaultname = "world"

)func main()

defer conn.close()

c := newgreeterclient(conn)

// contact the server and print out its response.

name := defaultname

if len(os.args) > 1

ctx, cancel := context.withtimeout(context.background(), time.second)

defer cancel()

r, err := c.sayhello(ctx, &hellorequest)

if err != nil

log.printf("greeting: %s", r.message)

}

執行服務端:$ go run greeter_server.go

執行客戶端:$ go run greeter_client.go

執行後,你在客戶端命令列就會看到:greeting: hello world

下面更新greeter服務,讓它擁有兩個方法。編輯helloworld.proto檔案,更新greeter服務,讓其有sayhelloagain方法。

syntax = "proto3";

package main;

// the greeting service definition

service greeter

// sends another greeting

rpc sayhelloagain (hellorequest) returns (helloreply) {}

}// the request message containing the user's name

message hellorequest

// the response message containing the greetings

message helloreply

接著根據我們新的服務定義,更新grpc**。假設當前目錄下面有helloworld.proto檔案。

$ protoc -i=. ./helloworld.proto --go_out=plugins=grpc:.
命令執行完成後,會在當前目錄下生成helloworld.pb.go檔案。這時我們就需要更新serverclient**,在應用程式中編寫部分**實現呼叫新的方法。

更新服務端,編輯greeter_server.go檔案,新增下面函式:

func (s *server) sayhelloagain(ctx context.context, in *pb.hellorequest) (*pb.helloreply, error) , nil

}

r, err = c.sayhelloagain(ctx, &pb.hellorequest)

if err != nil

log.printf("greeting: %s", r.message)

執行服務端:$ go run greeter_server/main.go

執行客戶端:$ go run greeter_client/main.go

執行,你在客戶端命令列就會看到:

greeting: hello world

greeting: hello again world

參考文章grpc

gRPC使用的分析

看了docker的原始碼就不得不了解一下grpc這個鬼東西。下面借鑑一下別人的例項,讓我這個初次接觸的人做乙個,最直接的分析。首先編譯安裝protobuf 工具。定義將要用到的rpc呼叫方法,引數和返回值都在上面進行了定義 service userservice rpc logout userid ...

grpc 使用方法

php中使用grpc php需要安裝grpc擴充套件。使用protoc命令生成對應的php protoc php out plugins grpc exporttask.proto 生成 包括 exporttask wshexportformat.php wshexporttaskcreatetpl...

使用MagicOnion實現gRPC

1.什麼是grpc 官方文件 2.什麼是magiconion 3.服務端 新建乙個webapi專案 4.客戶端 新建乙個控制台程式 using consul using grpc.core using magiconion.client using serverdefinition using sy...