Prometheus初步搭建和配置(Windows)

本文档介绍了如何在Windows上初步搭建和配置Prometheus,包括启动Prometheus、Grafana,以及设置服务发现机制。Prometheus启动后,可以通过http://localhost:9090/-/reload动态更新配置。Grafana默认地址为http://localhost:3000/。针对Windows系统,可以使用windows_exporter。服务发现可以通过配置文件prometheus.yml实现,如基于Consul、文件或HTTP的服务发现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网:Overview | Prometheus

1.Prometheus启动:

执行prometheus.exe,Prometheus启动时增加--web.enable-lifecycle;可以使用 http://localhost:9090/-/reload 来重新加载;

默认地址为:http://localhost:9090/graph  地址http://localhost:9090/targets可查看当前节点状态

2.Grafanna启动:

执行grafana-server.exe,默认地址为:http://localhost:3000/

3.节点启动:

不同exporter提供不同的监听服务window系统可使用windows_exporter-0.17.1-amd64.exe;

相关下载地址:Download | Prometheus

4.配置Prometheus服务发现:

Prometheus会通过读取配置文件prometheus.yml,来确定其服务发现机制,可同时配置多种服务发现,通过配置服务发现,可以使得再不重启Prometheus服务的情况下更新exporter。

配置文件示例:

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
  - job_name: "node"
    file_sd_configs:
    - refresh_interval: 1m
      files: 
      - "D:\\Prometheus\\prometheus-2.32.1.windows-amd64\\nodeyml\\node*.json"
  - job_name: 'http-other'
    http_sd_configs:
     - url: http://localhost:8080/hosts

  (1)基于网络的服务发现:可以发现通过向consul中特定服务加入节点, 就可以在prometheus动态变动采集的实例地址。(需要额外部署consul)
consul启动命令:consul agent -dev -ui -node=cy

(2)基于文件的服务发现提供了一种更通用的方式来配置静态目标并用作插入自定义服务发现机制的接口。
它读取一组包含零个或多个 <static_config>s 列表的文件。通过磁盘监视检测所有定义文件的更改并立即应用。文件可能以 YAML 或 JSON 格式提供。仅应用导致结构良好的目标组的更改。
文件必须包含静态配置列表,使用以下格式:
JSON json [ { "targets": [ "<host>", ... ], "labels": { "<labelname>": "<labelvalue>", ... } }, ... ]
YAML yaml - targets: [ - '<host>' ] labels: [ <labelname>: <labelvalue> ... ]
作为回退,文件内容也会以指定的刷新间隔定期重新读取。
每个目标__meta_filepath在 重新标记阶段都有一个元标签。它的值设置为从中提取目标的文件路径。
有一个 与此发现机制的集成列表。
# Patterns for files from which target groups are extracted.
files:
  [ - <filename_pattern> ... ]
# Refresh interval to re-read the files.
[ refresh_interval: <duration> | default = 5m ]
其中<filename_pattern>可能是以.json,.yml或结尾的路径.yaml。最后一个路径段可能包含一个*匹配任何字符序列的单个,例如my/path/tg_*.json.

(3) 基于http的服务发现:
配置文件更改,使用url返回:
  - job_name: 'http-other'
    http_sd_configs:
     - url: http://my:8080/api/hosts

示例:

    /**
     * 返回host内容
     * Prometheus使用基于http的服务发现时,会持续访问此接口
     *
     * @return json
     */
    @RequestMapping("/hosts")
    @ResponseBody
    public String getHost() {
        StringBuilder jsonstr = new StringBuilder();
        jsonstr.append("[");
        ResultTO<List<Node>> listResultTO = nodeService.getAll();
        if (listResultTO.isSuccess()) {
            List<Node> nodeList = listResultTO.getData();
            for (int i = 0; i < nodeList.size(); i++) {
                Node node = nodeList.get(i);
                jsonstr.append("{");
                jsonstr.append("\"targets\": [\"" + node.getIp() + ":" + node.getPort() + "\"],");
                jsonstr.append(" \"labels\": {");
                jsonstr.append(" \"id\": \"" + node.getId() + "\",");
                //判断是主机还是数据库
                if (null == node.getDbname() || node.getDbname().isEmpty()) {
                    jsonstr.append(" \"job\": \"os\"");
                } else {
                    jsonstr.append(" \"job\": \"db\"");
                }
                jsonstr.append("}");
                jsonstr.append("}");
                if (i != nodeList.size() - 1) {
                    jsonstr.append(",");
                }
            }
        }
        jsonstr.append("]");
        logger.info(jsonstr.toString());
        return jsonstr.toString();
    }

其他服务发现方式可查看官网,Promethues配置:
Configuration | Prometheus

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值