在 docker 中运行 c# worker 服务有意义吗? [英] Does it make sense to run a c# worker service in docker?

查看:56
本文介绍了在 docker 中运行 c# worker 服务有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 docker 中开发一个多容器应用程序.其中一项服务是一个长时间运行的 C# 控制台应用程序,它基本上对数据库进行一些轮询并将数据发送到 e 服务器.我只是通过添加以下语句来保持服务运行:

I am developing a multi-container app in docker. One of the services is a long-running console application in C# which basically does some polling on a database and sending the data to e server. I just keep the service running by adding this statement:

while(true);

现在我正在考虑将此服务更改为 .NET Core 辅助服务(甚至是 windows 服务,因为我只在 windows 主机上使用 windows 容器).我读过一些关于工作服务的优点的文章,但是当涉及到容器化应用程序时,它们似乎都已经过时了,因为我的容器无论如何都是作为一种后台服务"运行的.(而且我只为每个容器/图像使用一项服务).所以我的问题是:

Now I'm thinking of changing this service to a .NET Core worker service (or even windows service, since I am only using windows containers on windows hosts). I have read some articles about the pros of worker services, but they seem all quite obselete when it comes to a containerized application since my container anyway is running as kind of a "background service" (and i only use one service per container/image). So my question is:

与在 docker 中运行控制台应用相比,在 docker 中运行核心工作者服务有什么好处或缺点吗?

Is there any benefit or drawback when running a core worker service in docker compared to running a console app in docker?

更新:使用工人服务"我指的是 .NET Core 3.x 中可用的新工作器服务模板:https://www.stevejgordon.co.uk/what-are-dotnet-worker-services

Update: With "worker service" I refer to the new worker service template available in .NET Core 3.x: https://www.stevejgordon.co.uk/what-are-dotnet-worker-services

推荐答案

如果您总是要在容器中运行,那么请使用控制台应用程序.我认为作为服务运行没有内在的好处,因为容器在适当的编排(例如 Kubernetes)下应该被视为短暂的.此外,如果您保持简单,即控制台,那么将 .NET Core 3.1.x 应用程序作为 Linux 或 Windows 容器运行时会减少摩擦.

If you are always going to run in a container, then go with a console app. I see no inherent benefit of running as a service since containers, under proper orchestration such as Kubernetes, should be considered ephemeral. Also, you will have less friction in running your .NET Core 3.1.x application as a Linux or Windows container if you keep it simple i.e. console.

此外,我会在您的控制台中使用以下行以确保它与为容器分配的 CPU 配合良好:

Also, I would use the following line in your console to ensure it plays nice with the allocated CPU for the container:

while(true)
{
    Thread.Sleep(1 * 1000);
}

这篇关于在 docker 中运行 c# worker 服务有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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