spring boot metrics信息推送开发

简介:     上一篇文章是关于 “spring boot +RabbitMQ +InfluxDB+Grafara监控实践” 主要讲spring boot应用新能监控信息的收集方案实践   实践是hystrix信息推送的mq而metrics信息需要扫描,文章的最后也有相应的思考metrics信息能不能是应用本身也推送到mq那?   本篇文章就实践关于metrics信息的推送实现     有了上面的思考之后我就回过头来去看hystrix是怎么实现推送的。

 

  上一篇文章是关于 “spring boot +RabbitMQ +InfluxDB+Grafara监控实践” 主要讲spring boot应用新能监控信息的收集方案实践

  实践是hystrix信息推送的mq而metrics信息需要扫描,文章的最后也有相应的思考metrics信息能不能是应用本身也推送到mq那?

  本篇文章就实践关于metrics信息的推送实现

 

  有了上面的思考之后我就回过头来去看hystrix是怎么实现推送的。经过一番跟踪之后找到了具体干活的task代码

  

  有了这个代码就可以参考具体怎样实现metrics信息的推送了

  但是还有一个问题就是metrics信息虽然暴露了url接口但是应用内我怎么获取那???

  这里又引发了我们一探究竟的兴趣!。。。。。。继续看源码!!!!!!!!!!!

  从spring boot启动展示的日志中我们可以发现线索,具体/metrics路径具体执行的是哪里

  

Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()

  从org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()这里我们发现了端倪

  好的 我们就去这个包去找相关线索

  

  好的我们找到了这个包往下看

  终于找到他了这里我们就可以用定时器进行轮训调用了。基础准备已经ok,好了不多说了直接上写好的代码


package com.zjs.mic.metrics.stream;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.config.BindingProperties;
import org.springframework.cloud.stream.config.BindingServiceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.annotation.EnableScheduling;


@RefreshScope
@Configuration
@ConditionalOnClass({EnableBinding.class })
@ConditionalOnProperty(value = "metrics.stream.queue.enabled", matchIfMissing = true)
@EnableConfigurationProperties
@EnableScheduling
@EnableBinding(MetricsStreamClient.class)
public class MetricsStreamAutoConfiguration {

    @Autowired
    private BindingServiceProperties bindings;

    @Autowired
    private MetricsStreamProperties properties;

    @Autowired
    @Output(MetricsStreamClient.OUTPUT)
    private MessageChannel outboundChannel;

    @Autowired(required = false)
    private Registration registration;
    
    
    @Autowired
    MetricsMvcEndpoint mme;
    

    @Bean
    public HasFeatures metricsStreamQueueFeature() {
        return HasFeatures.namedFeature("Metrics Stream (Queue)",
                MetricsStreamAutoConfiguration.class);
    }

    @PostConstruct
    public void init() {
        BindingProperties outputBinding = this.bindings.getBindings()
                .get(MetricsStreamClient.OUTPUT);
        if (outputBinding == null) {
            this.bindings.getBindings().put(MetricsStreamClient.OUTPUT,
                    new BindingProperties());
        }
        BindingProperties output = this.bindings.getBindings()
                .get(MetricsStreamClient.OUTPUT);
        if (output.getDestination() == null) {
            output.setDestination(this.properties.getDestination());
        }
        if (output.getContentType() == null) {
            output.setContentType(this.properties.getContentType());
        }
    }
    @Bean
    public MetricsStreamTask metricsStreamTask(SimpleDiscoveryClient simpleDiscoveryClient) {
        ServiceInstance serviceInstance = this.registration;
        if (serviceInstance == null) {
            serviceInstance = simpleDiscoveryClient.getLocalServiceInstance();
        }
        return new MetricsStreamTask(this.outboundChannel, serviceInstance,
                this.properties,this.mme);
    }
}


package com.zjs.mic.metrics.stream;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("metrics.stream.queue")
public class MetricsStreamProperties {

    private boolean enabled = true;

    private boolean prefixMetricName = true;

    private boolean sendId = true;

    private String destination = "springCloudMetricsStream";

    private String contentType = "application/json";
    
    private String pathTail = "mem.*|heap.*|threads.*|gc.*|nonheap.*|classes.*";

    private long sendRate = 1000;

    private long gatherRate = 1000;

    private int size = 1000;


    public String getPathTail() {
        return pathTail;
    }

    public void setPathTail(String pathTail) {
        this.pathTail = pathTail;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isPrefixMetricName() {
        return prefixMetricName;
    }

    public void setPrefixMetricName(boolean prefixMetricName) {
        this.prefixMetricName = prefixMetricName;
    }

    public boolean isSendId() {
        return sendId;
    }

    public void setSendId(boolean sendId) {
        this.sendId = sendId;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public long getSendRate() {
        return sendRate;
    }

    public void setSendRate(long sendRate) {
        this.sendRate = sendRate;
    }

    public long getGatherRate() {
        return gatherRate;
    }

    public void setGatherRate(long gatherRate) {
        this.gatherRate = gatherRate;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}


package com.zjs.mic.metrics.stream;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.Assert;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;


@EnableScheduling
public class MetricsStreamTask {
    private final static Logger log = LoggerFactory.getLogger(MetricsStreamTask.class);

    private MessageChannel outboundChannel;

    private ServiceInstance registration;

    private MetricsStreamProperties properties;

    private MetricsMvcEndpoint mme;

    // Visible for testing
    final LinkedBlockingQueue<String> jsonMetrics;

    private final JsonFactory jsonFactory = new JsonFactory();

    public MetricsStreamTask(MessageChannel outboundChannel,
                             ServiceInstance registration, MetricsStreamProperties properties, MetricsMvcEndpoint mme) {
        Assert.notNull(outboundChannel, "outboundChannel may not be null");
        Assert.notNull(registration, "registration may not be null");
        Assert.notNull(properties, "properties may not be null");
        Assert.notNull(mme, "properties may not be null");
        this.outboundChannel = outboundChannel;
        this.registration = registration;
        this.properties = properties;
        this.jsonMetrics = new LinkedBlockingQueue<>(properties.getSize());
        this.mme=mme;
    }
    // TODO: use integration to split this up?
    @Scheduled(fixedRateString = "${metrics.stream.queue.sendRate:1000}")
    public void sendMetrics() {

        log.info("推送metrics信息");
        
        ArrayList<String> metrics = new ArrayList<>();
        this.jsonMetrics.drainTo(metrics);

        if (!metrics.isEmpty()) {
            if (log.isTraceEnabled()) {
                log.trace("sending stream Metrics metrics size: " + metrics.size());
            }
            for (String json : metrics) {
                // TODO: batch all metrics to one message
                try {
                    // TODO: remove the explicit content type when s-c-stream can handle
                    // that for us
                    this.outboundChannel.send(MessageBuilder.withPayload(json)
                            .setHeader(MessageHeaders.CONTENT_TYPE,
                                    this.properties.getContentType())
                            .build());
                }
                catch (Exception ex) {
                    if (log.isTraceEnabled()) {
                        log.trace("failed sending stream Metrics metrics: " + ex.getMessage());
                    }
                }
            }
        }
    }

    
    
    @Scheduled(fixedRateString = "${metrics.stream.queue.gatherRate:1000}")
    public void gatherMetrics() {
        log.info("开始获取metrics信息");
        try {
            
            StringWriter jsonString = new StringWriter();
            JsonGenerator json = this.jsonFactory.createGenerator(jsonString);
            json.writeStartObject();
            json.writeObjectField("instanceId",registration.getServiceId() + ":" + registration.getHost() + ":"
                    + registration.getPort());
            json.writeObjectField("type", "metrics");
            json.writeObjectField("currentTime",System.currentTimeMillis());
            @SuppressWarnings("unchecked")
            Map<String, Object> map = (Map<String, Object>) mme.value(this.properties.getPathTail());
            
            for (String str : map.keySet()) {
                json.writeObjectField(str, map.get(str));
            }
            
            json.writeEndObject();
            json.close();
            
            
            // output to stream
            this.jsonMetrics.add(jsonString.getBuffer().toString());
            
        }
        catch (Exception ex) {
            log.error("Error adding metrics metrics to queue", ex);
        }
    }

    

}


package com.zjs.mic.metrics.stream;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface MetricsStreamClient {
    String OUTPUT = "metricsStreamOutput";

    @Output(OUTPUT)
    MessageChannel metricsStreamOutput();
}


package com.zjs.mic.metrics.stream;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MetricsStreamAutoConfiguration.class)
@EnableConfigurationProperties({MetricsStreamProperties.class})
public @interface EnableMetricsStream {

}

    已经将上面的代码包装成注解打好包 在入口类加@EnableMetricsStream 注解就能生效

    剩下的就是我们去mq接收信息传递到响应数据库中进行处理就行了

  从而我们在“spring boot +RabbitMQ +InfluxDB+Grafara监控实践” 这篇文章中的图就变成下面这样了

 

    好实践部分就到这里

  总结思考

    监控信息hystrix和metrics到底是拉取好还是主动推送好!一下简单分析:

    拉取,对于被监控的应用来说值引用少量的包节省了推送信息的线程,基本没有什么开发量,对于一些严格权限控制的springboot应用,就需要额外开接口或者拉取进行权限验证很不方便

    推送,应用主动推送应用相关的包和注解占用对应的线程资源,应用可以进行严格的权限控制不用对接口做例外不需要扫描程序开发。

  我的结论是两者并存,不知道大家有没有什么其他想法可以说来听听!

  

 

相关实践学习
快速体验阿里云云消息队列RocketMQ版
本实验将带您快速体验使用云消息队列RocketMQ版Serverless系列实例进行获取接入点、创建Topic、创建订阅组、收发消息、查看消息轨迹和仪表盘。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
9天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
850 25
|
2月前
|
前端开发 Java API
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
本文介绍了如何使用Spring WebFlux构建高效、可扩展的非阻塞API,涵盖响应式编程核心概念、技术方案设计及具体实现示例,适用于高并发场景下的API开发。
284 0
|
12天前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
1月前
|
安全 数据可视化 Java
AiPy开发的 Spring 漏洞检测神器,未授权访问无所遁形
针对Spring站点未授权访问问题,现有工具难以检测如Swagger、Actuator等组件漏洞,且缺乏修复建议。全新AI工具基于Aipy开发,具备图形界面,支持一键扫描常见Spring组件,自动识别未授权访问风险,按漏洞类型标注并提供修复方案,扫描结果可视化展示,支持导出报告,大幅提升渗透测试与漏洞定位效率。
|
2月前
|
安全 算法 Java
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
831 10
|
6月前
|
人工智能 Java 数据库
飞算 JavaAI:革新电商订单系统 Spring Boot 微服务开发
在电商订单系统开发中,传统方式耗时约30天,需应对复杂代码、调试与测试。飞算JavaAI作为一款AI代码生成工具,专注于简化Spring Boot微服务开发。它能根据业务需求自动生成RESTful API、数据库交互及事务管理代码,将开发时间缩短至1小时,效率提升80%。通过减少样板代码编写,提供规范且准确的代码,飞算JavaAI显著降低了开发成本,为软件开发带来革新动力。
|
6月前
|
前端开发 Java UED
从基础到进阶:Spring Boot + Thymeleaf 整合开发中的常见坑与界面优化
本文深入探讨了 **Spring Boot + Thymeleaf** 开发中常见的参数绑定问题与界面优化技巧。从基础的 Spring MVC 请求参数绑定机制出发,分析了 `MissingServletRequestParameterException` 的成因及解决方法,例如确保前后端参数名、类型一致,正确设置请求方式(GET/POST)。同时,通过实际案例展示了如何优化支付页面的视觉效果,借助简单的 CSS 样式提升用户体验。最后,提供了官方文档等学习资源,帮助开发者更高效地掌握相关技能。无论是初学者还是进阶用户,都能从中受益,轻松应对项目开发中的挑战。
292 0
|
2月前
|
缓存 Java API
Spring WebFlux 2025 实操指南详解高性能非阻塞 API 开发全流程核心技巧
本指南基于Spring WebFlux 2025最新技术栈,详解如何构建高性能非阻塞API。涵盖环境搭建、响应式数据访问、注解与函数式两种API开发模式、响应式客户端使用、测试方法及性能优化技巧,助你掌握Spring WebFlux全流程开发核心实践。
509 0