简体   繁体   English

在Linux容器上的dotnet Core中实现消息使用者

[英]Implementing a Message Consumer in dotnet Core on Linux container

I'm looking to implement a service that will be deployed on a Linux kubernetes cluster. 我正在寻求实现将在Linux kubernetes集群上部署的服务。 It's going to consume and process messages from a queue. 它将使用和处理队列中的消息。

My question is, what is the recommended and conventional way of implementing a worker in dotnet core today? 我的问题是,今天在dotnet核心中实现工人的推荐和常规方式是什么? A background task in an ASPNET application seems overkill as I have no requirement for serving web requests. ASPNET应用程序中的后台任务似乎过大,因为我不需要处理Web请求。 A bit of research leads me to think a console app building an IHost and a custom IHostedService would do the job. 一些研究使我认为构建IHost和自定义IHostedService的控制台应用程序可以完成这项工作。 I'm surprised at the absence of examples for this scenario though which got me wondering if I'm missing a more standard way to implement this? 我对这种情况下没有示例感到惊讶,尽管这使我想知道我是否缺少实现这一目标的更标准方法?

Using a 'simple' console app, by adding Console.ReadLine() at the last code line will obviously not work as there's no input stream while running in K8s. 使用“简单”控制台应用程序,通过在最后一个代码行添加Console.ReadLine()显然将不起作用,因为在K8s中运行时没有输入流。

The recommended way to implement this is using a Host Builder. 推荐的实现此方法的方法是使用主机生成器。

WebHost is for web applications, as you mentioned, but there's a generic host builder for all other types of applications that generally do not required web capabilities. 如前所述,WebHost用于Web应用程序,但是对于所有其他通常不需要Web功能的其他类型的应用程序,都有一个通用的主机构建器。

Although at first it might seems a bit of an 'overkill' - it really is not. 尽管起初它似乎有点“过大杀伤力”,但实际上并非如此。 When planning on running on Kubernetes, you must consider other paths other than the happy one. 当计划在Kubernetes上运行时,您必须考虑快乐路径以外的其他路径。 For example, allowing K8s signalling you to terminal (handling SIGTERM) in case of scale down event on that deployment, or node draining - for graceful shutdown - which is supported out-of-the-box when using the HostBuilder. 例如,在该部署发生缩减事件时,允许K8s向您发出信号通知终端(处理SIGTERM),或者在使用HostBuilder时开箱即用地支持节点排空(以正常关闭)。 In addition will you be able to enjoy other built-in features such as auto-reload of settings from files (which works beautifully when mounting ConfigMap to a file) 此外,您还可以享受其他内置功能,例如从文件自动重载设置(将ConfigMap安装到文件时效果很好)

Documentation: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2 文档: https : //docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2

Sample project: https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/2.x/GenericHostSample 示例项目: https : //github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/host/generic-host/samples/2.x/GenericHostSample

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM