其他
Spring Boot + Prometheus + Grafana 打造可视化监控一条龙
来源:https://www.cnblogs.com/2YSP/p/12827487.html
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
@EnableSpringBootMetricsCollector
@SpringBootApplication
public class MonitorDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorDemoApplication.class, args);
}
}
port: 8848
spring:
application:
name: monitor-demo
security:
user:
name: admin
password: 1234
basic:
enabled: true
# 安全路径列表,逗号分隔,此处只针对/admin路径进行认证
path: /admin
# actuator暴露接口的前缀
management:
context-path: /admin
# actuator暴露接口使用的端口,为了和api接口使用的端口进行分离
port: 8888
security:
enabled: true
roles: SUPERUSER
@RestController
public class TestController {
public static final Map<String, Object> map = new ConcurrentHashMap<>();
@RequestMapping("")
public String testHeapUsed() {
for (int i = 0; i < 10000000; i++) {
map.put(i + "", new Object());
}
return "ok";
}
}
注意:由于开启了安全认证,所以访问这个URL的需要提示输入账号/密码,如果提示404请检查下你的请求地址是否正确,如果不设置management.context-path则默认地址是http://ip:port/prometheus
下载地址:https://prometheus.io/download/
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
# - job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
# static_configs:
# - targets: ['localhost:9090']
- job_name: 'monitor-demo'
scrape_interval: 5s # 刮取的时间间隔
scrape_timeout: 5s
metrics_path: /admin/prometheus
scheme: http
basic_auth: #认证信息
username: admin
password: 1234
static_configs:
- targets:
- 127.0.0.1:8888 #此处填写 Spring Boot 应用的 IP + 端口号
下载地址:https://grafana.com/grafana/download
[smtp]
enabled = true
host = smtp.exmail.qq.com:465
user = xxxx@ininin.com
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = XXX
cert_file =
key_file =
skip_verify = true
from_address = xxxx@ininin.com
from_name = Grafana
ehlo_identity = ininin.com
Evaluate every
For
Conditions
If no data or all values are null
If execution error or timeout
这套监控功能还是挺强大的,就是Prometheus的表达式有点多。
文档:
https://prometheus.io/docs/introduction/first_steps/
https://grafana.com/docs/grafana/latest/
https://github.com/2YSP/monitor-demo
END
往期精彩推荐几款 XShell 的替代品
Spring Boot 使用 Sa-Token-Quick-Login 插件实现快速登录认证
Spring Boot 接口数据加解密技巧,so easy!
Spring Boot 版的低代码开发平台
关注后端面试那些事,回复【2022面经】
获取最新大厂Java面经