.Net Core 3.0中的gRPC
Asp.Net Core的gRPC 服务
dotnet new grpc -o GrpcGreeter code -r GrpcGreeter dotnet run
配置gRPC,gRPC 是通过AddGrpc
方法启用的,每个 gRPC 服务通过MapGrpcService
方法添加到路由管道。
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddGrpc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { // Communication with gRPC endpoints must be made through a gRPC client. // To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909 endpoints.MapGrpcService<GreeterService>(); }); } }
配置Kestrel,需要 HTTP/2,通过 HTTPS 进行保护。
gRPC 要求 HTTP/2。 gRPC for ASP.NET Core 验证HttpRequest为HTTP/2
。在大多数现代操作系统上,Kestrel支持 HTTP/2 。 默认情况下,Kestrel 终结点配置为支持 HTTP/1.1 和 HTTP/2 连接。
用于 gRPC 的 Kestrel 终结点应使用 HTTPS 进行保护。 在开发中, https://localhost:5001
当存在 ASP.NET Core 开发证书时,将自动创建 HTTPS 终结点。 不需要配置。
在生产环境中,必须显式配置 HTTPS。 在下面的appsettings示例中,提供了一个使用 HTTPS 保护的 HTTP/2 终结点:
{ "Kestrel": { "Endpoints": { "HttpsDefaultCert": { "Url": "https://localhost:5001", "Protocols": "Http2" } }, "Certificates": { "Default": { "Path": "<path to .pfx file>", "Password": "<certificate password>" } } } }
或者,可以在Program.cs中配置 Kestrel 终结点:
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.ConfigureKestrel(options => { // This endpoint will use HTTP/2 and HTTPS on port 5001. options.Listen(IPAddress.Any, 5001, listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; listenOptions.UseHttps("<path to .pfx file>", "<certificate password>"); }); }); webBuilder.UseStartup<Startup>(); });
如果未使用 HTTPS 配置 HTTP/2 终结点,则终结点的ListenOptions.Protocols必须设置为HttpProtocols.Http2
。 HttpProtocols.Http1AndHttp2
无法使用,因为需要使用 HTTPS 来协商 HTTP/2。 如果没有 HTTPS,则端点的所有连接默认为 HTTP/1.1,并且 gRPC 调用失败。
使用控制台创建Grpc客户端
dotnet new console -o GrpcGreeterClient code -r GrpcGreeterClient
gRPC 客户端项目需要以下包:
dotnet add GrpcGreeterClient.csproj package Grpc.Net.Client
dotnet add GrpcGreeterClient.csproj package Google.Protobuf
dotnet add GrpcGreeterClient.csproj package Grpc.Tools
新建Protos文件夹,将GrpcGreeter的greet.proto文件夹复制到这里,然后编辑GrpcGreeterClient.csproj,添加<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Google.Protobuf" Version="3.9.2" /> <PackageReference Include="Grpc.Net.Client" Version="2.23.2" /> <PackageReference Include="Grpc.Tools" Version="2.24.0"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> </ItemGroup> </Project>