新增learn-kubernetes(https://github.com/yyong-brs/learn-kubernetes)相关文件
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY src/TimeCheck.csproj .
|
||||
RUN dotnet restore
|
||||
|
||||
COPY src /src
|
||||
RUN dotnet publish -c Release -o /out TimeCheck.csproj
|
||||
|
||||
# app image
|
||||
FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine
|
||||
|
||||
COPY src/appsettings.json /config/appsettings.json
|
||||
|
||||
WORKDIR /app
|
||||
ENTRYPOINT ["dotnet", "TimeCheck.dll"]
|
||||
|
||||
COPY --from=builder /out/ .
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Prometheus;
|
||||
using Serilog;
|
||||
|
||||
namespace Kiamol.Ch07.TimeCheck
|
||||
{
|
||||
class Program
|
||||
{
|
||||
private static readonly ManualResetEvent _ResetEvent = new ManualResetEvent(false);
|
||||
private static Counter _CheckCounter;
|
||||
private static string _Version;
|
||||
private static string _Env;
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile("/config/appsettings.json", optional: true)
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
_Version = config["Application:Version"];
|
||||
_Env = config["Application:Environment"];
|
||||
var intervalSeconds = config.GetValue<int>("Timer:IntervalSeconds") * 1000;
|
||||
var metricsEnabled = config.GetValue<bool>("Metrics:Enabled");
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Information()
|
||||
.WriteTo.File("/logs/timecheck.log", shared: true, flushToDiskInterval: TimeSpan.FromSeconds(intervalSeconds))
|
||||
.CreateLogger();
|
||||
|
||||
if (metricsEnabled)
|
||||
{
|
||||
_CheckCounter = Metrics.CreateCounter("timecheck_total", "Number of timechecks");
|
||||
StartMetricServer(config);
|
||||
}
|
||||
|
||||
using (var timer = new System.Timers.Timer(intervalSeconds))
|
||||
{
|
||||
timer.Elapsed += WriteTimeCheck;
|
||||
timer.Enabled = true;
|
||||
_ResetEvent.WaitOne();
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteTimeCheck(Object source, ElapsedEventArgs e)
|
||||
{
|
||||
Log.Information("Environment: {environment}; version: {version}; time check: {timestamp}",
|
||||
_Env, _Version, e.SignalTime.ToString("HH:mm.ss"));
|
||||
|
||||
if (_CheckCounter != null)
|
||||
{
|
||||
_CheckCounter.Inc();
|
||||
}
|
||||
}
|
||||
|
||||
private static void StartMetricServer(IConfiguration config)
|
||||
{
|
||||
var metricsPort = config.GetValue<int>("Metrics:Port");
|
||||
var server = new MetricServer(metricsPort);
|
||||
server.Start();
|
||||
Log.Information("Metrics server listening on port: {metricsPort}", metricsPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="prometheus-net" Version="3.6.0" />
|
||||
<PackageReference Include="Serilog" Version="2.9.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30204.135
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeCheck", "TimeCheck.csproj", "{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {502A678D-1443-4853-A777-00FA08C2BF53}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"Application": {
|
||||
"Version": "1.0",
|
||||
"Environment": "DEV"
|
||||
},
|
||||
"Timer": {
|
||||
"IntervalSeconds": "5"
|
||||
},
|
||||
"Metrics": {
|
||||
"Enabled": false,
|
||||
"Port" : 8080
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user