容器环境 springcloud gateway grafana prometheus采集集成与问题

容器环境 springcloud gateway grafana prometheus采集集成与问题

大家好,我是烤鸭:
    记录下网关上容器后,监控升级的过程。

原来的方式

grafana 和 prometheus 网上教程很多,就不细写了。

没上容器之前,可以在 prometheus 的配置文件配置具体的ip地址,进行监控数据拉取。

上了容器之后就不行了,每次发布ip都是动态的。

初次接入

prometheus 有个pushgateway的插件,支持被动接受数据的方式。

网上的开源项目:

https://github.com/prometheus/pushgateway

cloud的项目pom中引入:

<dependency>
   <groupId>io.prometheus</groupId>
   <artifactId>simpleclient_servlet</artifactId>
   <version>0.9.0</version>
</dependency>
<dependency>
   <groupId>io.prometheus</groupId>
   <artifactId>simpleclient_pushgateway</artifactId>
   <version>0.9.0</version>
 </dependency>

bootstrap.yml增加配置:

management:
  metrics:
    export:
      prometheus:
        pushgateway:
          enabled: true
          push-rate: 60s
          base-url: 192.168.1.1:9091 #pushgateway地址
          job: xxx-gateway-job  #job名称

接入的问题

由于第一次没添加ip的上报,导致数据上报的时候都集中在一起,外加频率间隔过大,生成的图也很不好看。

在这里插入图片描述

解决方案

修改yml增加配置:

management:
  metrics:
    export:
      prometheus:
        pushgateway:
          enabled: true
          push-rate: 15s #缩短push频率
          base-url: 192.168.1.1:9091 #pushgateway地址
          job: xxx-gateway-job  #job名称
          groupingKey:
            instance: ${xxxx.prometheus.instance}  ##配置这个属性需单独引入ip

ip获取工具类

@Configuration
@ConditionalOnProperty(prefix = "management.metrics.export.prometheus.pushgateway", name = "enabled", havingValue = "true",
        matchIfMissing = false)
public class PrometheusTagConfig {

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer() {
        String ip = this.findFirstNonLoopbackAddress();
        System.setProperty("xxxx.prometheus.instance", ip);
        return (registry) -> registry.config().commonTags("instance", ip);
    }

    /***
     * 获取ip
     * @date 2022/9/16 14:42
     * @return java.lang.String
     */
    private String findFirstNonLoopbackAddress() {
        try {
            InetAddress result = null;
            int lowest = Integer.MAX_VALUE;
            for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
                 nics.hasMoreElements(); ) {
                NetworkInterface ifc = nics.nextElement();
                if (ifc.isUp()) {
                    if (ifc.getIndex() < lowest || result == null) {
                        lowest = ifc.getIndex();
                    } else if (result != null) {
                        continue;
                    }
                    for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements(); ) {
                        InetAddress address = addrs.nextElement();
                        if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
                            result = address;
                        }
                    }
                }
            }
            return result != null ? result.getHostAddress() : null;
        } catch (Exception e) {
        }
        return null;
    }
}

现在采集的可以区分ip,而且频率看的曲线比较适合观看。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud Gateway内置了访问日志功能,可以通过配置日志级别来输出请求的详细信息,包括请求URL、请求方法、请求参数、响应状态码、响应时间等。通过分析访问日志,可以统计访问次数。 另外,Spring Cloud Gateway还可以集成PrometheusGrafana来进行实时监控和统计。具体步骤如下: 1. 集成Prometheus 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-prometheus</artifactId> </dependency> ``` 在application.yml文件中添加以下配置: ```yaml management: endpoints: web: exposure: include: prometheus metrics: export: prometheus: enabled: true ``` 2. 集成Grafana 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-grafana</artifactId> </dependency> ``` 在application.yml文件中添加以下配置: ```yaml management: grafana: host: http://localhost:3000 token: ${GRAFANA_API_TOKEN} enabled: true ``` 其中,GRAFANA_API_TOKEN是Grafana的API访问令牌。 3. 在Prometheus中配置数据源 在Prometheus的配置文件prometheus.yml中添加以下内容: ```yaml scrape_configs: - job_name: 'spring-cloud-gateway' metrics_path: '/actuator/prometheus' static_configs: - targets: ['localhost:8080'] ``` 其中,metrics_path是Spring Cloud Gateway暴露的Prometheus指标的URL路径,targets是Spring Cloud Gateway的地址和端口号。 4. 在Grafana中创建仪表盘 在Grafana中创建一个新的仪表盘,选择Prometheus作为数据源,然后添加需要监控的指标。例如,可以添加以下指标: - gateway_requests_seconds_count:请求次数 - gateway_requests_seconds_max:最大响应时间 - gateway_requests_seconds_sum:响应时间总和 通过监控仪表盘,可以实时查看Spring Cloud Gateway的请求次数、响应时间等信息,方便统计访问次数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烤鸭的世界我们不懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值