当前位置:网站首页>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 .
@Aspectpublic 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 .
@Aspectpublic 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();}@Overridepublic 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 IsAutoWrapperString 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]所创,转载请带上原文链接,感谢
边栏推荐
- Gradient understanding decline
- JetCache埋点的骚操作,不服不行啊
- Elasticsearch database | elasticsearch-7.5.0 application construction
- Chainlink将美国选举结果带入区块链 - Everipedia
- Electron应用使用electron-builder配合electron-updater实现自动更新
- Anomaly detection method based on SVM
- GUI 引擎评价指标
- 自然语言处理之命名实体识别-tanfordcorenlp-NER(一)
- Python machine learning algorithm: linear regression
- 快快使用ModelArts,零基础小白也能玩转AI!
猜你喜欢
随机推荐
简直骚操作,ThreadLocal还能当缓存用
A debate on whether flv should support hevc
keras model.compile损失函数与优化器
被老程式設計師壓榨怎麼辦?我不想辭職
Details of dapr implementing distributed stateful service
Clean架构能够解决哪些问题? - jbogard
DRF JWT authentication module and self customization
十二因子原则和云原生微服务 - DZone
数据科学家与机器学习工程师的区别? - kdnuggets
iptables基礎原理和使用簡介
GUI 引擎评价指标
[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
微信小程序:防止多次点击跳转(函数节流)
嘘!异步事件这样用真的好么?
After brushing leetcode's linked list topic, I found a secret!
Query意图识别分析
01 . Go语言的SSH远程终端及WebSocket
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
接口压力测试:Siege压测安装、使用和说明
GDB除錯基礎使用方法




