其他
SpringBoot + Prometheus + Grafana 打造可视化监控一条龙
<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
来源:https://www.cnblogs.com/2YSP/p/12827487.html
END
往期精彩Spring Framework 6.0 正式 GA,新一代框架的开始
Spring Boot + Redis 实现 API 接口防刷限流
Spring Boot 关于日期时间格式化处理方式总结
Spring Boot 最流行的 16 条最佳实践
关注后端面试那些事,回复【2022面经】
获取最新大厂Java面经