From 8e86291d8c11fef912cbd9d4f6b3a21645e1743a Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 11 Jul 2024 16:48:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Windows=20Service=20?= =?UTF-8?q?=E5=AE=89=E8=A3=9D=20Garnet=20=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GarnetWindowsService.sln | 25 +++++++++++++ .../GarnetWindowsService/GarnetService.cs | 35 +++++++++++++++++++ .../GarnetWindowsService.csproj | 15 ++++++++ .../GarnetWindowsService/Program.cs | 17 +++++++++ .../Properties/launchSettings.json | 12 +++++++ .../appsettings.Development.json | 8 +++++ .../GarnetWindowsService/appsettings.json | 8 +++++ samples/GarnetWindowsService/README.md | 27 ++++++++++++++ 8 files changed, 147 insertions(+) create mode 100644 samples/GarnetWindowsService/GarnetWindowsService.sln create mode 100644 samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs create mode 100644 samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj create mode 100644 samples/GarnetWindowsService/GarnetWindowsService/Program.cs create mode 100644 samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json create mode 100644 samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json create mode 100644 samples/GarnetWindowsService/GarnetWindowsService/appsettings.json create mode 100644 samples/GarnetWindowsService/README.md diff --git a/samples/GarnetWindowsService/GarnetWindowsService.sln b/samples/GarnetWindowsService/GarnetWindowsService.sln new file mode 100644 index 0000000..6a74725 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35027.167 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GarnetWindowsService", "GarnetWindowsService\GarnetWindowsService.csproj", "{AA03C1F8-86B1-446F-B4A0-4A69903351BB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9293CD78-718B-4A38-BE60-4A1C7DF6D3F7} + EndGlobalSection +EndGlobal diff --git a/samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs b/samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs new file mode 100644 index 0000000..596d961 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs @@ -0,0 +1,35 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Garnet; + +namespace GarnetWindowsService +{ + public class GarnetService(ILoggerFactory loggerFactory) : BackgroundService + { + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + if (!stoppingToken.IsCancellationRequested) + { + using var server = new GarnetServer([]); + + ILogger logger = loggerFactory.CreateLogger(); + + logger.LogInformation("Starting Garnet server..."); + + try + { + server.Start(); + } + catch (Exception ex) + { + logger.LogError(ex, "An error occurred while starting the Garnet server."); + } + + logger.LogInformation("Garnet server started."); + + await Task.Delay(Timeout.Infinite, stoppingToken); + } + } + } +} diff --git a/samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj b/samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj new file mode 100644 index 0000000..2775120 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + dotnet-GarnetWindowsService-d4bb1a65-ccaf-4675-9048-996583428873 + + + + + + + + diff --git a/samples/GarnetWindowsService/GarnetWindowsService/Program.cs b/samples/GarnetWindowsService/GarnetWindowsService/Program.cs new file mode 100644 index 0000000..9f5bdb1 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/Program.cs @@ -0,0 +1,17 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using GarnetWindowsService; + +var builder = Host.CreateApplicationBuilder(args); + +builder.Services.AddWindowsService(options => +{ + options.ServiceName = "Garnet Service"; +}); + +builder.Services.AddHostedService(); + +var host = builder.Build(); + +host.Run(); \ No newline at end of file diff --git a/samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json b/samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json new file mode 100644 index 0000000..eb54af3 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "GarnetWindowsService": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/GarnetWindowsService/GarnetWindowsService/appsettings.json b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/GarnetWindowsService/README.md b/samples/GarnetWindowsService/README.md new file mode 100644 index 0000000..eb79af3 --- /dev/null +++ b/samples/GarnetWindowsService/README.md @@ -0,0 +1,27 @@ +# ʹÓà Windows Service °²Ñb Garnet ·Ö²¼Ê½»º´æ + +Áã¶È¿ò¼ÜʹÓÃ΢Èí Garnet Ìæ´ú Redis ·Ö²¼Ê½»º´æ£¬¸ÃʾÀýÑÝʾÈçºÎʹÓà Windows Service ÔÚµ¥»úÉÏ°²×° Garnet ·þÎñ£¬Éú³ÉÓ¦ÓóÌÐòºóʹÓÃÒÔÏÂÃüÁî²Ù×÷·þÎñ¡£ + +## ´´½¨·þÎñ + +```shell +sc.exe create GarnetService binpath="C:\GarnetWindowsService.exe" start= auto +``` + +## Æô¶¯·þÎñ + +```shell +sc.exe start GarnetService +``` + +## Í£Ö¹·þÎñ + +```shell +sc.exe stop GarnetService +``` + +## жÔØ·þÎñ + +```shell +sc.exe delete GarnetService +``` \ No newline at end of file