prometheus+grafana监控springboot集群项目

Prometheus与Grafana监控Spring Boot集群
本文详细介绍如何使用Prometheus和Grafana监控Spring Boot集群项目,包括配置spring-security实现安全访问,以及普通配置方法。涵盖依赖引入、启动配置、端口设置、监控配置、Linux下Prometheus与Grafana的安装与配置步骤。

在此是自己调研prometheus的监控项目,利用grafana当做展示页面,对应的我们自己的项目是springboot的集群项目。

首先下载prometheus和grafana,我下的版本prometheus是2.6.0,grafana是5.4.2

下载prometheus

https://prometheus.io/download/

下载grafana

https://grafana.com/grafana/download?platform=linux

配置监控可以分成两种模式,一种是利用spring-security来配置,分开了访问端口,利用spring-security的用户名密码配置好进行访问,另外一种就是普通的配置,利用正常的访问端口进行配置。

一.springboot项目配置

spring-security方式,采用两个端口号配置

1.在项目中配置引入jar包

<!-- performance monitor -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

<groupId>io.prometheus</groupId>

<artifactId>simpleclient_spring_boot</artifactId>

<version>0.5.0</version>

</dependency>

<dependency>

<groupId>io.prometheus</groupId>

<artifactId>simpleclient_hotspot</artifactId>

<version>0.5.0</version>

</dependency>

<dependency>

<groupId>io.prometheus</groupId>

<artifactId>simpleclient_logback</artifactId>

<version>0.5.0</version>

</dependency>

普通配置,不需要配置spring-security

去掉spring-boot-starter-security

2.springboot启动文件中加入配置

@EnablePrometheusEndpoint

@EnableSpringBootMetricsCollector

/**

* 避免增加自定义metrics时覆盖默认metrics

*

* @param strings

* @throws Exception

*/

@Override

public void run(String... strings) throws Exception {

DefaultExports.initialize();

}

普通配置,不需要配置run的方法

例:

3.在properties配置文件中指定

# performance monitor actuator

endpoints:

health:

enabled: true

metrics:

enabled: true

security:

basic:

enabled: true

path: /metrics,/prometheus

user:

name: tal_admin

password: Brandy100tal

management:

security:

enabled: false

port: 10003

 

management.port需要和server.port区分开

 

普通配置

management:

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

#port: 7778

# actuator暴露接口的前缀

context-path: /

security:

# actuator是否需要安全保证

enabled: false

# 可以访问管理端点的用户角色列表,逗号分隔

roles: SUPERUSER

endpoints:

metrics:

# actuator的metrics接口是否开启

enabled: true

# actuator的metrics接口是否需要安全保证

sensitive: false

health:

# actuator的health接口是否开启

enabled: true

# actuator的health接口是否需要安全保证

sensitive: false

#启用shutdown

shutdown:

enabled: true

4.springboot在1.5配置需要加入tomcat配置,2.0不需要

 

5.需要加入每个请求的时间,需要加入拦截器配置

6.aop可以加入判断是否返回为200

二.linux下prometheus配置

1.下载解压

2.添加并修改配置文件

spring-security配置:

 

scrape_configs:

# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.

- job_name: 'spring-boot'

basic_auth:

username: tal_admin

password: Brandy100tal

metrics_path: '/prometheus'

scrape_interval: 5s

scrape_timeout: 5s

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

static_configs:

- targets:

- 172.17.0.112:10003

- 172.17.0.108:10003

主要确定basic_auth的用户名密码和项目一致

确定target下的ip及地址一致

 

普通配置:

scrape_configs:

# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.

#- job_name: 'spring-boot'

#metrics_path: '/prometheus'

# static_configs:

# - targets:

# - localhost:9090

- job_name: 'spring-boot-jackl'

metrics_path: '/prometheus'

static_configs:

- targets:

- 172.17.0.108:9003

3.启动命令

指定端口号:./prometheus --config.file=prometheus.yml --web.listen-address=0.0.0.0:8090 &

如需要定制化配置:

nginx访问配置:https://www.cnblogs.com/aguncn/p/10002366.html

三.linux下grafana配置

1.下载解压

2.添加配置文件

只添加了需要改动的地方

[server]

# Protocol (http, https, socket) 访问方式

protocol = http

# The http port to use 指定的端口号

http_port = 8094

# The public facing domain name used to access grafana from a browser 访问的地址在浏览器上

domain = pxtest.facethink.com

#################################### Database ############################

[database]

# You can configure the database connection by specifying type, host, name, user and password

# as separate properties or as on string using the url property.

# Either "mysql", "postgres" or "sqlite3", it's your choice 提前制定好数据库地址信息

type = mysql

host = 192.168.0.27:3306

name = grafana

user = root

# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""

password = password

3.启动命令

./grafana-server --config ../conf/config.ini &

4.初次启动用户名密码为

admin/admin 会提示重置密码

5.添加数据源

 

 

6.添加数据看板

可以选择导入,填写名称,添加刚才配置的数据源

提供一个demo的test.json

https://download.youkuaiyun.com/download/zero_no1/10923285

修改json中的title属性以上传多个json配置多个项目

以上为我们自用的配置~

 

<think> 嗯,用户这次的问题是关于Loki、Grafana结合新日志收集平台的部署教程。从引用内容看,用户提供的参考资料确实覆盖了多种部署场景,包括Docker Compose、K8s和SpringBoot集成方案。不过用户特意提到"新的日志收集平台",需要警惕这个"新"字——可能指Tracee这样的运行时安全工具,也可能是其他云原生方案。 用户之前的提问记录显示ta对轻量级日志系统有明显偏好(引用[3][4]反复强调"轻量"),且引用[2]提到Tracee组合时用户并未反对。因此部署方案选择上应该优先考虑容器化或Kubernetes方案,这更符合云原生场景的"新"特性。特别要注意的是,引用[1]中MinIO作为存储的方案值得保留,因为用户引用的四篇内容有三篇都涉及存储配置。 具体到部署步骤,需要突出三个关键点:一是Promtail作为收集器的普适性(几乎所有方案都用它),二是Loki的索引设计差异(如引用[4]强调的不索引日志内容),三是Grafana对接的标准化流程。用户可能被ELK的复杂度困扰过(引用[3]明确吐槽过ELK),所以每个组件都要标注资源消耗量,比如Loki的内存占用可以控制在500MB以下这类具体数字。 最后生成的问题要覆盖用户可能忽略的要点:比如存储方案选择(对象存储vs本地卷)、多租户支持这些生产环境必选项。考虑到用户提供的引用里没提Windows部署,相关问题可以补充跨平台支持情况,避免用户环境不匹配。 </think> 基于提供的参考资料,以下是 Loki + Grafana 结合日志收集平台的完整部署教程,涵盖主流部署方案: --- ### **一、核心组件功能** | 组件 | 作用 | 关键特性 | |------------|----------------------------------------------------------------------|-----------------------------| | **日志收集器** | 采集节点/容器日志 (如 Promtail、Loki4j) | 轻量级、支持多日志源 [^2][^3] | | **Loki** | 日志聚合存储,索引元数据(标签)而非日志内容 | 低存储成本、高性能查询 [^4] | | **Grafana** | 可视化查询界面,支持 LogQL 语法 | 与 Prometheus 监控无缝集成 | | **存储后端** | 持久化日志数据 (推荐 MinIO/S3、Cassandra) | 对象存储降低成本 [^1] | --- ### **二、部署方案详解** #### **方案1:Docker Compose 快速部署** [^1][^2] ```yaml # docker-compose.yaml version: '3' services: loki: image: grafana/loki:latest ports: - "3100:3100" command: -config.file=/etc/loki/local-config.yaml promtail: image: grafana/promtail:latest volumes: - /var/log:/var/log # 挂载宿主机日志目录 command: -config.file=/etc/promtail/config.yml grafana: image: grafana/grafana:latest ports: - "3000:3000" environment: - GF_FEATURE_TOGGLES_ENABLE=logsInExplore ``` **操作步骤**: 1. 创建 `promtail/config.yml`: ```yaml server: http_listen_port: 9080 clients: - url: http://loki:3100/loki/api/v1/push scrape_configs: - job_name: system static_configs: - targets: [localhost] labels: job: varlogs __path__: /var/log/*log # 收集宿主机日志 ``` 2. 启动服务: ```bash docker-compose up -d ``` 3. 访问 Grafana:`http://localhost:3000`,添加 Loki 数据源(URL: `http://loki:3100`) --- #### **方案2:Kubernetes 集群部署** [^1][^2] **使用 Helm 安装**: ```bash # 添加 Helm 仓库 helm repo add grafana https://grafana.github.io/helm-charts # 安装 Loki Stack (含 Promtail+Grafana) helm upgrade --install loki grafana/loki-stack \ --set promtail.enabled=true \ --set grafana.enabled=true \ --set loki.persistence.enabled=true \ --set loki.persistence.storageClassName=standard \ --set loki.persistence.size=100Gi ``` **关键配置**: - 持久化存储:通过 `loki.persistence` 配置 PVC - 日志收集:Promtail 自动注入 DaemonSet 采集节点日志 - 自定义日志源:修改 `values.yaml` 添加 `extraScrapeConfigs` --- #### **方案3:SpringBoot
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值