使用 Metrics.net + influxdb + grafana 搭建项目自动化监控和预警方案

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_25042791/article/details/86673581
这次分享使用Metrics.net + influxdb + grafana 构建项目自动化监控和预警方案。通过执行耗时,定位哪些接口拖累了服务的性能;通过请求频次,设置适当的限流和熔断机制,拦截非法或不合理的请求,保障服务的可用性。本次内容较多,部分详细内容将使用链接方式提供。

动手前,需下准备以下工具:

1、influxdb    下载地址: https://portal.influxdata.com/downloads/#influxdb
2、chronograf   下载地址: https://portal.influxdata.com/downloads/#chronograf   
3、grafana    下载地址: https://grafana.com/get 

正式开始:

1、安装influxdb, 

 

 

    以window环境为例,   下载解压后文件    

打开influxdb.conf,修改配置, 

[meta]
  # Where the metadata/raft database is stored
  dir = "E:/influxdb/meta"

[data]
  # The directory where the TSM storage engine stores TSM files.
  dir = "E:/influxdb/data"

  # The directory where the TSM storage engine stores WAL files.
  wal-dir = "E:/influxdb/wal"

网上有人是需要对[admin]修改,这个因为版本不同,没有[admin],所有对其不做修改。

最后cmd运行,进入到你的解压目录,执行命令:

influxd -config influxdb.conf
为了方便,我一般写个bat文件, 内容 

"E:\influxdb\influxd.exe" -config  "E:\influxdb\influxdb.conf"
运行脚本文件 出现下面的界面 ,infLuxdb安装完成。

 

其它安装可参考 http://blog.51cto.com/oybw88/2107228

 

2、安装chronograf

安装完数据库,怎么查看呢,这时候就需要chronograf了, 解压下载的文件。

 

直接运行chronograf.exe, 正常情况会出现

 

在浏览器中打开http://localhost:8888,出现以下界面说明成功了。

 

 

创建数据库, 也可以使用第一步中的 influx.exe命令操作数据库。

参考https://www.cnblogs.com/shhnwangjian/p/6897216.html?utm_source=itdadao&utm_medium=referral

 

3、安装grafana

解压文件,运行grafana-server.exe

 

出现以下界面

 

浏览器打开http://localhost:3000

 

具体操作 参考https://www.cnblogs.com/imyalost/p/9873641.html

综上,基本准备工作已经完成。

最核心的是在代码中进行埋点采集数据。

实例采用.net下Metrics.Net 组件。

新建项目,引用 组件

 

在程序启动的地方(Global 获取main)加上

Metric.Config.WithReporting(report => report
.WithInfluxDbHttp("127.0.0.1", 8086, "TestDB", "admin", "admin", null, null,
TimeSpan.FromSeconds(1), null, configFunc => configFunc
.WithConverter(new DefaultConverter())
// WithGlobalTags($"env={environment},host={Dns.GetHostName()}"))
.WithFormatter(new DefaultFormatter().
WithLowercase(true))
// .WithWriter(new InfluxdbHttpWriter(configFunc, batchSize))
简单实例:获取访问特定接口的次数

private static int Account = 0;

/// <summary>
/// 访问接口的次数
/// </summary>
[HttpGet]
public int Test(int op)
{
Account++;
Metric.Gauge("Service Account Count", () => Account, new Unit());
return 11;
}
代码可参考:https://www.cnblogs.com/shanyou/p/4004711.html

运行程序,访问接口, 这是去http://localhost:3000 创建

 

进行一些配置,配置可参考:https://blog.csdn.net/zbljz98/article/details/80496851      https://blog.csdn.net/jinzhencs/article/details/76059984

 

最后展现的效果图:

 

整体完成,

这里说的部分是大体,每一个环节都有很多需要去研究,在快速开发的项目中,哪有那么多少时间研究呢,,,,,

按照上述所说的能够完整的搭建起来并使用,具体在使用的时候慢慢学习,由浅到深。

 

 

 

参考来源:

https://blog.csdn.net/sD7O95O/article/details/79547487
https://www.cnblogs.com/shanyou/p/4004711.html
https://blog.csdn.net/u010185262/article/details/53158786
http://www.361way.com/influxdb-user/5291.html
http://www.cnblogs.com/shanyou/p/4004711.html
https://www.cnblogs.com/Leo_wl/p/4471050.html
https://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=2654069598&idx=1&sn=9ad93c53cf610293328cb3941383cdd1&chksm=80dbc70bb7ac4e1d3ef2f106a4c8ff819f7e1a8f11efa55274544d4a459927d6b6fe13b63d26&scene=21#wechat_redirect

谢谢各位的奉献。


————————————————
版权声明:本文为CSDN博主「wiArcher」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_25042791/article/details/86673581

posted @ 2019-11-14 19:49  心冰之海  阅读(253)  评论(0编辑  收藏  举报