DUBBO监控,设置接口调用数据的上报周期 值得一看
yuyutoo 2024-10-26 16:09 13 浏览 0 评论
DUBBO监控,设置接口调用数据的上报周期
dubbo是目前比较好用的,用来实现soa架构的一个工具,dubbo的用法和好处,我们这里略过,今天我们来讨论跟监控有关的话题。
大家大知道,在确保系统的稳定道路上,系统监控是必不可少的,只有实时监控系统中接口的调用情况,并定期汇总统计数据,才能知道系统是否到了瓶颈,以及是否有接口异常调用情况。
dubbo已有的监控方案
dubbo中自带了dubbo-monitor模块,其作用简单来说就是:
根据配置判断是否启用
MonitorFilter
过滤器
当dubbo的xml配置文件中配置了<dubbo:monitor xxx/>
,比如:<dubbo:monitor protocol="registry" />
或<dubbo:monitor protocol="dubbo" address="dubbo://127.0.0.1:30004/MonitorService" />
等等
根据protocol配置,判断获取
MonitorService
接口方式以及获取接口地址
如果是registry
根据<dubbo:registry xxx />
配置获取,如果是dubbo
直接使用address属性配置的地址
根据protocol配置,判断使用哪个
MonitorFactory
如果是
registry
,则根据最后获取到的具体通信协议来决定;如果是
dubbo
,使用默认工厂类:DubboMonitorFactroy。见dubbo默认配置文件:
META-INF/dubbo/internal/com.alibaba.dubbo.monitor.MonitorFactory
dubbo=com.alibaba.dubbo.monitor.dubbo.DubboMonitorFactroy
这里我们可以自定义协议,比如myDubbo
,然后在项目中创建一个文件:
META-INF/dubbo/com.alibaba.dubbo.monitor.MonitorFactory
myDubbo=org.rembau.dubboTest.MyDubboMonitorFactory
这时项目启动时,如果发现协议为myDubbo,会使用MyDubboMonitorFactory
执行相关操作。
问题:这里暴露了监控配置的一个问题,就是我们不能为dubbo协议重新定义一个MonitorFactory
,所以连带的不能重新实现Monitor
。
使用
MonitorFilter
过滤器处理每一个接口调用
如果我们用的dubbo协议来连接MonitorService
服务,则是使用DubboMonitor
实例来处理接口调用数据,该类实现了对调用数据的简单汇总,并每60秒调用MonitorService
服务的collect方法,把数据发送出来。
使用dubbo-monitor-simple接收数据
dubbo-monitor-simple是dubbo提供的简单监控中心,可以用来显示接口暴露,注册情况,也可以看接口的调用明细,调用时间等。
还有其他一些比较好的工具比如Dubbo-Monitor等
针对已有方案的改进
写到这里才是本文的重点,我们绝大部分使用者都是使用dubbo协议进行通信,所以也就是使用DubboMonitor
发送数据,这已经完全够用了。但是DubboMonitor
是每分钟上报一次,并且每个应用中,与之有关的所有接口都会产生一条数据,生产环境中,数据量还是挺大的。如果上报的时间间隔可以设置为10分钟或者30分钟,数据量也会相应减少10倍、30倍,如果我们自己汇聚还需要分布式服务对数据的一致性处理,会把事情变复杂。
查看
DubboMonitor
源码:
DubboMonitor
的构造方法:
this.monitorInterval = (long)monitorInvoker.getUrl().getPositiveParameter("interval", '60000');this.sendFuture = this.scheduledExecutorService.scheduleWithFixedDelay(new Runnable() { public void run() { try { DubboMonitor.this.send(); } catch (Throwable var2) { DubboMonitor.logger.error("Unexpected error occur at send statistic, cause: " + var2.getMessage(), var2); } } }, this.monitorInterval, this.monitorInterval, TimeUnit.MILLISECONDS);
其中.getPositiveParameter("interval", '60000')
方法大概意思是,在URL对象实例中找interval参数的值。那URL的参数会从什么地方获取呢?
MonitorConfig
的属性、MonitorConfig
中Map<String, String> parameters
属性的key-value
最终获取的URL是根据MonitorConfig
得来的,查看AbstractInterfaceConfig
代码:
protected URL loadMonitor(URL registryURL) { if (monitor == null) { String monitorAddress = ConfigUtils.getProperty("dubbo.monitor.address"); String monitorProtocol = ConfigUtils.getProperty("dubbo.monitor.protocol"); if (monitorAddress != null && monitorAddress.length() > 0 || monitorProtocol != null && monitorProtocol.length() > 0) { monitor = new MonitorConfig(); } else { return null; } } appendProperties(monitor); Map<String, String> map = new HashMap<String, String>(); map.put(Constants.INTERFACE_KEY, MonitorService.class.getName()); map.put("dubbo", Version.getVersion()); map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis())); if (ConfigUtils.getPid() > 0) { map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid())); } appendParameters(map, monitor); String address = monitor.getAddress(); String sysaddress = System.getProperty("dubbo.monitor.address"); if (sysaddress != null && sysaddress.length() > 0) { address = sysaddress; } if (ConfigUtils.isNotEmpty(address)) { if (! map.containsKey(Constants.PROTOCOL_KEY)) { if (ExtensionLoader.getExtensionLoader(MonitorFactory.class).hasExtension("logstat")) { map.put(Constants.PROTOCOL_KEY, "logstat"); } else { map.put(Constants.PROTOCOL_KEY, "dubbo"); } } return UrlUtils.parseURL(address, map); } else if (Constants.REGISTRY_PROTOCOL.equals(monitor.getProtocol()) && registryURL != null) { return registryURL.setProtocol("dubbo").addParameter(Constants.PROTOCOL_KEY, "registry").addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)); } return null; }
方案1 使用javassist动态重写
MonitorConfig
class
在应用启动时,使用javassist给MonitorConfig
添加interval字段,并设置默认值,DubboMonitor
初始化时会获取到该字段的值,并使用这个值当作发送数据的周期。
如果应用使用了spring框架,则下面代码有关在加载spring配置文件之前执行。如果应用是web应用则处理起来更麻烦,需要重写org.springframework.web.context.ContextLoaderListener
代码如下:
ClassPool pool = ClassPool.getDefault(); try { CtClass cc = pool.get("com.alibaba.dubbo.config.MonitorConfig"); // 增加属性,这里仅仅是增加属性字段 CtField ctField = new CtField(CtClass.intType, "interval", cc); ctField.setModifiers(Modifier.PUBLIC); cc.addField(ctField, "10000");//设置10秒周期 CtMethod ctMethod = new CtMethod(CtClass.intType, "getInterval", null, cc); ctMethod.setBody("return interval;"); ctMethod.setModifiers(Modifier.PUBLIC); cc.addMethod(ctMethod); cc.toClass(); } catch (NotFoundException | CannotCompileException e) { logger.error("", e); }
方案2 使用
<dubbo:registry xxx />
配置
分析源码,可以知道,如果配置<dubbo:registry interval="10000" xxx />
,dubbo解析文件时,会把interval="10000"当作一个key-value存入MonitorConfig
中Map<String, String> parameters
DubboBeanDefinitionParser
中parse方法(摘选)
NamedNodeMap attributes = element.getAttributes(); int len = attributes.getLength(); for (int i = 0; i < len; i++) { Node node = attributes.item(i); String name = node.getLocalName(); if (! props.contains(name)) { if (parameters == null) { parameters = new ManagedMap(); } String value = node.getNodeValue(); parameters.put(name, new TypedStringValue(value, String.class)); } } if (parameters != null) { beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters); }
但是有个问题,如果我给<dubbo:registry xxx>
加上interval属性,应用启动时会报错。我们知道原因是dubbo的xsd文件没有放开这个限制,我们只要修改dubbo.xsd文件就可以了,但是dubbo.xsd是在jar包里,所以我们改不了。
我们可以自己写xsd配置,重命名为dubbo-youyue.xsd:
文件头改为:
<xsd:schema xmlns="http://code.alibabatech.com/schema/youyue/dubbo"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"xmlns:tool="http://www.springframework.org/schema/tool"targetNamespace="http://code.alibabatech.com/schema/youyue/dubbo">
monitorType节点添加:
<xsd:attribute name="interval" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><![CDATA[ The monitor interval. ]]></xsd:documentation> </xsd:annotation></xsd:attribute>
然后把dubbo.xml中关联的dubo.xsd改成我们自己写的xsd,如:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
改成
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:dubbo="http://code.alibabatech.com/schema/youyue/dubbo"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://code.alibabatech.com/schema/youyue/dubbo http://code.alibabatech.com/schema/youyue/dubbo/dubbo.xsd">
添加 spring.schemas 文件,用来使用本地文件取代远程文件
http://code.alibabatech.com/schema/youyue/dubbo/dubbo.xsd=META-INF/dubbo-youyue.xsd
添加 spring.handlers 文件,用来标记分析dubbo.xml中<dubbo:xx xx>
节点的分析处理类
http://code.alibabatech.com/schema/youyue/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
至此,配置好<dubbo:registry interval="10000" xxx />
启动应用,可以看到上报周期由60秒变成了10秒。
高级架构师视频资料分享链接:
data:text/html;charset=UTF-8;base64,
5p625p6E5biI5a2m5Lmg5Lqk5rWB576k5Y+35pivNTc1NzUxODU0Cg==
复制粘贴在网站即可!
相关推荐
- Python操作Word文档神器:python-docx库从入门到精通
-
Python操作Word文档神器:python-docx库从入门到精通动动小手,点击关注...
- Python 函数调用从入门到精通:超详细定义解析与实战指南 附案例
-
一、函数基础:定义与调用的核心逻辑定义:函数是将重复或相关的代码块封装成可复用的单元,通过函数名和参数实现特定功能。它是Python模块化编程的基础,能提高代码复用性和可读性。定义语法:...
- 等这么长时间Python背记手册终于来了,入门到精通(视频400集)
-
本文毫无套路!真诚分享!前言:无论是学习任何一门语言,基础知识一定要扎实,基础功非常的重要,找一个有丰富编程经验的老师或者师兄带着你会少走很多弯路,你的进步速度也会快很多,无论我们学习的目的是什么,...
- 图解Python编程:从入门到精通系列教程(附全套速查表)
-
引言本系列教程展开讲解Python编程语言,Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,它也是互联网最热门的编程语言之一。Python生态丰富,库(模块)极其丰富,这使...
- Python入门教程(非常详细)从零基础入门到精通,看完这一篇就够
-
本书是Python经典实例解析,采用基于实例的方法编写,每个实例都会解决具体的问题和难题。主要内容有:数字、字符串和元组,语句与语法,函数定义,列表、集、字典,用户输入和输出等内置数据结构,类和对象,...
- Python函数全解析:从入门到精通,一文搞定!
-
1.为什么要用函数?函数的作用:封装代码,提高复用性,减少重复,提高可读性。...
- Python中的单例模式:从入门到精通
-
Python中的单例模式:从入门到精通引言单例模式是一种常用的软件设计模式,它保证了一个类只有一个实例,并提供一个全局访问点。这种模式通常用于那些需要频繁创建和销毁的对象,比如日志对象、线程池、缓存等...
- 【Python王者归来】手把手教你,Python从入门到精通!
-
用800个程序实例、5万行代码手把手教你,Python从入门到精通!...
- Python从零基础入门到精通:一个月就够了
-
如果想从零基础到入门,能够全职学习(自学),那么一个月足够了。...
- Python 从入门到精通:一个月就够了
-
要知道,一个月是一段很长的时间。如果每天坚持用6-7小时来做一件事,你会有意想不到的收获。作为初学者,第一个月的月目标应该是这样的:熟悉基本概念(变量,条件,列表,循环,函数)练习超过30个编...
- Python零基础到精通,这8个入门技巧让你少走弯路,7天速通编程!
-
Python学习就像玩积木,从最基础的块开始,一步步搭建出复杂的作品。我记得刚开始学Python时也是一头雾水,走了不少弯路。现在回头看,其实掌握几个核心概念,就能快速入门这门编程语言。来聊聊怎么用最...
- 神仙级python入门教程(非常详细),从0到精通,从看这篇开始!
-
python入门虽然简单,很多新手依然卡在基础安装阶段,大部分教程对一些基础内容都是一带而过,好多新手朋友,对一些基础知识常常一知半解,需要在网上查询很久。...
- Python类从入门到精通,一篇就够!
-
一、Python类是什么?大家在生活中应该都见过汽车吧,每一辆真实存在、能在路上跑的汽车,都可以看作是一个“对象”。那这些汽车是怎么生产出来的呢?其实,在生产之前,汽车公司都会先设计一个详细的蓝图...
- 学习Python从入门到精通:30天足够了,这才是python基础的天花板
-
当年2w买的全套python教程用不着了,现在送给有缘人,不要钱,一个月教你从入门到精通1、本套视频共487集,本套视频共分4季...
- 30天Python 入门到精通(3天学会python)
-
以下是一个为期30天的Python入门到精通学习课程,专为零基础新手设计。课程从基础语法开始,逐步深入到面向对象编程、数据处理,最后实现运行简单的大语言模型(如基于HuggingFace...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)