這在prometheus上已經提供了現成的exporter,能夠參考html
https://github.com/prometheus/blackbox_exporterlinux
具體使用和效果這裏還有一個博客git
https://medium.com/the-telegraph-engineering/how-prometheus-and-the-blackbox-exporter-makes-monitoring-microservice-endpoints-easy-and-free-of-a986078912eegithub
以tomcat爲例,建立一個metrics的應用,而後裏面加入一個index.jsp文件,將須要暴露的指標都寫到這個文件中redis
好比數據庫
[root@master metrics]# cat index.jsp # HELP helloworld_ordernumber Number of Order. # TYPE helloworld_ordernumber gauge helloworld_ordernumber 10 # HELP helloworld_orderamount Amount of Order. # TYPE helloworld_orderamount gauge helloworld_orderamount 100
說明以下:tomcat
修改prometheus的配置文件jsp
加入被監控的target微服務
[root@master prometheus-2.7.1.linux-amd64]# cat 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'. static_configs: - targets: ['localhost:9100'] - targets: ['localhost:8080']
啓動,而後打開http://192.168.56.108:9090/targetsthis
獲取和訪問指標