# **USE FILE-BASED SERVICE DISCOVERY TO DISCOVER SCRAPE TARGETS**
Prometheus提供了多種[服務發現選項](https://github.com/prometheus/prometheus/tree/master/discovery)來發現抓取目標,包括[Kubernetes](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#%3Ckubernetes_sd_config),[Consul](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#%3Cconsul_sd_config)等。如果您需要使用當前不支持的服務發現系統,Prometheus[基于文件的服務發現](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#%3Cfile_sd_config)機制可能會最好地滿足您的用例,該機制使你能夠在JSON文件中列出抓取目標(以及這些目標相關的元數據)。
該指南中,我們將會
* 在本地安裝、運行一個Prometheus [Node Exporter](https://prometheus.io/docs/guides/node-exporter)
* 創建一個`targets.json`文件,用以為Node Exporter指定主機和端口信息。
* 安裝并運行Prometheus實例,該實例配置使用targets.json文件來發現Node exporter。
## **安裝運行Node Exporter**
查看`使用Node Exporter來監控Linux宿主機metrics`指南的[這部分](https://prometheus.io/docs/guides/node-exporter#installing-and-running-the-node-exporter)。該Node Exporter運行在9100端口。通過請求確認該Node Exporter確實在暴露metrics:
~~~
curl http://localhost:9100/metrics
~~~
metrics看上去像這樣:
~~~
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
...
~~~
## **安裝、配置、運行Prometheus**
下載、解壓、安裝Prometheus。
解壓的目錄里包含配置文件`prometheus.yml`。替換配置文件中相同的部分如下:
~~~
scrape_configs:
- job_name: 'node'
file_sd_configs:
- files:
- 'targets.json'
~~~
該配置文件指定了一個名稱為`node`的job,該job從targets.json文件中檢索Node Exporter實例的主機和端口信息。
現在創建一個targets.json文件,同時添加內容如下:
~~~
[
{
"labels": {
"job": "node"
},
"targets": [
"localhost:9100"
]
}
]
~~~
該配置指定了一個名稱為`node`的job,其target為:`localhost:9100`。
現在你可以開啟Prometheus:
~~~
./prometheus
~~~
如果Prometheus啟動成功,你能夠看到一行日志如下:
~~~
level=info ts=2018-08-13T20:39:24.905651509Z caller=main.go:500 msg="Server is ready to receive web requests."
~~~
## **探索服務發現的metrics**
- 介紹(Introduction)
- 概覽(Overview)
- First steps
- 方案比較
- FAQ
- Roadmap
- Media
- 術語表(Glossary)
- 概念(Concepts)
- 數據模型(Data model)
- 指標類型(Metric types)
- 作業和實例(Jobs and instances)
- Prometheus
- Getting started
- 安裝
- 配置
- 配置
- 記錄規則(Recording Rules)
- 報警規則(Alerting Rules)
- 模版示例
- 模版參考
- Rules的單元測試
- Querying
- Basics
- Operators
- Functions
- Examples
- HTTP API
- 存儲(Storage)
- 聯邦(Federation)
- 管理API(Management API)
- 遷移(Migration)
- API穩定性
- 可視化(Virsualization)
- Instrumenting
- 客戶端庫
- 開發客戶端庫
- 推送metrics
- exporters & 集成
- 開發exporters
- 格式一覽(Exposition formats)
- Operating
- 安全
- 集成
- 報警(Alerting)
- 報警概覽
- Alertmanager
- 配置
- 客戶端
- 通知模版參考
- 通知模版樣例
- 管理API
- Best Practices
- Metric & label 名稱
- Instrumentation
- console & dashboard
- Histogram & summary
- 報警
- 記錄規則(Recording rules)
- 何時使用Pushgateway
- Remote write tuning
- Guides
- 使用cAdvisor監控Docker容器
- 使用基于文件的服務發現來發現抓取target
- Basic auth
- 使用node exporter來監控Linux宿主機metrics
- Instrumenting一個Go應用程序
- TLS加密