Prometheus(普罗米修斯):一个开源的监控系统和时间序列数据库(TSDB)
Grafana:监控绘图程序,也称监控可视化面板

一、安装Prometheus

1.安装依赖

yum install -y epel-release go

2.下载与解压

wget https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-amd64.tar.gz
tar -zxvf prometheus-2.35.0.linux-amd64.tar.gz prometheus-2.35.0.linux-amd64/

3.修改prometheus.yml配置

mv prometheus-2.35.0.linux-amd64 /usr/local/prometheus
cd /usr/local/prometheus
vim prometheus.yml

配置如下:

# 全局配置
global:
  scrape_interval: 15s # 默认拉取评论
  evaluation_interval: 15s # 执行规则频率

# 拉取配置
scrape_configs:
  # 拉取任务名
  - job_name: "prometheus"
      # 静态配置目标列表
    static_configs:
      - targets: ["localhost:9090"]
  # MySQL状态实例
  - job_name: "mysql-status"
    static_configs:
      - targets: ["localhost:3306"]
        labels:
          instance: mysql-status

4.配配置开机启动

vim /etc/systemd/system/prometheus.service

配置内容:

[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090

[Install]
WantedBy=multi-user.target

5.开机启动

# 重新加载服务配置文件
systemctl daemon-reload
# 启动prometheus并设置开机自启动
systemctl start prometheus && systemctl enable prometheus

6.验证prometheus是否东成功
浏览器访问:http://localhost:9090/targets
命令行:lsof -i:9090
7.各种prometheus插件
https://prometheus.io/docs/instrumenting/exporters/
node_exporter:用于机器系统收集
mysqld_exporter:用于MySQL服务器数据收集
8.给prometheus添加账号访问
Prometheus通过Nginx加密登陆(二)

二、SpringBoot程序

1.添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
  <version>${springboot.version}</version>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
  <version>${micrometer.version}</version>
</dependency>

2.在application.yml中添加配置

management:
  endpoints:
    web:
      exposure:
        # 暴露哪些端点,*星号为所以
        include: '*'
  endpoint:
    prometheus:
      # 开启prometheus抓取数据
      enabled: true
    shutdown:
      # 端点开启优雅关闭
      enabled: true
    metrics:
      # 开启程序度量信息,比如内存用量和HTTP请求数
      enabled: true
  metrics:
    tags:
      application: ${spring.application.name}

3.添加监控JVM性能指标信息类

@Configuration
public class ApplicationConfig {

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
}

4.修改prometheus服务器上的prometheus.yml文件

scrape_configs:
    - job_name: "blog-monitor"
    scrape_interval: 5s
    metrics_path: "/blog/actuator/prometheus"
    static_configs:
      - targets: ["localhost:19090"]
        labels:
          instance: blog-monitor

5.重启prometheus

systemctl restart prometheus

三、安装Grafana

1.下载与安装

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.5.0-1.x86_64.rpm
sudo yum install grafana-enterprise-8.5.0-1.x86_64.rpm

2.查看grafana安装路径

whereis grafana

3.启动服务

systemctl enable grafana-server
systemctl start grafana-server
最后修改:2022 年 09 月 03 日
如果觉得我的文章对你有用,请随意赞赏