1、Prometheus

1)介绍

Prometheus是一套开源的监控&报警&时间序列数据库的组合,基于应用的metrics来进行监控的开源工具 。

架构图:



2)下载

https://prometheus.io/download/

3)安装
tar -xvzf  prometheus-2.1.0.linux-amd64.tar.gz
ln -s prometheus-2.1.0.linux-amd64 prometheus
./prometheus --config.file=prometheus.yml &
ps -ef|grep prometheus |grep -v grep

通过指定配置文件prometheus.yml启动Prometheus

配置文件官方说明: https://prometheus.io/docs/prometheus/latest/configuration/configuration/

默认情况下,Prometheus会监控自己本身。

4)prometheus自身metrics

http://192.168.8.101:9090/metrics



太多了,真是没法看,还好有个弱弱的图形页面 (待会整合到Grafana 中就方便看了)

5)prometheus自身graph

http://192.168.8.101:9090/graph





选中某个指标,点击Execute . 超多指标可以查看 …

6)查看prometheus配置



prometheus.yml文件

7)查看监控对象

2、SpringBoot

1)pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>clientmonitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>clientmonitor</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--增加依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2) application.yaml or application.properties
spring:
application:
name: springbootPrometheusGrafana management:
endpoints:
web:
exposure:
include: '*'
metrics:
tags:
application: ${spring.application.name}
spring.application.name=springbootPrometheusGrafana
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
#注意此处使用,访问的时候必须访问/actuator/prometheus,如不配置则访问/prometheus
management.endpoints.web.base-path=/actuator
3)实例化MeterRegistryCustomizer

@SpringBootApplication
public class ClientmonitorApplication { public static void main(String[] args) {
SpringApplication.run(ClientmonitorApplication.class, args);
} @Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
4) 注册Metrics

实现 MeterBinder 接口的 bindTo 方法,将要采集的指标注册到 MeterRegistry

@Component
public class JobMetrics implements MeterBinder {
public Counter job1Counter;
public Counter job2Counter; public Map<String, Double> map; JobMetrics() {
map = new HashMap<>();
} @Override
public void bindTo(MeterRegistry meterRegistry) {
this.job1Counter = Counter.builder("counter_builder_job_counter1")
.tags(new String[]{"name", "tag_job_counter1"})
.description("description-Job counter1 execute count").register(meterRegistry); this.job2Counter = Counter.builder("counter_builder_job_counter2")
.tags(new String[]{"name", "tag_job_counter2"})
.description("description-Job counter2 execute count ").register(meterRegistry); Gauge.builder("gauge_builder_job_gauge", map, x -> x.get("x"))
.tags("name", "tag_job_gauge")
.description("description-Job gauge")
.register(meterRegistry);
} }
5) 定时任务实现counter,gauge数据填充
@Component
@EnableScheduling
public class MyJob { private Integer count1 = 0; private Integer count2 = 0; @Autowired
private JobMetrics jobMetrics; @Async("main")
@Scheduled(fixedDelay = 1000)
public void doSomething() {
count1++;
jobMetrics.job1Counter.increment();
jobMetrics.map.put("x", Double.valueOf(count1));
System.out.println("task1 count:" + count1);
if(count1%2==0){
System.out.println("%5==0");
jobMetrics.map.put("x", Double.valueOf(1));
} } @Async
@Scheduled(fixedDelay = 10000)
public void doSomethingOther() {
count2++;
jobMetrics.job2Counter.increment();
System.out.println("task2 count:" + count2);
}
}
7) Controller 接口方式实现counter,gauge数据填充
@RestController
public class CounterController {
@Autowired
private JobMetrics jobMetrics; @RequestMapping(value = "/counter1", method= RequestMethod.GET)
public void counter1() {
jobMetrics.job2Counter.increment();
} @RequestMapping(value = "/counter2", method= RequestMethod.GET)
public void counter2() {
jobMetrics.job2Counter.increment();
}
@RequestMapping(value = "/gauge", method= RequestMethod.GET)
public void gauge(@RequestParam(value = "x") String x) {
System.out.println("gauge controller x"+x);
jobMetrics.map.put("x",Double.valueOf(x));
}
}
6) prometheus 更改配置文件,接入SpringBoot

增加如下配置

#SpringBoot应用配置
- job_name: 'springbootPrometheusGrafana'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.8.1:8080']

Targets

metrisc_path:/actuator/prometheus

Configuration

Service Discovery:

3、Grafana

1)介绍

Prometheus 的可视化功能比较弱,这里我们来接入Grafana 。

Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。它主要有以下六大特点:

  • 展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式;

  • 数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;

  • 通知提醒:以可视方式定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值时通过Slack、PagerDuty等获得通知;

  • 混合展示:在同一图表中混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源;

  • 注释:使用来自不同数据源的丰富事件注释图表,将鼠标悬停在事件上会显示完整的事件元数据和标记;

  • 过滤器:Ad-hoc过滤器允许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询。

2) 安装
yum localinstall grafana-6.2.1-1.x86_64.rpm
systemctl start grafana-server
#设为开机启动
systemctl enable grafana-server
ps -ef|grep grafana-server |grep -v grep
3)访问

配置文件 /etc/grafana/grafana.ini , 默认3000端口,按需修改

##################### Grafana Configuration Example #####################
#
# Everything has defaults so you only need to uncomment things you want to
# change # possible values : production, development
;app_mode = production # instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty
;instance_name = ${HOSTNAME} #################################### Paths ####################################
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
;data = /var/lib/grafana # Temporary files in `data` directory older than given duration will be removed
;temp_data_lifetime = 24h # Directory where grafana can store logs
;logs = /var/log/grafana # Directory where grafana will automatically scan and look for plugins
;plugins = /var/lib/grafana/plugins # folder that contains provisioning config files that grafana will apply on startup and while running.
;provisioning = conf/provisioning #################################### Server ####################################
[server]
# Protocol (http, https, socket)
;protocol = http # The ip address to bind to, empty will bind to all interfaces
;http_addr = # The http port to use
;http_port = 3000 "/etc/grafana/grafana.ini" 530L, 16841C
;access_key =
;secret_key = [external_image_storage.webdav]
;url =
;public_url =
;username =
;password = [external_image_storage.gcs]
;key_file =
;bucket =
;path = [external_image_storage.azure_blob]
;account_name =
;account_key =
;container_name = [external_image_storage.local]
# does not require any configuration [rendering]
# Options to configure external image rendering server like https://github.com/grafana/grafana-image-renderer
;server_url =
;callback_url = [enterprise]
# Path to a valid Grafana Enterprise license.jwt file
;license_path = [panels]
# If set to true Grafana will allow script tags in text panels. Not recommended as it enable XSS vulnerabilities.
;disable_sanitize_html = false [plugins]
;enable_alpha = false
;app_tls_skip_verify_insecure = false

访问 http://192.168.8.101:3000/login

默认的用户名和密码为 admin/admin

SpringBoot JVM监控 grafana 导入471 dashboard

SpringBoot集成prometheus的更多相关文章

  1. SpringBoot整合Prometheus

    SpringBoot整合Prometheus 一.需求 二.实现步骤 1.引入jar包 2.application.prometheus文件配置 3.查看指标数据 4.接入到 prometheus 中 ...

  2. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

  4. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  5. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  6. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  7. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  8. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  9. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

随机推荐

  1. vue axios异步请求django

    1,配置请求路径 (1),vue中的请求路径要与django视图路径相同. (2),vue中的路由路径也要和django视图路径相同,比如视图路径为127.0.0.1:8000:home/index, ...

  2. 5 java 笔记

    1  建议不要在循环体内修改循环变量的值 2 java语言没有提供goto语句来控制程序的跳转 2 java语言同样也提供了continue和break关键字来控制程序的循环结构 3 java中的标签 ...

  3. vuejs 深度监听

    data: { obj: { a: 123 } }, 监听obj中a属性 watch: { 'obj.a': { handler(newName, oldName) { console.log('ob ...

  4. Caffe测试单独的算子

    最近有一个需求是测试单独算子在CPU.Caffe使用的GPU.cuDNN上的性能,一个是使用caffe的time问题,还有一个是使用单独的test功能. time选项的使用,大家都比较熟悉,单独的te ...

  5. 创建LEANGOO账号

    转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_login.html#toggle-id-2 Leangoo采用SaaS模式运行,通过邮箱 ...

  6. vue防重复点击(指令实现)

    快速点击按钮会重复多次调用接口,防止出现这样的情况 全局定义,方便调用 新建plugins.js export default { install (Vue) { // 防重复点击(指令实现) Vue ...

  7. 第十五章、python中的进程操作-开启多进程

    目录 第十五章.python中的进程操作-开启多进程 一.multprocess模块 二.multprocess.process模块 三.Process()对象方法介绍 四.Process()对象属性 ...

  8. Tomcat 7 简单定制

    Tomcat笔记 安装 wget https://mirrors.huaweicloud.com/apache/tomcat/tomcat-7/v7.0.96/bin/apache-tomcat-7. ...

  9. PAT Basic 1044 火星数字 (20 分)

    火星人是以 进制计数的: 地球人的 被火星人称为 tret. 地球人数字 到 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov ...

  10. SDK更新失败问题解决

    环境:1. win102. Have over fire wall 解决办法:1.启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『A ...