springboot1.x+promethus+grafana打造分布式监控系统

客户端集成prothmethus 端点

目前的环境是:

  1. springboot-1.5.9.RELEASE
  2. promethus 2.18.1
  3. grafana6.0.0

 

加入promethus依赖

 

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
   </dependency>
   <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
   </dependency>

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

   <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
   </dependency>
   <!-- The client -->

   <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
      <version>1.0.3</version>
   </dependency>
   <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-spring-legacy</artifactId>
      <version>1.0.3</version>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
         <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
         </exclusion>
      </exclusions>
   </dependency>
</dependencies>

 

加入配置文件

@Configuration

public class MicrometerConfiguration {



    @Value("${spring.application.name}")

    private String applicationName;



    @Bean

    MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {

        return meterRegistry1 -> {

            meterRegistry.config()

                    .commonTags("application", StringUtils.isEmpty(applicationName)?"sample-applicaton"

                            :applicationName);

        };

    }
}

 

修改yml文件或properties文件

management:

  security:

    enabled: false

spring:

  application:

    name: springboot-sample

启动项目

完成项目启动后,可以在控制台->endpoints->mappings中看到暴露的promethus端点

安装prothmethus

windows 安装prothmethus

下载prothmethus  windows安装包,官网地址

https://prometheus.io/download/ 

目前使用的版本是prometheus-2.18.1.windows-amd64

配置promethus

下载完成安装包后,加压在任何位置,修改prometheus.yml文件主要修改的部分是标红的部分

# my global config

global:

  scrape_interval:     10s # Set the scrape interval to every 15 seconds. Default is every 1 minute.

  evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute.

  scrape_timeout: 10s

  # 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: 'sample-scrape'

    metrics_path: '/prometheus'

    # metrics_path defaults to '/metrics'

    # scheme defaults to 'http'.



    static_configs:

    - targets: ['localhost:8080']

  - job_name: 'dgp-scrape'

    metrics_path: '/crDgp/prometheus'

    # metrics_path defaults to '/metrics'

    # scheme defaults to 'http'.



    static_configs:

    - targets: ['localhost:9090']

 

tips:分别监控了两个项目,一个jobName是sample-scrape,项目地址和端口是localhost:8080 ,暴露的端点是 /prometheus,另一个是dgp-scrape ,地址和端口是 localhost:9090,暴露端点是  /crDgp/promethus

 

启动promethus项目

prometheus.exe --web.listen-address=0.0.0.0:8888 --web.enable-lifecycle

  1. --web.listen-address=0.0.0.0:8888 表示启动在8888端口
  2. --web.enable-lifecycle 表示允许使用managementapi

访问localhost:8888 查看页面

 

查看目标列表  status->targets

 

可以看到那些主机down了,那些是正常的

 

linux docker安装和启动项目

docker run -d --name prometheus -p 8888:9090 -m 500M -v /opt/soft/prometheus/prometheus.yml:/prometheus.yml -v /opt/soft/prometheus/data:/data prom/prometheus --config.file=/prometheus.yml --log.level=info

其他步骤和windows一致,不再赘述

安装grafana

windows安装使用grafana

获取安装包

官网地址:https://grafana.com/get

我使用的版本是: 6.0.0

启动grafana

到bin目录下,执行grafana-server.exe

访问:localhost:3000,默认的用户名和密码都是admin

添加promethus数据源

configuration->add data source ->选择promethus,填写信息并保存

添加统计看板

  1. 看板模板库地址:https://grafana.com/dashboards
  2. 搜索

 

找到

复制id

回到grafana安装看板

+->import

粘贴id并等待

选择promethus数据源

查看看板

linux安装和启动grafana

docker run --name grafana -d -p 3000:3000 -v /opt/soft/grafana:/var/lib/grafana -e "GF_SMTP_ENABLED=true" grafana/grafana

注意: 一定要将/opt/soft/grafana 这个目录授权为777 否则会报没有权限错误,这个目录是为了存储相关配置信息,防止镜像删除后,配置信息也丢失

我们可通过http://ip:3000访问Grafana网页界面(缺省的帐号/密码为admin/admin)

其他步骤和windows一致,不再赘述

答:以下是一个示例的docker-compose.yml文件,用于启动PrometheusGrafana来监控一个或多个应用程序: ``` version: '3' services: prometheus: image: prom/prometheus container_name: prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml command: - '--config.file=/etc/prometheus/prometheus.yml' restart: always grafana: image: grafana/grafana container_name: grafana depends_on: - prometheus ports: - "3000:3000" environment: GF_SERVER_ROOT_URL: http://localhost:3000 GF_SECURITY_ADMIN_USER: admin GF_SECURITY_ADMIN_PASSWORD: admin volumes: - ./grafana-data:/var/lib/grafana restart: always ``` 在该docker-compose.yml文件中,我们创建了两个服务:prometheusgrafanaPrometheus服务使用Prometheus Docker映像,并将端口映射到9090。它还将/prometheus.yml文件作为挂载卷传递给容器。在启动容器时,我们使用命令行参数来指定/config.file=/etc/prometheus/prometheus.yml。 Grafana服务程序使用Grafana Docker映像,并依赖于prometheus服务。Grafana服务映射端口3000,并将以下环境变量传递给容器:GF_SERVER_ROOT_URL:使用localhost:3000 (在该容器中内部)作为grafana的根URL,GF_SECURITY_ADMIN_USER和GF_SECURITY_ADMIN_PASSWORD管理Grafana管理员的凭据。此服务还挂载了grafana-data目录,以便可以在重启容器时保留所有Grafana的设置。 最后,我们定义了restart: always参数以确保容器在退出时自动重新启动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值