如何解决将gRPC服务绑定到aspnetcore中的特定端口
使用aspnetcore 3.1
和Grpc.AspNetCore
nuget软件包,我已经成功地使gRPC服务与this tutorial中所述的标准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");
});
}
这似乎可以满足我的要求,但是我找不到关于它的任何文档,并且它需要在每个服务的代码中进行配置。也许有更好的方法?
解决方法
据我所知,没有其他方法可以为GRPC服务设置特定端口。
grpc服务也在asp.net核心Kestrel服务器上运行,该服务器将侦听端口而不是该服务。
如果您的asp.net核心应用程序仅具有GRPC服务,则可以将Kestrel服务器的侦听端口设置为5001。
如果您具有MVC Web API等多种服务,RequireHost是最好的解决方法,仅允许特定端口访问grpc服务。
如果要提示路由系统输入GRPC服务需要指定的端口,则可以使用以下端口:
routes.MapGrpcService<MyService>().RequireHost("*:5001");
,
这在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的支持。
,您可以尝试使用 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 提供不同的请求管道,则此行为很有用。
,需要配置中间件
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>();
}
});