使用
aspnetcore 3.1
和 Grpc.AspNetCore
nuget 包,我成功地让 gRPC 服务与标准 asp.net 控制器一起成功运行,如本教程中所述。
但是我想将 gRPC 服务绑定到特定端口(例如 5001),如果可能的话最好通过配置而不是代码。这是因为我想限制我的 gRPC 服务的公开方式。
我最接近的是在映射端点时使用
RequireHost
:
// Startup.cs
public void Configure(IApplicationBuilder app)
{
// ...
app.useEndpoints(endpoints =>
{
endpoints.MapGrpcService<MyService>()
.RequireHost("0.0.0.0:5001");
});
}
这似乎符合我的要求,但我找不到任何有关它的文档,并且它需要在每个服务的代码中进行配置。也许有更好的方法?
在
ASP.NET Core 6.0
中,可以在 Properties > launchSettings.json
文件中更改端口。但仅当您从 Visual Studio
或 VS Code
运行服务器时才会考虑此文件。
我尝试使用
.exe
文件直接运行服务器进行测试。服务器使用默认端口运行:"http://localhost:5000;https://localhost:5001"
。
最后,我将它从
appsettings.json
更改为.exe
文件:
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "https://localhost:7005",
"Protocols": "Http1AndHttp2"
},
"gRPC": {
"Url": "http://localhost:5005",
"Protocols": "Http2"
}
}
这适用于 Kestrel(服务器端):
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000);
options.Listen(IPAddress.Loopback, 5005, configure => configure.UseHttps());
});
webBuilder.UseStartup<Startup>();
});
客户端:
var httpHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
using var channel = GrpcChannel.ForAddress("https://localhost:5005", new GrpcChannelOptions { HttpHandler = httpHandler } );
var client = new Greeter.GreeterClient(channel);
注:
var httpHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
当您拥有没有信任链的自签名证书时(主要是在开发时)。
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
用于支持http。
需要配置中间件
app.UseRouting();
app.MapWhen(context => {
return context.Connection.LocalPort == 1000
}, newApp => {
newApp.UseRouting();
newApp.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<Service1>();
}
});
app.MapWhen(context => {
return context.Connection.LocalPort == 2000
}, newApp => {
newApp.UseRouting();
newApp.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<Service2>();
}
});
据我所知,没有其他方法可以为GRPC服务设置特定端口。
grpc服务也运行在asp.net core kestrel服务器上,服务器将监听端口而不是服务。
如果你的asp.net core应用程序只有GRPC服务,你可以将kestrel服务器的监听端口设置为5001。
如果您有多个服务,例如 MVC Web api 或其他服务,RequireHost 是仅允许特定端口访问 grpc 服务的最佳解决方法。
如果您想提示GRPC服务的路由系统需要指定端口,可以使用以下端口:
routes.MapGrpcService<MyService>().RequireHost("*:5001");
仅当请求使用您定义的端口时,您可以尝试使用
UseWhen
方法来使用 MapGrpcService
端点。
var grpcPort = 5001;
app.UseWhen(context => context.Connection.LocalPort == grpcPort,
builder =>
{
builder.UseRouting();
builder.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<MyService>();
});
});
这样做的好处是不必为每项服务重复
.RequireHost("*:5001");
,尽管重复 UseRouting
两次可能会导致奇怪的行为:例如,除非您在 builder.UseAuthentication()
之后输入 builder.UseRouting()
,否则身份验证可能无法正常工作。
但是,如果您希望为 REST 和 gRPC 提供不同的请求管道,则此行为非常有用。
Работает в таком варианте:
Blockquote builder.WebHost.UseUrls("https://localhost:5005");