tangjianft 2020-03-05
jvm:
maven添加dependence
<!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-registry-prometheus --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.3.5</version> </dependency>
编辑springboot项目的yml文件
yml配置参考https://blog.csdn.net/u014401141/article/details/84784422
server: port: 8085 spring: #for monitor application: name: mall_prometheus management: #TODO endpoint 解释查询 endpoints: web: exposure: #include: "*" include: info, health, beans, env, metrics, mappings, scheduledtasks, sessions, threaddump, docs, logfile, jolokia, prometheus base-path: /actuator #默认该路径,不更改可不用配置 #cors跨域支持 cors: allowed-origins: http://example.com allowed-methods: GET,PUT,POST,DELETE prometheus: id: springmetrics endpoint: beans: cache: time-to-live: 10s #端点缓存响应的时间量 health: show-details: always #详细信息显示给所有用户 server: port: 8001 #默认8888 #address: 127.0.0.1 #配置此项表示不允许远程连接 #monitor metrics: export: datadog: application-key: ${spring.application.name} web: server: auto-time-requests: true
配置prometheus.yml
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. evaluation_interval: 15s # Evaluate rules every 15 seconds. scrape_configs: - job_name: prometheus static_configs: - targets: [‘localhost:9090‘] labels: instance: prometheus - job_name: linux static_configs: - targets: [‘47.112.188.174:9100‘] labels: instance: node - job_name: ‘spring‘ metrics_path: ‘/actuator/prometheus‘ static_configs: - targets: [‘47.112.188.174:8001‘]
docker 启动服务时,开放8001端口
# 9100是exporter的端口 docker run -p 8001:8001 -p 8085:8085 --name mall-portal --link mall-mysql:db --link mall-redis:redis --link mongo:mongo --link rabbitmq:rabbit -v /etc/localtime:/etc/localtime -v/usr/local/dockerdata/mall-project/mall-port/logs:/var/logs -d mall/mall-portal:1.0-SNAPSHOT
启动prometheus时加载配置
docker run --name prometheus -d -p 9090:9090 --privileged=true -v /usr/local/dockerdata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
grafana添加对应dashboard:4701
https://grafana.com/grafana/dashboards/4701
mysql
启动mysql的时候
数据库内执行:
use mall; GRANT REPLICATION CLIENT, PROCESS ON *.* to ‘exporter‘@‘%‘ identified by ‘exporter‘; GRANT SELECT ON performance_schema.* TO ‘exporter‘@‘%‘; flush privileges;
docker 启动 mysqld_exporter
docker run -d --restart=always --name mysqld-exporter -p 9104:9104 -e DATA_SOURCE_NAME=‘exporter:(47.112.188.174:3306)/‘ prom/mysqld-exporter
curl 验证一下
curl localhost:9104/metrics process_virtual_memory_bytes 1.16281344e+08# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.# TYPE process_virtual_memory_max_bytes gaugeprocess_virtual_memory_max_bytes -1# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.# TYPE promhttp_metric_handler_requests_in_flight gaugepromhttp_metric_handler_requests_in_flight 1# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.# TYPE promhttp_metric_handler_requests_total counterpromhttp_metric_handler_requests_total{code="200"} 0promhttp_metric_handler_requests_total{code="500"} 0promhttp_metric_handler_requests_total{code="503"} 0
然后修改一下prometheus的yaml。。。。。。具体编写和运行不再赘述。。。
上面这种方式太过繁琐,几乎每次加一个exporter,都要修改prometheus.yml,并重启。可以使用consul来自动搜寻服务。
- job_name: consul consul_sd_configs: - server: ‘47.112.188.174:8500‘ services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*mall.* action: keep