Springboot
pom.xml导入prometheus依赖
<!--prometheus-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.yml
spring:
application:
name: monitor
server:
port: 88
management:
server:
port: 9003
endpoints:
web:
exposure:
include: '*'
endpoint:
metrics:
enabled: true
health:
show-details: always
prometheus:
enabled: true
metrics:
export:
prometheus:
enabled: true
tags:
application: voice-qc-backend
启动类
@SpringBootApplication
public class GrafanaApplication {
public static void main(String[] args) {
SpringApplication.run(GrafanaApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
写个测试接口
@RestController
@RequestMapping("prometheus")
public class PrometheusController {
@GetMapping("hello")
public String hello(){
return "hello";
}
}
访问接口
查看prometheus是否有捕获消息
Prometheus
拉取镜像
docker pull prom/prometheus:v2.40.5
mkdir -p /docker_data/prometheus/config/
vim /docker_data/prometheus/config/prometheus.yml
宿主机创建配置文件
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
# prometheus的IP地址
- targets: ['192.168.1.11:9090']
使用宿主机的配置文件启动容器
docker run -d --network=host --privileged=true --restart=always -v /docker_data/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml --name prometheus-v2.40.5 prom/prometheus:v2.40.5
访问ip:9090
修改配置文件
scrape_configs:
- job_name: "prometheus"
static_configs:
# prometheus的ip:port
- targets: ["192.168.1.11:9090"]
- job_name: "metricsLocalTest"
metrics_path: "/actuator/prometheus"
static_configs:
# springboot项目的ip:port
- targets: ["192.168.1.117:9003"]
重启容器使配置文件生效
docker restart prometheus-v2.40.5
Grafana
拉取镜像
docker pull grafana/grafana:7.3.7
需要指定root用户启动并映射文件到宿主机,如果不用-u root则可能会报错mkdir: can’t create directory ‘/var/lib/grafana/plugins’: Permission denied
docker run -d --network=host --privileged=true --restart=always -u root -v /docker_data/grafana:/var/lib/grafana --name grafana-7.3.7 grafana/grafana:7.3.7
访问ip:3000,用户名和密码都是admin
配置prometheus数据源
选择prometheus,填写prometheus的ip和port
导入4701模板(其它模板也是可以的)
选择prometheus,然后再点击import
查找模板
《Grafana官网查找模板》
如果联网直接使用ID,没有联网就可以选择下载JSON,导入。我们上面就是直接使用ID,现在使用JSON导入。
进入官网搜索模板,如:node exporter,选择含有中文的那个。
选择下载的JSON文件导入,然后再选择prometheus,点击最下方的Load即可。