自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

.NET Worker Service 部署到 Linux 作為 Systemd Service 運(yùn)行

系統(tǒng) Linux
在本文中,我通過一個實(shí)例詳細(xì)介紹了如何將 .NET Worker Service 部署到 Linux 系統(tǒng)作為 Systemd Service 運(yùn)行,并說明了如何使用 systemctl 命令來管理服務(wù),如何使用 journalctl 命令查看 Systemd 服務(wù)日志。

[[407943]]

本文轉(zhuǎn)載自微信公眾號「技術(shù)譯站」,作者技術(shù)譯站。轉(zhuǎn)載本文請聯(lián)系技術(shù)譯站公眾號。

今天我接著介紹一下如何將 Worker Service 部署到 Linux 上,并作為 Systemd Service 運(yùn)行。

我在本文中要覆蓋的內(nèi)容包含:

  • 作為 Linux 控制臺程序運(yùn)行
  • 作為 Systemd Service 運(yùn)行
  • 開機(jī)自動啟動、查看日志信息

創(chuàng)建項(xiàng)目并發(fā)布

§下載 Worker Service 源碼

我將基于上一篇文章中的 Worker Service 源碼來修改,如果您安裝有 git,可以用下面的命令獲取它:

  1. git clone git@github.com:ITTranslate/WorkerServiceAsWindowsService.git 

然后,使用 Visual Studio Code 打開此項(xiàng)目,構(gòu)建一下,以確保一切正常:

  1. dotnet build 

§刪除用不到的依賴項(xiàng)

刪除上一篇文章中用于 Windows Services 的依賴程序包:

  1. git clone git@github.com:ITTranslate/WorkerServiceAsWindowsService.git 

然后,刪除 Program.cs 中的 .UseWindowsService() 方法調(diào)用。

§修改配置文件

打開配置文件 appsettings.json,將日志文件保存路徑中的 \ 改為 /,其他不用做任何更改。

  1.   "Name""RollingFile"
  2.   "Args": { 
  3.     "pathFormat""Logs/{Hour}.log"
  4.     "outputTemplate""{Timestamp:o} [{Level:u3}] ({MachineName}/{ProcessId}/{ProcessName}/{ThreadId}) {Message}{NewLine}{Exception}" 
  5.   } 
  6. }, 
  7.   "Name""SQLite"
  8.   "Args": { 
  9.     "sqliteDbPath""Logs/log.db"
  10.     "tableName""Logs"
  11.     "maxDatabaseSize": 1, 
  12.     "rollOver"true 
  13.   } 

這是因?yàn)?Windows 中使用反斜杠 \ 來表示目錄,而 Linux 中使用正斜杠 / 來表示目錄。

假如不修改保存路徑,您將會看到日志被保存成如下的尷尬文件名:

  1. 'Logs\2021061715.log' 
  2. 'Logs\log.db' 

§發(fā)布程序

運(yùn)行 dotnet publish 命令將應(yīng)用程序及其依賴項(xiàng)發(fā)布到文件夾。

  1. dotnet publish -c Release -r linux-x64 -o c:\test\workerpub\linux 

這里我使用 -r linux-x64 參數(shù),指定發(fā)布獨(dú)立部署于 Linux 系統(tǒng)的應(yīng)用程序。

命令運(yùn)行完成后,您會在 C:\test\workerpub\linux 文件夾中看到適用于 Linux 系統(tǒng)的可執(zhí)行程序及其所有依賴項(xiàng)。

作為 Linux 控制臺程序運(yùn)行

將文件夾 C:\test\workerpub\linux 下的文件壓縮為 linux.zip。

打開 Xshell 工具,連接到一臺 Linux 測試機(jī)(我的測試機(jī)操作系統(tǒng)為 CentOS 7.3),在測試機(jī)上新建 /srv/Worker 目錄:

  1. mkdir /srv/Worker 

使用 rz 命令將 linux.zip 復(fù)制到測試機(jī),

然后在測試機(jī)上解壓 linux.zip 到 /srv/Worker 目錄:

  1. unzip linux.zip -d /srv/Worker 

為我們的應(yīng)用程序分配可執(zhí)行權(quán)限,并運(yùn)行:

  1. # 分配可執(zhí)行權(quán)限 
  2. chmod +x /srv/Worker/MyService 
  3. # 運(yùn)行 
  4. /srv/Worker/MyService 

按下 Ctrl+C 關(guān)閉應(yīng)用,等待關(guān)閉前必須完成的任務(wù)正常結(jié)束后,應(yīng)用退出。輸入 ls /srv/Worker 命令回車,您會看到在該目錄下多了一個 Logs 目錄,日志文件輸出正常。

作為 Systemd Service 運(yùn)行

§添加 Systemd Service 依賴

為了讓我們的 Worker 監(jiān)聽來自 Systemd 的啟動和停止信號,我們需要添加 Microsoft.Extensions.Hosting.Systemd NuGet 包:

  1. dotnet add package Microsoft.Extensions.Hosting.Systemd 

然后,我們需要修改 Program.cs 中的 CreateHostBuilder 方法,添加 UseSystemd 方法調(diào)用,將宿主(Host)生命周期設(shè)置為 Microsoft.Extensions.Hosting.Systemd.SystemdLifetime,以便應(yīng)用程序可以接收啟動和停止信號,并配置控制臺輸出記錄為 systemd 格式。

  1. public static IHostBuilder CreateHostBuilder(string[] args) => 
  2.     Host.CreateDefaultBuilder(args) 
  3.         .UseSystemd() // Sets the host lifetime to Microsoft.Extensions.Hosting.Systemd.SystemdLifetime... 
  4.         .ConfigureServices((hostContext, services) => 
  5.         { 
  6.             services.AddHostedService<Worker>(); 
  7.         }) 
  8.         .UseSerilog(); //將 Serilog 設(shè)置為日志提供程序 

重新運(yùn)行以下命令將程序發(fā)布到文件夾:

  1. dotnet publish -c Release -r linux-x64 -o c:\test\workerpub\linux 

然后重復(fù)前面的步驟,在 Xshell 中使用 rz 命令將應(yīng)用程序復(fù)制到測試機(jī),并為 /srv/Worker/MyService 文件分配可執(zhí)行權(quán)限。

§配置文件

接下來我們需要創(chuàng)建配置文件,將服務(wù)的有關(guān)信息告知 systemd,以便它知道如何運(yùn)行此服務(wù)。為此,我們需要創(chuàng)建一個 .service 文件,我們將在注冊和運(yùn)行此服務(wù)的 Linux 機(jī)器上使用該文件。

在我們的項(xiàng)目中創(chuàng)建一個名為 MyService.service 的服務(wù)單元配置文件,內(nèi)容如下:

  1. [Unit] 
  2. Description=Long running service/daemon created from .NET worker template 
  3.  
  4. [Service] 
  5. # The systemd service file must be configured with Type=notify to enable notifications. 
  6. Type=notify 
  7. # will set the Current Working Directory (CWD). Worker service will have issues without this setting 
  8. WorkingDirectory=/srv/Worker 
  9. # systemd will run this executable to start the service 
  10. ExecStart=/srv/Worker/MyService 
  11. to query logs using journalctl, set a logical name here   
  12. SyslogIdentifier=MyService 
  13.  
  14. # Use your username to keep things simple. 
  15. # If you pick a different user, make sure dotnet and all permissions are set correctly to run the app 
  16. To update permissions, use 'chown yourusername -R /srv/Worker' to take ownership of the folder and files, 
  17. #       Use 'chmod +x /srv/Worker/MyService' to allow execution of the executable file 
  18. User=yourusername 
  19.  
  20. # This environment variable is necessary when dotnet isn't loaded for the specified user
  21. To figure out this value, run 'env | grep DOTNET_ROOT' when dotnet has been loaded into your shell. 
  22. Environment=DOTNET_ROOT=/usr/share/dotnet/dotnet 
  23.  
  24. # This gives time to MyService to shutdown gracefully. 
  25. TimeoutStopSec=300 
  26.  
  27. [Install] 
  28. WantedBy=multi-user.target 

使用時應(yīng)將 User=yourusername 項(xiàng)中的 yourusername 改為具體的 linux 系統(tǒng)的登錄名。

Systemd 期望所有的配置文件放置在 /etc/systemd/system/ 目錄下,我們打開此目錄,并使用 rz 命令將服務(wù)配置文件復(fù)制到 /etc/systemd/system/MyService.service,

  1. cd /etc/systemd/system/ 
  2.  
  3. rz 

然后執(zhí)行以下命令讓 systemd 重新加載配置文件:

  1. systemctl daemon-reload 

§管理服務(wù)

之后,可以運(yùn)行以下命令來檢查 systemd 是否識別了我們的服務(wù):

  1. systemctl status MyService 

結(jié)果顯示如下:

這表明我們注冊的新服務(wù)被禁用了,可以通過運(yùn)行以下命令來啟動它:

  1. systemctl start MyService 

重新運(yùn)行 systemctl status MyService 命令查看服務(wù)狀態(tài),顯示如下:

停止服務(wù)可以運(yùn)行以下命令:

  1. systemctl stop MyService 

如果您希望該服務(wù)在開機(jī)時自動啟動,那么可以運(yùn)行以下命令:

  1. systemctl enable MyService 

禁用開機(jī)自動啟動,可以運(yùn)行以下命令:

  1. systemctl disable MyService 

查看服務(wù)是否開機(jī)自動啟動,可以運(yùn)行以下命令:

  1. systemctl is-enabled MyService 

§Systemd 服務(wù)日志

命令 journalctl 可以用來查看 systemd 收集的日志。systemd-journald 服務(wù)負(fù)責(zé) systemd 的日志收集,它從內(nèi)核、systemd 服務(wù)和其他源檢索信息。日志的集中收集,有利于對其進(jìn)行檢索查詢。journal 中的日志記錄是結(jié)構(gòu)化和有索引的,因此 journalctl 能夠以各種有用的格式來展現(xiàn)日志信息。

我們可以使用 journalctl 命令來驗(yàn)證應(yīng)用程序是否成功運(yùn)行,因?yàn)樵撁羁梢愿欙@示應(yīng)用程序的輸出信息:

  1. journalctl -u MyService -f 

按 Ctrl-C 退出命令。

當(dāng)我們在程序中調(diào)用 UseSystemd 方法時,會將 Extensions.LogLevel 映射到 Syslog 日志級別:

LogLevel Syslog level systemd name
Trace/Debug 7 debug
Information 6 info
Warning 4 warning
Error 3 err
Critical 2 crit

所以,我們可以使用 journalctl 命令的優(yōu)先級標(biāo)記(priority-flag)-p 來根據(jù)日志級別過濾應(yīng)用程序的輸出信息:

  1. journalctl -p 4 -u MyService -f 

總結(jié)

在本文中,我通過一個實(shí)例詳細(xì)介紹了如何將 .NET Worker Service 部署到 Linux 系統(tǒng)作為 Systemd Service 運(yùn)行,并說明了如何使用 systemctl 命令來管理服務(wù),如何使用 journalctl 命令查看 Systemd 服務(wù)日志。

當(dāng)我們向 HostBuilder 添加了 .UseSystemd() 方法調(diào)用后,編譯出的程序,既可以作為 Linux 控制臺應(yīng)用運(yùn)行,也可以作為 Systemd Service 運(yùn)行。

您可以從 GitHub 下載本文中的源碼。

參考:

https://swimburger.net/blog/dotnet/how-to-run-a-dotnet-core-console-app-as-a-service-using-systemd-on-linux

https://devblogs.microsoft.com/dotnet/net-core-and-systemd/

https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-publish

https://www.freedesktop.org/wiki/Software/systemd/

https://systemd.io/

https://www.linode.com/docs/guides/how-to-use-journalctl/

https://github.com/ITTranslate/WorkerServiceAsWindowsService 前篇文章源碼

 

https://github.com/ITTranslate/WorkerServiceAsSystemdService 本文源碼

 

責(zé)任編輯:武曉燕 來源: 技術(shù)譯站
相關(guān)推薦

2022-04-26 09:16:07

PWA線程生命周期

2023-10-31 15:08:56

WorkBoxServiceWorker

2009-03-25 09:44:46

IBM云計(jì)算智慧地球

2021-03-03 08:18:54

Service組件

2009-11-27 13:45:02

Tomcat for

2010-12-24 09:36:15

部署Silverlig

2009-10-29 09:06:26

VB.NET Web

2009-10-13 11:22:46

VB.NET調(diào)用Web

2009-10-13 10:21:58

VB.NET實(shí)現(xiàn)Web

2009-10-13 09:33:49

VB.NET Web

2009-10-29 11:26:28

VB.NET調(diào)用Web

2009-07-27 09:07:04

Profile SerASP.NET AJA

2009-07-27 17:32:39

Web ServiceASP.NET

2010-03-04 10:50:45

windows ser

2009-07-02 16:36:58

Python程序

2017-10-18 15:28:08

Service WorPWA離線

2021-09-11 15:36:08

AWS云服務(wù)Amazon

2009-12-30 15:50:18

ADO.NET Dat

2021-06-02 08:07:59

LinuxService應(yīng)用

2014-07-28 14:46:40

Service
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號