前言
简单介绍一下承载。
正文
名称叫做承载,其实就是.net core 定义的一套长期运行的服务的规范。
这个服务可以是web服务,也可以是其他服务,比如tcp,或者一些监控服务。
这里以监控服务为例子:
public class PerformanceMetrics
{
private static readonly Random _random = new Random();
public int Processor { get; set; }
public long Memory { get; set; }
public long Network { get; set; }
public override string ToString()
{
return $"CPU: {Processor*100}% Memory: {Memory /(1024*1024)}M" +
$"Network: {Network/(1024*1024)}M/s";
}
public static PerformanceMetrics Create() => new PerformanceMetrics()
{
Processor = _random.Next(1, 8),
Memory = _random.Next(10, 100) * 1024 * 1024,
Network = _random.Next(10, 100) * 1024 * 1024
};
}
internal class PerformanceMetricsCollector : IHostedService
{
private IDisposable _scheduler;
public Task StartAsync(CancellationToken cancellationToken)
{
_scheduler = new Timer(Callback, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
return Task.CompletedTask;
static void Callback(Object state)
{
Console.WriteLine($"{ DateTimeOffset.UtcNow } { PerformanceMetrics.Create() }");
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_scheduler.Dispose();
return Task.CompletedTask;
}
}
然后服务继承IHostedService, 有一个StartAsync 和 StopAsync, 也就是开始和停止服务。
然后在main里面进行注入:
new HostBuilder()
.ConfigureServices(svcs =>
// svcs.AddSingleton<IHostedService, PerformanceMetricsCollector>()
svcs.AddHostedService<PerformanceMetricsCollector>()
)
.Build()
.Run();
这里就是添加这个服务,然后Build,在run 起来就好了。
之所以我们要使用这个HostBuilder,其实就是为了里面帮我们定义的一些依赖注入。
比如一个长期服务需要用到这几个接口:
internal interface IMemoryMetricsCollector
{
long GetUsage();
}
internal interface INetworkMetricsCollector
{
long GetThroughput();
}
internal interface IProcessorMetricsCollector
{
int GetUsage();
}
internal interface IMetricsDeliverer
{
Task DeliverAsync(PerformanceMetrics counters);
}
他们的实现为:
internal class FakeMetricsCollector : IMemoryMetricsCollector, INetworkMetricsCollector, IProcessorMetricsCollector
{
long INetworkMetricsCollector.GetThroughput()
{
return PerformanceMetrics.Create().Network;
}
long IMemoryMetricsCollector.GetUsage()
{
return PerformanceMetrics.Create().Memory;
}
int IProcessorMetricsCollector.GetUsage()
{
return PerformanceMetrics.Create().Processor;
}
}
public class FackMetricsDeliverer : IMetricsDeliverer
{
Task IMetricsDeliverer.DeliverAsync(PerformanceMetrics counter)
{
Console.WriteLine($"{ DateTimeOffset.UtcNow } { counter }");
return Task.CompletedTask;
}
}
然后你的服务就可以这样写:
internal class PerformanceMetricsCollector2 : IHostedService
{
private IDisposable _scheduler;
private readonly IProcessorMetricsCollector _processorMetricsCollector;
private readonly IMemoryMetricsCollector _memoryMetricsCollector;
private readonly INetworkMetricsCollector _networkMetricsCollector;
private readonly IMetricsDeliverer _metricsDeliverer;
public PerformanceMetricsCollector2(IProcessorMetricsCollector processorMetricsCollector,
IMemoryMetricsCollector memoryMetricsCollector,
INetworkMetricsCollector networkMetricsCollector,
IMetricsDeliverer metricsDeliverer)
{
_processorMetricsCollector = processorMetricsCollector;
_memoryMetricsCollector = memoryMetricsCollector;
_networkMetricsCollector = networkMetricsCollector;
_metricsDeliverer = metricsDeliverer;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_scheduler = new Timer(Callback, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
return Task.CompletedTask;
async void Callback(Object state)
{
var counter = new PerformanceMetrics
{
Processor = _processorMetricsCollector.GetUsage(),
Memory = _memoryMetricsCollector.GetUsage(),
Network = _networkMetricsCollector.GetThroughput()
};
await _metricsDeliverer.DeliverAsync(counter);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_scheduler.Dispose();
return Task.CompletedTask;
}
}
那么在builder的时候,你需要这样写:
var collector = new FakeMetricsCollector();
new HostBuilder()
.ConfigureServices(svcs =>
{
svcs.AddSingleton<IProcessorMetricsCollector>(collector);
svcs.AddSingleton<IMemoryMetricsCollector>(collector);
svcs.AddSingleton<INetworkMetricsCollector>(collector);
svcs.AddSingleton<IMetricsDeliverer, FackMetricsDeliverer>();
svcs.AddHostedService<PerformanceMetricsCollector2>();
})
.Build()
.Run();
这样就可以注入服务了。
有了依赖注入,那么可以做很多事情,比如说配置:
internal class FackMetricsDeliverer3 : IMetricsDeliverer
{
private readonly TransportType _transport;
private readonly EndPoint _deliverTo;
private readonly ILogger _logger;
private readonly Action<ILogger, DateTimeOffset,
PerformanceMetrics, EndPoint, TransportType, Exception> _logForDelivery;
public FackMetricsDeliverer3(IOptions<MetricsCollectionOptions> optionsAccessor,
ILogger<FackMetricsDeliverer3> logger)
{
var options = optionsAccessor.Value;
_transport = options.Transport;
_deliverTo = options.DeliverTo;
_logger = logger;
_logForDelivery = LoggerMessage.Define<DateTimeOffset, PerformanceMetrics,
EndPoint, TransportType>(LogLevel.Information, 0, $"[{0}] Deliver performance counter {1} to {2} via {3}");
}
Task IMetricsDeliverer.DeliverAsync(PerformanceMetrics counters)
{
// Console.WriteLine($"[{DateTimeOffset.Now}] Deliver performance counter {counters} to {_deliverTo} via {_transport}");
_logForDelivery(_logger, DateTimeOffset.UtcNow, counters, _deliverTo, _transport, null);
return Task.CompletedTask;
}
}
然后服务这样写:
internal class PerformanceMetricsCollector3 : IHostedService
{
private IDisposable _scheduler;
private readonly IProcessorMetricsCollector _processorMetricsCollector;
private readonly IMemoryMetricsCollector _memoryMetricsCollector;
private readonly INetworkMetricsCollector _networkMetricsCollector;
private readonly IMetricsDeliverer _metricsDeliverer;
private readonly TimeSpan _captureInterval;
public PerformanceMetricsCollector3(IProcessorMetricsCollector processorMetricsCollector,
IMemoryMetricsCollector memoryMetricsCollector,
INetworkMetricsCollector networkMetricsCollector,
IMetricsDeliverer metricsDeliverer,
IOptions<MetricsCollectionOptions> optionsAccesor)
{
_processorMetricsCollector = processorMetricsCollector;
_memoryMetricsCollector = memoryMetricsCollector;
_networkMetricsCollector = networkMetricsCollector;
_metricsDeliverer = metricsDeliverer;
_captureInterval = optionsAccesor.Value.CaptureInterval;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_scheduler = new Timer(Callback, null, _captureInterval, _captureInterval);
return Task.CompletedTask;
async void Callback(Object state)
{
var counter = new PerformanceMetrics
{
Processor = _processorMetricsCollector.GetUsage(),
Memory = _memoryMetricsCollector.GetUsage(),
Network = _networkMetricsCollector.GetThroughput()
};
await _metricsDeliverer.DeliverAsync(counter);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_scheduler.Dispose();
return Task.CompletedTask;
}
}
然后注入的时候这样:
var collector = new FakeMetricsCollector();
new HostBuilder()
.ConfigureAppConfiguration(builder =>
{
builder.AddJsonFile("appSettings.json");
})
.ConfigureServices((context, svcs) =>
{
svcs.AddSingleton<IProcessorMetricsCollector>(collector);
svcs.AddSingleton<IMemoryMetricsCollector>(collector);
svcs.AddSingleton<INetworkMetricsCollector>(collector);
svcs.AddSingleton<IMetricsDeliverer, FackMetricsDeliverer2>();
svcs.AddHostedService<PerformanceMetricsCollector3>();
svcs.AddOptions().Configure<MetricsCollectionOptions>(context.Configuration.GetSection("MetricsCollection"));
})
.Build()
.Run();
就是把appSettings.json进行注入。
这里有了配置之后,还有一个东西需要注意,那就是环境,比如不同的环境加载不同的配置:
new HostBuilder()
// 设置host 环境
.ConfigureHostConfiguration(builder => builder.AddCommandLine(args))
// 设置app 环境
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile("appSettings.json", optional: false);
Console.WriteLine(context.HostingEnvironment.EnvironmentName);
builder.AddJsonFile(path: $"appSettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);
})
.ConfigureServices((context, svcs) =>
{
svcs.AddSingleton<IProcessorMetricsCollector>(collector);
svcs.AddSingleton<IMemoryMetricsCollector>(collector);
svcs.AddSingleton<INetworkMetricsCollector>(collector);
svcs.AddSingleton<IMetricsDeliverer, FackMetricsDeliverer2>();
svcs.AddHostedService<PerformanceMetricsCollector3>();
svcs.AddOptions().Configure<MetricsCollectionOptions>(context.Configuration.GetSection("MetricsCollection"));
})
.Build()
.Run();
有了不同环境不同的配置后,那么你还需要日志:
internal class FackMetricsDeliverer3 : IMetricsDeliverer
{
private readonly TransportType _transport;
private readonly EndPoint _deliverTo;
private readonly ILogger _logger;
private readonly Action<ILogger, DateTimeOffset,
PerformanceMetrics, EndPoint, TransportType, Exception> _logForDelivery;
public FackMetricsDeliverer3(IOptions<MetricsCollectionOptions> optionsAccessor,
ILogger<FackMetricsDeliverer3> logger)
{
var options = optionsAccessor.Value;
_transport = options.Transport;
_deliverTo = options.DeliverTo;
_logger = logger;
_logForDelivery = LoggerMessage.Define<DateTimeOffset, PerformanceMetrics,
EndPoint, TransportType>(LogLevel.Information, 0, $"[{0}] Deliver performance counter {1} to {2} via {3}");
}
Task IMetricsDeliverer.DeliverAsync(PerformanceMetrics counters)
{
// Console.WriteLine($"[{DateTimeOffset.Now}] Deliver performance counter {counters} to {_deliverTo} via {_transport}");
_logForDelivery(_logger, DateTimeOffset.UtcNow, counters, _deliverTo, _transport, null);
return Task.CompletedTask;
}
}
然后配置的时候这样配:
// about log
var collector = new FakeMetricsCollector();
new HostBuilder()
// 设置host 环境
.ConfigureHostConfiguration(builder => builder.AddCommandLine(args))
// 设置app 环境
.ConfigureAppConfiguration((context, builder) => {
builder.AddJsonFile("appSettings.json", optional: false);
Console.WriteLine(context.HostingEnvironment.EnvironmentName);
builder.AddJsonFile(path: $"appSettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);
})
.ConfigureServices((context, svcs) =>
{
svcs.AddSingleton<IProcessorMetricsCollector>(collector);
svcs.AddSingleton<IMemoryMetricsCollector>(collector);
svcs.AddSingleton<INetworkMetricsCollector>(collector);
svcs.AddSingleton<IMetricsDeliverer, FackMetricsDeliverer3>();
svcs.AddHostedService<PerformanceMetricsCollector3>();
svcs.AddOptions().Configure<MetricsCollectionOptions>(context.Configuration.GetSection("MetricsCollection"));
})
.ConfigureLogging((context , builder)=>
{
builder.AddConfiguration(context.Configuration.GetSection("Logging"));
builder.AddConsole();
})
.Build()
.Run();
这里ConfigureLogging 配置了日志,同样配置了日志的配置。
至此就是一个.net core 定义的一个框架了。
里面有依赖注入、配置、环境、日志。
所以我们依赖这个框架就可以马上专注我们的业务。
下一节看下这种框架是如何设计的。
结
下一节承载的设计。
标签:core,Task,AddSingleton,private,public,重新整理,readonly,net,svcs From: https://www.cnblogs.com/aoximin/p/17560719.html