gRPC 是Google發起的一個開源遠端過程呼叫 系統。該系統基於HTTP/2 協定傳輸,使用Protocol Buffers 作為介面描述語言。 其他功能: 認證 雙向流 流控制 超時 最常見的應用場景是: 微服務架構下,多種語言服務之間的高效互動。 將手機服務、瀏覽器連線至後臺 產生高效的使用者端庫-- 維基百科
微軟官網介紹:
gRPC是一種與語言無關的高效能遠端過程呼叫 (RPC) 框架。
gRPC 的主要好處是:
現代、高效能、輕量級的 RPC 框架。
合約優先的 API 開發,預設使用 Protocol Buffers,允許語言無關的實現。
可用於多種語言的工具來生成強型別伺服器和使用者端。
支援使用者端、伺服器和雙向流式呼叫。
通過 Protobuf 二進位制序列化減少網路使用。
這些優勢使 gRPC 非常適合:
效率至關重要的輕量級微服務。
需要多種語言進行開發的多語言系統。
需要處理流請求或響應的對等實時服務。
程式包管理器控制檯新增 Grpc.AspNetCore nuget包參照
Install-Package Grpc.AspNetCore -Version 2.47.0
*
新建一個Grpc及Proto資料夾分別放置 gRPC服務和proto檔案(方便管理)
在proto資料夾下新建一個 user.proto 檔案(vs 檔案模板沒有該字尾名檔案,直接新建任意檔案改檔名即可),在 user.proto檔案中定義gRPC服務方法、入參及返回值
syntax = "proto3";
option csharp_namespace = "GrpcUser";
package UserApi;
service User {
rpc GetUserByOrganizationId(OrganizationUserRequest) returns (OrganizationUserRequestResponse) {}
}
message OrganizationUserRequest {
string organizationid = 1;
}
message OrganizationUserRequestResponse {
string organizationid = 1;
repeated OrganizationUserItemResponse items = 2;
}
message OrganizationUserItemResponse {
string id = 1;
string name =2;
int32 sex = 3;
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.47.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Grpc\" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Proto\user.proto" GrpcServices="Server" Generator="MSBuild:Compile"/>
</ItemGroup>
</Project>
using Grpc.Core;
using GrpcUser;
namespace gRPCServer.Grpc
{
public class UserService : User.UserBase
{
public override Task<OrganizationUserResponse> GetUserByOrganizationId(OrganizationUserRequest request, ServerCallContext context)
{
/*******此處實際業務從持久層獲取資料**********/
var organizationUser = new OrganizationUser(request.Organizationid);
organizationUser.Items = GetUserInfos();
return Task.FromResult(MapToResponse(organizationUser));
}
private List<UserInfo> GetUserInfos()
{
var userInfos = new List<UserInfo>();
userInfos.Add(new UserInfo
{
Id = 1,
Name = "使用者1",
Sex = 0
});
userInfos.Add(new UserInfo
{
Id = 2,
Name = "使用者2",
Sex = 1
});
return userInfos;
}
private OrganizationUserResponse MapToResponse(OrganizationUser organizationUser)
{
var response = new OrganizationUserResponse()
{
Organizationid = organizationUser.OrganizationId
};
organizationUser.Items.ForEach(item => response.Items.Add(new OrganizationUserItemResponse
{
Id = item.Id,
Name = item.Name,
Sex = item.Sex
}));
return response;
}
}
public class OrganizationUser
{
public string? OrganizationId { get; set; }
public List<UserInfo> Items { get; set; }
public OrganizationUser(string organizationId)
{
OrganizationId = organizationId;
}
}
public class UserInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int Sex { get; set; }
}
}
//啟用grpc
builder.Services.AddGrpc();
//對映grpc服務
app.MapGrpcService<UserService>();
builder.WebHost.ConfigureKestrel(
options =>
{
//webApi監聽埠
options.Listen(System.Net.IPAddress.Any, 5157, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1AndHttp2;
});
//grpc監聽埠
options.Listen(System.Net.IPAddress.Any, 5158, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2;
});
}
);
using gRPCServer.Grpc;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(
options =>
{
//webApi監聽埠
options.Listen(System.Net.IPAddress.Any, 5157, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1AndHttp2;
});
//grpc監聽埠
options.Listen(System.Net.IPAddress.Any, 5158, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2;
});
}
);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//啟用grpc
builder.Services.AddGrpc();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//對映grpc服務
app.MapGrpcService<UserService>();
app.UseAuthorization();
app.MapControllers();
app.Run();
Install-Package Grpc.AspNetCore -Version 2.47.0
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Proto\user.proto" GrpcServices="Client" Generator="MSBuild:Compile"/>
</ItemGroup>
</Project>
//注入grpc使用者端
builder.Services.AddGrpcClient<GrpcUser.User.UserClient>(
options =>
{
options.Address = new Uri("http://localhost:5158"); //grpcServer專案設定的grpc服務監聽埠
});
using Microsoft.AspNetCore.Mvc;
using GrpcUser;
namespace gRPCClient.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
private User.UserClient _userClient;
public UserController(User.UserClient userClient)
{
_userClient = userClient;
}
/// <summary>
/// 獲取使用者
/// </summary>
/// <param name="organizationId"></param>
/// <returns></returns>
[HttpGet]
public async Task<OrganizationUserResponse> GetUser(string organizationId)
{
var user = await _userClient.GetUserByOrganizationIdAsync(new OrganizationUserRequest { Organizationid = organizationId });
return user;
}
}
}
先啟動 grpcSever專案,然後再啟動 client專案,在swagger頁面呼叫 GetUser方法即可獲取到grpc介面返回資料,Grpc服務於使用者端呼叫就搭建好了
完整專案結構
grpcui是Go語言編寫的,所以第一步我們先要進行Go環境的搭建。
安裝完成,以管理員身份開啟PowerShell,並修改Go環境變數
go env -w GO111MODULE=on
因為Go 包源在外網,所以我們要設定一些代理,方便安裝
go env -w GOPROXY=https://goproxy.cn,direct
安裝grpcui
go install github.com/fullstorydev/grpcui/cmd/grpcui@latest
安裝完成即可使用命令測試是否安裝成功
grpcui --help
安裝完偵錯工具還需要修改一些我們的grpcService專案,讓專案把grpc服務介面給反射出來,grpcui工具才能獲取到相應介面並以webUi的方式進行偵錯
程式包管理器控制檯新增 Grpc.AspNetCore.Server.Reflection nuget包參照
Install-Package Grpc.AspNetCore.Server.Reflection -Version 2.47.0
專案注入grpc服務反射包服務
builder.Services.AddGrpcReflection();
啟用grpc對映
app.MapGrpcReflectionService();
最終gRPCServer專案的Program 檔案設定如下
using gRPCServer.Grpc;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(
options =>
{
//webApi監聽埠
options.Listen(System.Net.IPAddress.Any, 5157, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1AndHttp2;
});
//grpc監聽埠
options.Listen(System.Net.IPAddress.Any, 5158, listenOptions =>
{
listenOptions.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2;
});
}
);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//啟用grpc
builder.Services.AddGrpc();
//啟用grpc反射
builder.Services.AddGrpcReflection();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//對映grpc服務
app.MapGrpcService<UserService>();
//對映grpc反射服務
app.MapGrpcReflectionService();
app.UseAuthorization();
app.MapControllers();
app.Run();
啟動gRPC專案
PowerShell 執行命令 grpcui -plaintext {grpc服務的ip埠地址}
即可開啟webUi介面偵錯grpc介面
啟動的WebUI偵錯頁面
在此頁面我們就可以像使用Postman偵錯webApi一樣偵錯我們的grpc介面了
參考資料:
微軟gRPC使用教學:https://docs.microsoft.com/en-us/aspnet/core/grpc/?view=aspnetcore-6.0
grpcui測試grpc服務教學:https://docs.microsoft.com/en-us/aspnet/core/grpc/test-tools?view=aspnetcore-6.0
grpcui專案教學:https://github.com/fullstorydev/grpcui