上一篇文章是关于 “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应用,就需要额外开接口或者拉取进行权限验证很不方便

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

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

  

spring boot metrics信息推送开发的更多相关文章

  1. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  2. 《推送开发全面盘点当前Android后台保活方案的真实运行效果》

        登录 立即注册 TCP/IP详解 资讯 动态 社区 技术精选 首页   即时通讯网›专项技术区›推送开发全面盘点当前Android后台保活方案的真实运行效果(截止2 ...   帖子 打赏 分 ...

  3. Spring Boot 的Maven多模块开发web项目使用外部容器进行部署

    Spring Boot中自带有Tomcat容器,因此Spring Boot项目只需要运行main函数,就可以运行,但是以往的web项目,我们习惯于使用自己安装的Tomcat运行或者使用Tomcat.J ...

  4. spring boot + vue + element-ui全栈开发入门——开篇

    最近经常看到很多java程序员朋友还在使用Spring 3.x,Spring MVC(struts),JSP.jQuery等这样传统技术.其实,我并不认为这些传统技术不好,而我想表达的是,技术的新旧程 ...

  5. spring boot + vue + element-ui全栈开发入门——基于Electron桌面应用开发

     前言 Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. Electron通过将Chromium和Node.js合并到同一个运行时环 ...

  6. spring boot + vue + element-ui全栈开发入门

    今天想弄弄element-ui  然后就在网上找了个例子 感觉还是可以用的  第一步是完成了  果断 拿过来  放到我这里这  下面直接是连接  点进去 就可以用啊 本想着不用vue   直接导入连接 ...

  7. Pushlet实现后台信息推送(二)

    上一篇日志利用推送源周期性地向订阅了某一事件的所有网页端推送信息,但怎么实现向特定的某一个用户推送信息呢,想象一个网络聊天室,怎么向单独的一个好友私聊呢.问题的关键就是那个SessionID,Push ...

  8. php 微信客服信息推送失败 微信重复推送客服消息 40001 45047

    /*** * 微信客服发送信息 * 微信客服信息推送失败 微信重复推送客服消息 40001 45047 * 递归提交到微信 直到提交成功 * @param $openid * @param int $ ...

  9. iOS开发:创建推送开发证书和生产证书,以及往极光推送官网上传证书的步骤方法

    在极光官网上面上传应用的极光推送证书的实质其实就是上传导出的p12文件,在极光推送应用管理里面,需要上传两个p12文件,一个是生产证书,一个是开发证书 ,缺一不可,具体如下所示: 在开发者账号里面创建 ...

随机推荐

  1. MySQL抓包工具:MySQL Sniffer【转】

    本文来自:https://github.com/Qihoo360/mysql-sniffer 简介 MySQL Sniffer 是一个基于 MySQL 协议的抓包工具,实时抓取 MySQLServer ...

  2. RNN入门(二)识别验证码

    介绍   作为RNN的第二个demo,笔者将会介绍RNN模型在识别验证码方面的应用.   我们的验证码及样本数据集来自于博客: CNN大战验证码,在这篇博客中,我们已经准备好了所需的样本数据集,不需要 ...

  3. Guid的生成和数据修整(去除空格和小写字符)

    SqlServer实现 SELECT LOWER(LTRIM(RTRIM(REPLACE(NEWID(),'-','')))) NEWID()函数产生随机数,例如:F874153F-D99B-40A9 ...

  4. C# 操作注册表WindowsRegistry

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...

  5. 在 UWP 中实现 Expander 控件

    WPF 中的 Expander 控件在 Windows 10 SDK 中并不提供,本文主要说明,如何在 UWP 中创建这样一个控件.其效果如下图: 首先,分析该控件需要的一些特性,它应该至少包括如下三 ...

  6. 【WebSocket No.3】使用WebSocket协议来做服务器

    写在开始 上面一篇写了一篇使用WebSocket做客户端,然后服务端是socke代码实现的.传送门:webSocket和Socket实现聊天群发 本来我是打算写到一章上的,毕竟实现的都是一样的功能,后 ...

  7. Again Prime? No Time.(uva10870+数论)

    Again Prime? No time.Input: standard inputOutput: standard outputTime Limit: 1 second The problem st ...

  8. Elasticsearch系列(1):认识Elasticsearch

    官方定义 Elasticsearch 是一个实时的分布式搜索分析引擎, 它能让你以一个之前从未有过的速度和规模,去探索你的数据. 它被用作全文检索.结构化搜索.分析以及这三个功能的组合. Elasti ...

  9. Python 部分系统类的常用方法整理

    下面是常用的几个系统类的常用方法整理: list: 列表[1, 2,...] set: 集合,无重复元素{1, 2,...} str: 字符串 dict: 字典{a:'a', b:'b',...} T ...

  10. bootstrap timepicker 在angular中取值赋值 并转化为时间戳

    上一篇我们讲到angular对于timepicker的一个封装后的插件angular-bootstrap-timepicker,但是由于angular的版本必须是v1.2.30以上的.对于有些涉及到多 ...