其他分享
首页 > 其他分享> > net core承载服务

net core承载服务

作者:互联网

 

 

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Extensions.Configuration;

namespace HelloWorld
{
    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}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
        };
    }

    public interface IProcessorMetricsCollector
    {
        int GetProcessor();
    }
    public interface IMemoryMetricsCollector
    {
        int GetMemory();
    }
    public interface INetWorkMetricsCollector
    {
        int GetNetWork();
    }
    public interface IDelieveMetricsCollector
    {
        Task Delieve(PerformanceMetrics create);
    }

    public class PerformanceMetricsCollector1 :
        IProcessorMetricsCollector,
        IMemoryMetricsCollector,
        INetWorkMetricsCollector
    {
        public static readonly Random _random = new Random();
        public int GetNetWork()
        {
            return _random.Next(10, 100);
        }

        public int GetMemory()
        {
            return _random.Next(10, 100)*1024*1024;
        }

        public int GetProcessor()
        {
            return _random.Next(100, 400);
        }
    }
    public class PerformanceMetricsDelieve : IDelieveMetricsCollector
    {
        public Task Delieve(PerformanceMetrics create)
        {
            Console.WriteLine($"{DateTimeOffset.Now}:{create}");
            return Task.CompletedTask;
        }
    }

    public class MetricsCollectionOptions
    { 
        public TimeSpan CaptureInterval { get; set; }
        public TransportType TransportType { get; set; }
        public Endpoint Endpoint { get; set; }

    }

    public enum TransportType { 
        Tcp,
        Http,
        Udp
    }
    public class Endpoint
    {
        public string Host { get; set; }
        public int Port { get; set; }
        public override string ToString() => $"{Host}:{Port}";
    }

    public sealed class PerformanceMetricsCollector : IHostedService
    {



        //private readonly IProcessorMetricsCollector _processorMetricsCollector;
        //private readonly IMemoryMetricsCollector _memoryMetricsCollector;
        //private readonly INetWorkMetricsCollector _netWorkMetricsCollector;
        //private readonly IDelieveMetricsCollector _delieveMetricsCollector;

        //public PerformanceMetricsCollector(IProcessorMetricsCollector processorMetricsCollector,
        //    IMemoryMetricsCollector memoryMetricsCollector,
        //    INetWorkMetricsCollector netWorkMetricsCollector,
        //    IDelieveMetricsCollector delieveMetricsCollector
        //    )
        //{
        //    _processorMetricsCollector = processorMetricsCollector;
        //    _memoryMetricsCollector = memoryMetricsCollector;
        //    _netWorkMetricsCollector = netWorkMetricsCollector;
        //    _delieveMetricsCollector = delieveMetricsCollector;
        //}

        public Timer _scheduler;
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _scheduler = new Timer(state =>
            {
                //var create = new PerformanceMetrics()
                //{
                //    Processor = _processorMetricsCollector.GetProcessor(),
                //    Memory = _memoryMetricsCollector.GetMemory(),
                //    NetWork = _netWorkMetricsCollector.GetNetWork()
                //};
                //await _delieveMetricsCollector.Delieve(create);
               Console.WriteLine(PerformanceMetrics.Create().ToString());
            }, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5));
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            _scheduler?.Dispose();
            return Task.CompletedTask;
        }
    }

    class Program
    {


        static void Main(string[] args)
        {
            var collector = new PerformanceMetricsCollector1();
            new HostBuilder()
                .ConfigureAppConfiguration(configBuilder => {
                    configBuilder.AddJsonFile("");
                })
                .ConfigureServices(services =>
                {
                    services.AddSingleton<IProcessorMetricsCollector>(collector);//, PerformanceMetricsCollector1>();
                    services.AddSingleton<IMemoryMetricsCollector>(collector);//, PerformanceMetricsCollector1>();
                    services.AddSingleton<INetWorkMetricsCollector>(collector);//, PerformanceMetricsCollector1>();
                    services.AddSingleton<IDelieveMetricsCollector, PerformanceMetricsDelieve>();
                     services.AddHostedService<PerformanceMetricsCollector>();// .AddSingleton<IHostedService, PerformanceMetricsCollector>();
                })
                .Build()
                .Run();
        }
    }

}

承载服务是在依赖注入基础上的。

标签:core,承载,set,1024,random,get,using,net,public
来源: https://www.cnblogs.com/Insist-Y/p/16241030.html