应该如何托管GRPC服务? [英] How should a GRPC Service be hosted?

查看:122
本文介绍了应该如何托管GRPC服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用链接。现在,我想知道应该如何托管该服务器,以便实现以下目标:

I have created a GRPC Server in C# using the example given at Link. Now I want to figure out as how should I be hosting this server so that I achieve following:


  • 我应该将此服务器设置为控制台应用程序吗?或Windows服务。如果我将其设置为Windows Service,则更新该服务将非常繁琐(这是一个很大的缺点),如果将其设置为控制台应用程序,则仅需要关闭exe即可进行更新。但这带来了错误地关闭同一个产品的代价。还有其他更好的方法吗?

  • 使用IIS不会出现此问题,因为我可以简单地从LB中删除该站点并停止该站点以执行更新,但是由于GRPC不会作为IIS的一部分,我不确定要如何使它正常工作。

欢迎使用任何引用来引用更好的体系结构。

Any references for the better architecture are welcomed.

推荐答案

我们可以使用 Microsoft.Extensions.Hosting pacakge托管一个.net核心控制台应用程序,方法是使用HostBuilder API开始构建gRPC主机并进行设置。

We can use Microsoft.Extensions.Hosting pacakge to host a .net core console application by using the HostBuilder API to start building gRPC host and setting it up.

要运行gRPC服务,我们首先需要在以下位置启动/停止 Grpc.Core.Server 托管服务。托管服务基本上是一段代码,它在主机本身启动时由主机运行,而在主机停止时相同。以下代码实现了 GrpcHostedService 来覆盖 IHostedService 接口:

In order to run the gRPC service, we first need to start/stop Grpc.Core.Server in a hosted service. A hosted service is basically a piece of code that is run by the host when the host itself is started and the same for when it is stopped. The following code implement a GrpcHostedService to override IHostedService interface:

using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Hosting;

namespace Grpc.Host
{
    public class GrpcHostedService: IHostedService
    {
        private Server _server;

        public GrpcHostedService(Server server)
        {
            _server = server;
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            _server.Start();
            return Task.CompletedTask;
        }

        public async Task StopAsync(CancellationToken cancellationToken) => await _server.ShutdownAsync();
    }
}

Program.cs中,使用HostBuilder API开始构建grpc主机并进行设置:

In the Program.cs, use HostBuilder API to start building our grpc host and setting it up:

public class Program
{
    public static async Task Main(string[] args)
    {
        var hostBuilder = new HostBuilder()
             // Add configuration, logging, ...
            .ConfigureServices((hostContext, services) =>
            {
                // Better to use Dependency Injection for GreeterImpl
                Server server = new Server
                {
                    Services = {Greeter.BindService(new GreeterImpl())},
                    Ports = {new ServerPort("localhost", 5000, ServerCredentials.Insecure)}
                };
                services.AddSingleton<Server>(server);
                services.AddSingleton<IHostedService, GrpcHostedService>();
            });

        await hostBuilder.RunConsoleAsync();
    }
}

这样做,通用主机将自动运行StartAsync在我们的托管服务上,该服务随后会在 Server 实例上调用StartAsync,实际上是启动gRPC服务器。

By doing this, the generic host will automatically run StartAsync on our hosted service, which in turn will call StartAsync on the Server instance, essentially start the gRPC server.

当我们使用Control-C关闭主机时,通用主机将自动在托管服务上调用StopAsync,该主机将再次在 Server 实例上调用StopAsync

When we shut down the host with Control-C, the generic host will automatically call StopAsync on our hosted service, which again will call StopAsync on the Server instance which will do some clean up.

对于HostBuilder中的其他配置,您可以看到此博客

For other configuration in HostBuilder, you can see this blog.

这篇关于应该如何托管GRPC服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆