liujijiang

Actuator+Prometheus+Grafana监控springboot应用

2020.06.14

Actuator

监控Springboot应用,获取他的各个指标
如果你的Springboot引入了Actuator,那么你的应用就可以暴露一下指定的端口,外界可以通过这些端口拿到应用的运行情况

maven导入jar:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
			<version>2.3.0.RELEASE</version>
		</dependency>

配置:

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    tags:
      application: ${spring.application.name}

添加一个配置项:

@SpringBootApplication
public class Trainingtest1Application {

	public static void main(String[] args) {
		SpringApplication.run(Trainingtest1Application.class, args);
	}

	@Bean
	MeterRegistryCustomizer<MeterRegistry> configurer(
			@Value("${spring.application.name}") String applicationName) {
		return (registry) -> registry.config().commonTags("application", applicationName);
	}

}

启动应用,访问/actuator:
image-0c1f2336683547f08c0232a5a1fd2b5a

访问/actuator/health

image-fb74ee7b4ca14a119cc8310fc0627397

默认只有health和info是可以访问的

配置yml中开启了启用所有端点

endpoints:
    web:
      exposure:
        include: "*"

Prometheus

Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合

定时收集你应用的各项指标

docker中安装:

拉取:

docker pull prom/prometheus

prometheus.yml:配置文件

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
 
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - alertmanager:9093
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
 
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # 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'.
    scrape_interval: 5s
    static_configs:
    - targets: ['127.0.0.1:9090']
  
# 任意写,建议英文,不要包含特殊字符
  - job_name: 'spring'
  # 多久采集一次数据
    scrape_interval: 15s
  # 采集时的超时时间
    scrape_timeout: 10s
  # 采集的路径是啥
    metrics_path: '/actuator/prometheus'
  # 采集服务的地址,设置成上面Spring Boot应用所在服务器的具体地址。
    static_configs:
    - targets: ['应用IP:端口']

可以监控多个项目,每个job_name就是一个项目的名字,然后是配置这个项目

注意:应用IP不能是内网IP,需要是你的服务器的公网IP,一开始我在配置的时候用localhost,然后这个服务的监控就提示连接失败,然后我把应用部署到服务器上,把应用IP:端口换成服务器的IP和应用的端口就好了。

配置文件放一个地方,容器运行时需要映射到这个文件,比如放在/root/prometheus/prometheus.yml

运行:

docker run -d -p 9090:9090 --name prometheus -v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml docker.io/prom/prometheus

别忘了开发服务器9090端口

访问:http://IP:9090

image-d923ed9e331247708bb501d5560c77b9

image-1cd5fa10fd214b658d921c231d6488d6

Grafana

图像化监控

docker中安装:

docker pull grafana/grafana

运行

docker run -d -p 3000:3000 --name grafana docker.io/grafana/grafana

访问:http://服务器IP:3000

image-33ea0194f7264e2d8468956f8a0c3893

点击:Add your first data source

image-a1d69f8f2cda4c3dad5499bbf1d58f04

输入prometheus的url,测试并保存

image-2d085b7360564e95a87129c434715ac9

添加Dashboard:

可以自己配置Dashboard,也可以从Dashboard市场中选择别人已经配置好的Dashboard用:

Dashboard市场

选择并搜索配置好的D按时board

image-239c6f79e56a47f38c35a9405e7b5bc9

记住这个Dashboard的ID:9568

image-d938bf2bd2804786ad61095a1389ce56

来到Grafana:

  • -> 导入:

image-be40b3f4a6f147aab05c510b2d716407

输入ID

image-561f2ca747d24e6ab625b0436231f940

选择你刚才配置的Prometheus

image-30df5913169340659220c541cfe808da

Dashboard面板:

image-c69c42d0473741df97685064800937ba