最近这段时间一直在忙,没时间写博客,负责了一个项目,从前端到后端一直忙,同时还有其余第几个项目的系统架构要处理。前端
去年就开始关注net core了,只是平时写写demo,没用在项目中,正好此次机会就用了net core,具体是何时开始的不太记得了,总之刚开始是用core 1.0开发,而后在开发的时候忽然想到,平时咱们的项目中都没有作过项目的实时监控,为何此次不试试看呢,并且还能知道天天什么时段的流量走向,系统吞吐量等。记得以前去北京总公司的时候,看到java开发部那边有一个大屏幕,实时的显示项目的吞吐量、请求量等信息,感受很是酷,咱们net应该能够能够实现吧。java
抱着这总心态就去找了一些相关资料,也就在项目用了起来。linux
项目部署在windows环境下,Influxdb的介绍这里再也不赘述。git
首先安装InfluxDB时序数据库,地址以下:https://portal.influxdata.com/downloads#influxdb ,这里我就下载Windows Binaries (64-bit),具体的写一下配置文件和安装过程。github
解压后打开influxdb.conf,由于influxdb的默认配置全是针对linux配置的,因此咱们须要修改一下配置文件。数据库
修改下面3个liunx的路径,改成winodws路径以下:json
[meta] # Where the metadata/raft database is stored dir = "influxdb/meta"
[data] # The directory where the TSM storage engine stores TSM files. dir = "influxdb/data"
# The directory where the TSM storage engine stores WAL files. wal-dir = "influxdb/wal"
我将influxdb的文件都放在了influxdb目录下,能够用相对位置,也能够用绝对位置,这个能够根据我的须要修改。windows
这里提一下,Influxdb v1.2.4以后好像网页图形化管理界面就去掉了,1.2.4以前的是有网页图形化管理界面的,influxdb.conf文件以前还有后端
[admin] # Determines whether the admin service is enabled. enabled = true # The default bind address used by the admin service. bind-address = ":8083"
不事后面的版本,conf文件里就没有这个了,因此你们要注意一下。后面版本没办法用网页图形化管理,若是要链接Influxdb的话能够在目录下以cmd运行influx.exe,若是不想全部人均可以访问你的InfluxDB,那么你能够在conf文件里配置认证信息api
# Determines whether HTTP endpoint is enabled.
enabled = true
# The bind address used by the HTTP service.
bind-address = ":8086"
# Determines whether user authentication is enabled over HTTP/HTTPS.
auth-enabled = true
(注意:在没配置好帐号密码前,请先别将上面的:auth-enabled 设置成 true,先在auth-enabled = false 不启用身份验证的时候,建立一个管理员权限的帐号,命令以下:CREATE USER "admin" WITH PASSWORD '123456' WITH ALL PRIVILEGES,建立好帐号后,将auth-enabled 设置成 true,而后启动influxdb,最后使用帐号密码的状况下建立一个名为"AppMetricsDemo"的数据,命令以下:CREATE DATABASE "AppMetricsDemo")
最后cmd运行,进入到你的解压目录,执行命令:
influxd -config influxdb.conf
这里说一下,使用influx.exe登陆时,输入如下命令:influx -host 127.0.0.1 -port 8086 -username "admin" -password "123456",这样就连上InfluxDB了。而后建立数据库:CREATE DATABASE "AppMetricsDemo"。
若是你以为这样比较麻烦,能够安装两个influxDB,一个是V1.2.4版的,一个是最新版的,这是须要修改配置文件的端口,将两个influxDB的端口修改为不同的就好,而后用1.2.4版本的链接最新版的便可,如图,点击右上角的齿轮图标便可出现链接表单,
(安装好influxDB后,记得在influxDB中建立Demo须要的数据库“AppMetricsDemo”)
安装Grafana,下载地址:https://grafana.com/get,咱们解压后进入bin目录,如图:
直接运行grafana-server.exe便可。
Grafana默认会监听3000的端口,因此咱们进入http://localhost:3000,
会让你登录,直接输入本地的管理员账户便可,账户:admin 密码:admin,进入后如图:
安装完成以后,咱们下载相关仪表模版的Json文件。
地址以下:https://grafana.com/dashboards/2125
而后咱们导入咱们的仪表:如图操做便可
添加咱们上面的数据源,如图:
选择Add DataSource,而后操做以下:
这样,咱们就完成了Grafana的安装配置和添加数据源。
因为实际项目中,咱们不可能在服务器上运行个控制台去开启这些服务,因此咱们须要将influxDB和Grafana发布成服务的形式运行在服务器上,这里咱们可使用nssm工具将InfluxDB和Grafana封装成服务运行。
下载地址以下:http://www.nssm.cc/download
解压后进入到对应系统版本文件夹中,里面有个32位和64位的文件,根据本身的实际状况选择,如图:
咱们选择win64,进入文件夹后运行cmd,输入nssm install InfluxDB 运行后出现以下界面:
重点说一下参数这一栏,Argument里输入:-config influxdb.conf,相似上面在cmd中输入Influxd -config influxdb.conf
安装好后运行起来就好,Grafana的安装相似上面的操做,只是Argument这一栏不须要输入任何东西。
.netCore 中使用AppMetrics,新建一个名为Demo 的api,而后编辑右键编辑 Demo.csproj文件,在ItemGroup节点下新增如下Package
<PackageReference Include="App.Metrics" Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.AspNetCore.Endpoints" Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.AspNetCore.Reporting" Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.AspNetCore.Tracking" Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.Extensions.Reporting.InfluxDB" Version="1.2.0" />
<PackageReference Include="App.Metrics.Formatters.Json" Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.Reporting.InfluxDB" Version="2.0.0-alpha" />
保存后,项目就引用了AppMetrics相关类库
而后修改appsettings.json文件。添加以下代码
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } }, "InfluxDB": { "IsOpen": true, "DataBaseName": "AppMetricsDemo", "ConnectionString": "http://10.10.134.109:8086", "username": "admin", "password": "123456", "app": "RepairApp", "env": "stage" } }
ConfigureServices方法,以下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using App.Metrics; namespace Demo { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region Metrics监控配置 string IsOpen = Configuration.GetSection("InfluxDB")["IsOpen"].ToLower(); if (IsOpen == "true") { string database = Configuration.GetSection("InfluxDB")["DataBaseName"]; string InfluxDBConStr = Configuration.GetSection("InfluxDB")["ConnectionString"]; string app = Configuration.GetSection("InfluxDB")["app"]; string env = Configuration.GetSection("InfluxDB")["env"]; string username = Configuration.GetSection("InfluxDB")["username"]; string password = Configuration.GetSection("InfluxDB")["password"]; var uri = new Uri(InfluxDBConStr); var metrics = AppMetrics.CreateDefaultBuilder() .Configuration.Configure( options => { options.AddAppTag(app); options.AddEnvTag(env); }) .Report.ToInfluxDb( options => { options.InfluxDb.BaseUri = uri; options.InfluxDb.Database = database; options.InfluxDb.UserName = username; options.InfluxDb.Password = password; options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30); options.HttpPolicy.FailuresBeforeBackoff = 5; options.HttpPolicy.Timeout = TimeSpan.FromSeconds(10); options.FlushInterval = TimeSpan.FromSeconds(5); }) .Build(); services.AddMetrics(metrics); services.AddMetricsReportScheduler(); services.AddMetricsTrackingMiddleware(); services.AddMetricsEndpoints(); } #endregion services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #region 注入Metrics string IsOpen = Configuration.GetSection("InfluxDB")["IsOpen"].ToLower(); if (IsOpen == "true") { app.UseMetricsAllMiddleware(); // Or to cherry-pick the tracking of interest app.UseMetricsActiveRequestMiddleware(); app.UseMetricsErrorTrackingMiddleware(); app.UseMetricsPostAndPutSizeTrackingMiddleware(); app.UseMetricsRequestTrackingMiddleware(); app.UseMetricsOAuth2TrackingMiddleware(); app.UseMetricsApdexTrackingMiddleware(); app.UseMetricsAllEndpoints(); // Or to cherry-pick endpoint of interest app.UseMetricsEndpoint(); app.UseMetricsTextEndpoint(); app.UseEnvInfoEndpoint(); } #endregion app.UseMvc(); } } }
代码这一块基本上完成了。
接下来运行项目,访问如下,而后看看Grafana中的仪表盘看看
附上demo地址:https://github.com/landonzeng/AppMetricsDemo