Spring Boot 实现应用监控和报警_第1页
Spring Boot 实现应用监控和报警_第2页
Spring Boot 实现应用监控和报警_第3页
Spring Boot 实现应用监控和报警_第4页
Spring Boot 实现应用监控和报警_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SpringBoot实现应用监控和报警SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一。它们三者之间的关系大概如下图:01开发SpringBoot应用首先,创建一个SpringBoot项目,pom文件如下:<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-actuatorartifactId>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-webartifactId>

dependency>

<dependency>

<groupId>jectlombokgroupId>

<artifactId>lombokartifactId>

<optional>trueoptional>

dependency>

<dependency>

<groupId>metheusgroupId>

<artifactId>simpleclient_spring_bootartifactId>

<version>0.8.1version>

dependency>

<dependency>

<groupId>org.springframework.bootgroupId>

<artifactId>spring-boot-starter-securityartifactId>

dependency>注意:

这里的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因为最新的simpleclient_spring_boot只支持1.5.X,不确定2.X版本的能否支持。MonitorDemoApplication启动类增加注解:@EnablePrometheusEndpoint

@EnableSpringBootMetricsCollector

@SpringBootApplication

public

class

MonitorDemoApplication

{

public

static

void

main(String[]args)

{

SpringApplication.run(MonitorDemoApplication.class,args);

}

}配置文件application.yml:server:

port:

8848

spring:

application:

name:monitor-demo

security:

user:

name:admin

password:

1234

basic:

enabled:true

#安全路径列表,逗号分隔,此处只针对/admin路径进行认证

path:/admin

#actuator暴露接口的前缀

management:

context-path:/admin

#actuator暴露接口使用的端口,为了和api接口使用的端口进行分离

port:

8888

security:

enabled:true

roles:SUPERUSER测试代码TestController:@RequestMapping("/heap/test")

@RestController

public

class

TestController{

public

static

finalMap<String,

Object>map=

new

ConcurrentHashMap<>();

@RequestMapping("")

public

String

testHeapUsed(){

for

(inti=

0;i<

10000000;i++){

map.put(i+

"",

new

Object());

}

return

"ok";

}

}这里的逻辑就是在请求这个接口后,创建大量对象保存到map中增加堆内存使用量,方便后面测试邮件报警。启动项目后,可以在IDEA中看到有很多Endpoints,如图:开始我的IDEA是不显示这个Endpoints,后来发现是我使用的idea版本太老了,还是2017.1的,而这个需要idea2017.2版本以上才能看到。

后来只好重新下载安装,弄了好久。启动完毕,访问http://localhost:8888/admin/prometheus就可以看到服务暴露的那些监控指标了。注意:由于开启了安全认证,所以访问这个URL的需要提示输入账号/密码,如果提示404请检查下你的请求地址是否正确,如果不设置management.context-path则默认地址是http://ip:port/prometheus02

安装Prometheus下载地址点击这里https://prometheus.io/download/,本文下载的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。解压后修改prometheus.yml文件,配置数据采集的目标信息。scrape_configs:

#Thejobnameisaddedasalabel

`job=`

toanytimeseriesscrapedfromthisconfig.

#-

job_name:

'prometheus'

#metrics_pathdefaultsto

'/metrics'

#schemedefaultsto

'http'.

#

static_configs:

#-

targets:['localhost:9090']

-

job_name:

'monitor-demo'

scrape_interval:

5s

#刮取的时间间隔

scrape_timeout:

5s

metrics_path:/admin/prometheus

scheme:http

basic_auth:#认证信息

username:admin

password:

1234

static_configs:

-

targets:

-

:8888

#此处填写

SpringBoot应用的IP+端口号更多配置信息请查看官方文档。现在可以启动Prometheus了,命令行输入:prometheus.exe--config.file=prometheus.yml,访问http://localhost:9090/targets,查看SpringBoot采集状态是否正常。03安装Grafana下载地址点击这里:/grafana/download,本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。搜索顶级架构师公众号,回复“手册”,送你一份Java面试题宝典解压后运行bin目录下的grafana-server.exe启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin。现在开始创建自己的可视化监控面板。1.设置数据源2.创建一个Dashboard3.填写采集的指标点注意:

这里的指标点不能随便填,必须是已有的可以在Prometheus看到。4.选择图表样式5.填写标题描述最后点击右上角的保存,输入Dashboad的名称即可。Tips:

这里的图表布局是可以用鼠标拖动的04添加邮件报警在实际项目中当监控的某的个指标超过阈值(比如CPU使用率过高),希望监控系统自动通过短信、钉钉和邮件等方式报警及时通知运维人员,Grafana就支持该功能。第一步:

点击[Alerting]——>[Notificationchannels]添加通知通道这里的Type有很多选项,包括webhook、钉钉等,这里以邮件为例。第二步:

邮箱配置Grafana默认使用conf目录下defaults.ini作为配置文件运行,根据官方的建议我们不要更改defaults.ini而是在同级目录下新建一个配置文件custom.ini。

以腾讯企业邮箱为例,配置如下:####################################SMTP/Emailing#####################

[smtp]

enabled

=

true

host

=:465

user

=xxxx@

#Ifthepasswordcontains#or;youhavetowrapitwithtriplequotes.Ex"""#password;"""

password

=XXX

cert_file

=

key_file

=

skip_verify

=

true

from_address

=xxxx@

from_name

=Grafana

ehlo_identity

=然后需要重启Grafana,命令grafana-server.exe-config=E:\file\grafana-6.3.3\conf\custom.ini第三步:

为指标添加alert配置预警规则配置通知方式和信息Evaluateevery表示检测评率,这里为了测试效果,改为1秒For如果警报规则配置了For,并且查询违反了配置的阈值,那么它将首先从OK变为Pending。从OK到PendingGrafana不会发送任何通知。一旦警报规则的触发时间超过持续时间,它将更改为Alerting并发送警报通知。Conditionswhen表示什么时间,of表示条件,isabove表示触发值,同时,设置了isabove后会有一条红线。Ifnodataorallvaluesarenull如果没有数据或所有值都为空,这里选择触发报警Ifexecutionerrorortimeout如果执行错误或超时,这里选择触发报警注意:

下一次触发,比如10秒后,它不会再次触发,防止报警风暴产生!第

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论