当前位置:网站首页>How do the general bottom buried points do?
How do the general bottom buried points do?
2020-11-06 01:15:00 【Yin Jihuan】
It takes time to monitor the database in the program , Want to automatically transfer link tracking information in the underlying framework , These needs often come across , Especially when building the infrastructure .
There is only one core goal , That is to encapsulate it at the bottom , It doesn't need to be cared by the upper users . Today, I'd like to talk to you about how to deal with the common embedded point method of the underlying extension .
The framework has its own extension point
If you use a framework when you design it , It is very convenient to reserve extension points . such as Mybatis Interceptor , We can deal with... In the interceptor Sql monitor , rewrite .
Like Ali's Sentinel frame , Can pass SPI To expand Slot, Adjust the order of arrangement , Add custom Slot To realize current limiting alarm, etc .
The quality of open source frameworks varies , There's something better designed in the early days , There are plenty of extension points , Convenient for users . There are also some that are less comprehensive , You need to extend it when you use it , Found no extension point found , For scenarios where the framework itself does not provide extension points , Please go on to the following .
Modify source code
If the framework has no extension points , The most direct way is to modify the source code of the open source framework to expand the functions you want , The usual way is to clone the source code into its own private repository , And then modify , test , Repackage for use .
Like we used XXL-JOB Do task scheduling , Also changed some code , The configuration information of monitoring notification is extended in the interface , By default, only email is supported , It can be extended to mobile phones , Nails, etc .
The bad thing about modifying the source code is that it needs to be aligned with the version of the original framework , If it's not aligned , It's OK to change it . Misalignment means that something has been fixed bug And some new features , Can't use . To align , You need to constantly merge new versions of the code into your own branches .
There are many other companies , It's an open source version , Build your own version of the company , The follow-up is to follow the internal use requirements to expand and maintain , It doesn't synchronize with the community version , If you have a dedicated team to do this , It's also a way .
File with the same name is covered
The way to change the source code needs to synchronize the new version of the code frequently , Sometimes you just want to modify a class , For example, some operations at the bottom are monitored by buried points , If the framework itself does not provide extension points, you can only change the source code to achieve .
In fact, there is a way to take advantage of it , It is to create as like as two peas you want to modify in the project , Package name + The categories are the same , The same is true for method names , You can change the implementation of the method . So you can cover jar The class in the package , It has something to do with the order in which the classes are loaded , Load your own definition first .
The advantage of this approach is that you don't have to synchronize new versions of code often , If you're using an updated version of the framework , As long as the package name and class name remain unchanged , You only cover that class , New features and fixed bug No impact .
Cut in the face
Facets are very useful when doing a lot of uniform processing , The same is true for the scene where the bottom buried point is made .
For example, we need to focus on Mongodb All operations are monitored at the buried point , You can modify MongoDB Driver source code , You can create a file with the same name to overlay , There are many ways , Find a fit , And the most important thing to achieve the requirements .
With Spring In the operation Mongodb To illustrate , stay Spring Data Mongodb China will MongoTemplate To operate Mongodb. The easiest way is to go straight to MongoTemplate Class to be buried , So that all operations can be monitored .
Cut directly to with a section MongoTemplate In all ways , And then bury it , It's easy .
@Aspect
public class MongoTemplateAspect {
@Pointcut("execution(* org.springframework.data.mongodb.core.MongoTemplate.*(..))")
public void pointcut() {}
@Around("pointcut()")
public Object around(final ProceedingJoinPoint pjp) throws Throwable {
String callMethod = pjp.getSignature().getDeclaringType().getSimpleName() + "." + pjp.getSignature().getName();
Map<String, Object> data = new HashMap<>();
data.put("params", JsonUtils.toJson(pjp.getArgs()));
return CatTransactionManager.newTransaction(() -> {
try {
return pjp.proceed();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}, "Mongo", callMethod, data);
}
}
And such as , You also want to monitor Redis dependent ,Redis It's also with Spring Integrated framework , Then there is RedisTemplate This class , The same can be done with facets .
be based on Template Class to bury some , It's relatively upper level , If you want to monitor at the bottom , That is to say Connection This floor ,Template The operations are based on Connection To achieve .
Again, we can replace... With sections Connection Related implementation , You can get a slice, for example Connection Methods , Then replace Connection The object of monitoring is the object with buried point monitoring .
@Aspect
public class RedisAspect {
@Pointcut("target(org.springframework.data.redis.connection.RedisConnectionFactory)")
public void connectionFactory() {}
@Pointcut("execution(org.springframework.data.redis.connection.RedisConnection *.getConnection(..))")
public void getConnection() {}
@Pointcut("execution(org.springframework.data.redis.connection.RedisClusterConnection *.getClusterConnection(..))")
public void getClusterConnection() {}
@Around("getConnection() && connectionFactory()")
public Object aroundGetConnection(final ProceedingJoinPoint pjp) throws Throwable {
RedisConnection connection = (RedisConnection) pjp.proceed();
return new CatMonitorRedisConnection(connection);
}
@Around("getClusterConnection() && connectionFactory()")
public Object aroundGetClusterConnection(final ProceedingJoinPoint pjp) throws Throwable {
RedisClusterConnection clusterConnection = (RedisClusterConnection) pjp.proceed();
return new CatMonitorRedisClusterConnection(clusterConnection);
}
}
CatMonitorRedisConnection Medium to primary RedisConnection Be enhanced , It doesn't affect the original RedisConnection The function of .
public class CatMonitorRedisConnection implements RedisConnection {
private final RedisConnection connection;
private CatMonitorHelper catMonitorHelper;
public CatMonitorRedisConnection(RedisConnection connection) {
this.connection = connection;
this.catMonitorHelper = new CatMonitorHelper();
}
@Override
public byte[] get(byte[] key) {
return catMonitorHelper.execute(RedisCommand.GET, key, () -> connection.get(key));
}
}
Java Agent
Java Agent You can change the bytecode of a loaded class at run time , We can add code logic that we need to monitor . There is no need to modify the original code , Zero invasive .
You can see it in a lot of great open source frameworks Java Agent Application , image APM frame SkyWalking, Asynchronously passing context transmittable-thread-local etc. .
Java Agent Compared with other ways , There is still a certain threshold , After all, it is not a technical point that is often used in daily development . If you want to understand this extension , You can take a look at the source code of some open source frameworks that have been used , I'll know how to use it . Post a paragraph below transmittable-thread-local In the thread pool to expand the code bar , The main thing is to make use of javassist Operation bytecode .
try {
final CtMethod afterExecute = clazz.getDeclaredMethod("afterExecute", new CtClass[]{runnableClass, throwableClass});
// unwrap runnable if IsAutoWrapper
String code = "$1 = com.alibaba.ttl.threadpool.agent.internal.transformlet.impl.Utils.unwrapIfIsAutoWrapper($1);";
logger.info("insert code before method " + signatureOfMethod(afterExecute) + " of class " + afterExecute.getDeclaringClass().getName() + ": " + code);
afterExecute.insertBefore(code);
modified = true;
} catch (NotFoundException e) {
// clazz does not override afterExecute method, do nothing.
}
About author : Yin Jihuan , Simple technology enthusiasts ,《Spring Cloud Microservices - Full stack technology and case analysis 》, 《Spring Cloud Microservices introduction Actual combat and advanced 》 author , official account Ape world Originator .
I have compiled a complete set of learning materials , Those who are interested can search through wechat 「 Ape world 」, Reply key 「 Learning materials 」 Get what I've sorted out Spring Cloud,Spring Cloud Alibaba,Sharding-JDBC Sub database and sub table , Task scheduling framework XXL-JOB,MongoDB, Reptiles and other related information .
版权声明
本文为[Yin Jihuan]所创,转载请带上原文链接,感谢
边栏推荐
- Cocos Creator 原始碼解讀:引擎啟動與主迴圈
- [C#] (原創)一步一步教你自定義控制元件——04,ProgressBar(進度條)
- Menu permission control configuration of hub plug-in for azure Devops extension
- 普通算法面试已经Out啦!机器学习算法面试出炉 - kdnuggets
- Microservices: how to solve the problem of link tracing
- 选择站群服务器的有哪些标准呢?
- 【C/C++ 2】Clion配置与运行C语言
- 【快速因數分解】Pollard's Rho 演算法
- 简直骚操作,ThreadLocal还能当缓存用
- python 下载模块加速实现记录
猜你喜欢
DRF JWT authentication module and self customization
(1) ASP.NET Introduction to core3.1 Ocelot
TF flags的简介
python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库
TensorFlow2.0 问世,Pytorch还能否撼动老大哥地位?
Gradient understanding decline
连肝三个通宵,JVM77道高频面试题详细分析,就这?
Pycharm快捷键 自定义功能形式
自然语言处理之分词、命名主体识别、词性、语法分析-stanfordcorenlp-NER(二)
词嵌入教程
随机推荐
【QT】 QThread部分原始碼淺析
nlp模型-bert从入门到精通(二)
如何在Windows Server 2012及更高版本中將域控制器降級
数据科学家与机器学习工程师的区别? - kdnuggets
文本去重的技术方案讨论(一)
python过滤敏感词记录
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
After brushing leetcode's linked list topic, I found a secret!
小白量化投资交易入门课(python入门金融分析)
Microservices: how to solve the problem of link tracing
连肝三个通宵,JVM77道高频面试题详细分析,就这?
GBDT与xgb区别,以及梯度下降法和牛顿法的数学推导
Query意图识别分析
如何对Pandas DataFrame进行自定义排序
Basic principle and application of iptables
【快速因數分解】Pollard's Rho 演算法
ETCD核心機制解析
Top 10 best big data analysis tools in 2020
7.2.2 compressing static resources through gzipresourceresolver
基于深度学习的推荐系统