gRPC是google開源高性能分布式RPC框架,支持http/2 雙向數據流傳輸及Protobuff,可以在任何環境下運行。 它可以有效地將數據中心內和跨數據中心的服務與可插拔支持進行負載均衡,跟蹤,運行狀況檢查和身份驗證。 它也適用於分布式計算,將設備,移動應用程序和瀏覽器連接到后端服務。
核心功能:
- 10種語言的語言客戶端庫
- 高效的線路和簡單的服務定義框架
- 基於http / 2傳輸的雙向流式傳輸
- 可插入的身份驗證,跟蹤,負載平衡和健康檢查
主要使用場景:
- 在微服務式架構中有效地連接多點服務
- 將移動設備,瀏覽器客戶端連接到后端服務
- 生成高效的客戶端庫
可以參考gRPC官方文檔 https://grpc.io/docs/
官方支持的語言一覽:
RPC的框架還有 facebook的Thrift、阿里的Dubbo、rpcx等,可以自行百度查看資料。提供2篇關於rpc框架性能測試的文章
分布式RPC框架性能大比拼 http://colobu.com/2016/09/05/benchmarks-of-popular-rpc-frameworks/
開源RPC(gRPC/Thrift)框架性能評測 https://www.cnblogs.com/softidea/p/7232035.html
一、檢查golang的安裝環境
https://golang.org/dl/ 需要牆,或者在這里下載 https://pan.baidu.com/s/12tTmrVIel6sfeBInpt9lQA 最新版本1.10 下載msi安裝即可
window下go version 驗證安裝
二、安裝、配置ProtoBuff
- 下載protoc-3.5.1-win32.zip 解壓,配置環境變量 https://github.com/google/protobuf/releases
- window下驗證安裝
- 安裝Go語言的protoc插件
go get -u github.com/golang/protobuf/protoc-gen-go
會在GOPATH下Bin目錄編譯生成protoc-gen-go.exe
三、下載gRPC
注意 :官方文檔里的使用 go get -u google.golang.org/grpc 下載,實際情況即使用梯子貌似也下載不了
而且在github里gRPC貌似也已經按照語言來分開了,如圖:
下載最新版本 https://github.com/grpc/grpc-go/releases 最新版本是 grpc-go-1.11.1.zip 解壓到%GOPATH%下google.golang.org/grpc文件夾,如圖:
PS: 不能直接 import github的地址,由於歷史原因(grpc內部代碼還是用的google.golang.org)和golang的包管理機制不健全
四、官方的grpc示例程序
1.server服務端
package main
import (
"log"
"net"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/reflection"
)
const (
port = ":50051"
)
type server struct{}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "hi,你好 " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
如果運行報錯 cannot find package "golang.org/x/text/secure/bidirule"、cannot find package "golang.org/x/text/unicode/norm"
下載代碼 https://github.com/golang/text 復制到%GOPATH%/src/golang.org/x/text文件夾下。
如果運行報錯 cannot find package "google.golang.org/genproto/googleapis/rpc/status"
下載代碼 https://github.com/google/go-genproto 復制到%GOPATH%/src/google.golang.org/genproto文件夾下。
官方的server/client源代碼地址:https://github.com/grpc/grpc-go/tree/master/examples/helloworld
案例server源代碼地址:https://github.com/nickchou/grpc-go
2.client客戶端
grpc客戶端,這里我們重新建一個.Net Core的項目(參考官網的 C#版本)
官方C# 版本的服務器、客戶端代碼:https://github.com/grpc/grpc/tree/master/examples/csharp/helloworld
新做的.Net Core 客戶端,完整代碼:https://github.com/nickchou/grpc-netcore
客戶端代碼如下
using System;
using Grpc.Core;
using Helloworld;
namespace Core.Client
{
class Program
{
static void Main(string[] args)
{
//創建一個長連接的channel(不加密、不安全的)
Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
String user = "zhangsan";
var reply = client.SayHello(new HelloRequest { Name = user });
Console.WriteLine("Greeting: " + reply.Message);
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
1.上圖右側解決方案Greeter類庫中的Helloworld.cs和HelloworldGrpc.cs 是generate_protos.bat 根據protos/helloworld.proto的定義用grpc.tools里的protoc.exe來生成的
2.注意右側解決方案中的Nuget引用,grpc.tools實際也是Nuget引用但是.Net Core不像C#一樣拉組件dll文件在package目錄下,而是一個window賬戶的公共目錄,所以是從公共目錄中復制在解決方案里,方便根據bat批處理來生成文件。
protos/helloworld.proto文件內容如下:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
批處理generate_protos.bat的代碼如下,由於是net core用所以改了部分
setlocal
@rem enter this directory
cd /d %~dp0
set TOOLS_PATH=grpc.tools\1.10.0\tools\windows_x86
%TOOLS_PATH%\protoc.exe -I protos --csharp_out Greeter protos/helloworld.proto --grpc_out Greeter --plugin=protoc-gen-grpc=%TOOLS_PATH%\grpc_csharp_plugin.exe
endlocal
pause
3.執行結果
總結:
1、感覺c#客戶端的還是比較麻煩,如果服務端要給客戶端調用還是建議直接Greeter類庫打包成DLL組件放Nuget給客戶端調用會更方便一些,不要讓調用方去生成grpc調用類
2、golang服務端主要是因為各種包管理配置比較復雜,后面重新整理下更好的golang的包管理方法
3、golang服務端總體代碼來看還是比較簡單方便,grpc雖然不是性能最高的,但是基於http2的應用總體感覺還是足夠了。
4、僅供學習參考用 ,基於golang和c#的server/client 官方源代碼里都有,可自行測試