prometheus+grafana监控springboot集群项目

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在此是自己调研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>嗯,用户问的是有部署Spring Boot项目的经验应该怎么回答。首先,我需要分析用户的需求。可能用户正在准备面试,或者需要描述他们的项目经验,所以想知道如何有效且有结构地表达部署Spring Boot应用的经验。 接下来,我应该考虑用户可能的背景。他们可能是有一定开发经验,但部署方面需要更系统的回答。需要涵盖部署的不同方面,比如打包、环境配置、部署方式、监控等。同时,用户可能希望突出他们的实际操作能力和问题解决能力。 然后,我需要回忆常见的部署方式,比如JAR/WAR部署、Docker容器化、云服务等,这些都是关键点。还要考虑自动化部署工具,比如Jenkins、GitLab CI/CD,这些能展示持续集成和交付的能力。另外,环境配置如数据库、缓存、反向代理的设置也是重要部分,比如Nginx配置SSL、负载均衡等。 监控和日志管理也是部署的重要环节,PrometheusGrafana、ELK等工具的使用经验可以加分。安全措施如防火墙配置、HTTPS、权限控制也需要提及,显示全面的运维能力。 用户可能还希望展示问题解决经验,比如性能调优、内存泄漏处理,这能体现实际应用中的应变能力。需要给出一个结构清晰的回答框架,帮助用户组织他们的经验,使其回答有条理且全面。 最后,要确保语言简洁,使用中文,符合之前的系统指令,比如数学公式的正确格式,但这里可能不太涉及数学内容。需要检查是否有遗漏的关键点,比如不同的部署环境(本地服务器、云平台)、数据库迁移、备份策略等,这些都是用户可能有经验的地方,应包含在回答中。</think>以下是为您整理的SpringBoot项目部署经验回答框架,分步骤说明关键要点: 一、基础部署流程 1. 项目打包 - 使用`mvn package`生成可执行JAR/WAR - 配置`spring-boot-maven-plugin`插件 - 区分开发/生产环境配置文件(Profile机制) 2. 服务器环境搭建 - JDK版本匹配(推荐OpenJDK 11+) - Web服务器配置(Tomcat/Nginx组合方案) - 数据库部署(MySQL集群配置示例): ```bash # 生产环境主从配置 spring.datasource.url=jdbc:mysql://master:3306/db?useSSL=false&serverTimezone=UTC spring.datasource.slave.url=jdbc:mysql://slave:3306/db?useSSL=false&serverTimezone=UTC ``` 二、进阶部署方案 1. 容器化部署 - Dockerfile编写要点: ```dockerfile FROM openjdk:11-jre EXPOSE 8080 ADD target/*.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] ``` - Docker Compose编排数据库与缓存 - 容器网络与存储卷配置 2. 云原生部署 - AWS ECS/EKS部署流程 - 阿里云ACK容器服务配置 - Serverless方案(如AWS Lambda冷启动优化) 三、运维保障体系 1. 监控方案 - Prometheus + Grafana监控指标采集 ```yaml management: endpoints: web: exposure: include: "*" metrics: tags: application: ${spring.application.name} ``` - ELK日志收集架构 - 健康检查端点配置 2. 高可用策略 - Nginx负载均衡配置示例: ```nginx upstream springboot { server 192.168.1.101:8080 weight=5; server 192.168.1.102:8080 weight=5; keepalive 32; } ``` - 数据库读写分离实现 - Redis哨兵模式缓存集群 四、典型问题解决案例(示例) 1. 内存泄漏排查 - 使用jmap生成堆转储文件 - MAT工具分析GC Roots - 优化ThreadLocal使用方式 2. 性能调优实践 - JVM参数调优: ```bash java -Xms2048m -Xmx2048m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -jar app.jar ``` - 数据库连接池配置(HikariCP推荐参数) - 异步处理方案(@Async线程池定制) 五、安全加固措施 1. HTTPS强制跳转配置 2. Spring Security OAuth2集成 3. 定期漏洞扫描(OWASP Dependency-Check) 4. 防火墙规则配置(iptables策略示例) 回答建议:按照STAR法则组织回答: - Situation:项目背景(用户量/日活等数据) - Task:承担的部署职责 - Action:具体实施的技术方案 - Result:达成的效果(可用性/响应时间等指标提升) 可根据实际经验选择2-3个重点技术方案详细说明,配合具体性能指标提升数据(如:部署后API响应时间从1200ms降低至300ms)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值