当前位置: 首页 > news >正文

agent扩展-自定义外部加载路径

自定义classLoader实现加载外部jar, 以skywalking agent 类加载器为例子

整体思路

  • 扩展findClass ,解决loadClass可以查找到
  • 扩展findResource,解决getResources可以获取到资源

基本原理

ClassLoader loadClass的加载顺序
findLoadedClass 加载本地已经加载过的class
parent.loadClass 通过父类去加载class
findClass 前面都没有加载到,就通过findClass去搜索jar并加载class
在这里插入图片描述
扩展findClass
在这里插入图片描述
在这里插入图片描述

扩展findResource

扩展后AgentClassLoader.getDefault().getResources(“skywalking-plugin.def”); 就可以获取插件定义
在这里插入图片描述

完整源代码

AgentClassLoader

/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/package org.apache.skywalking.apm.agent.core.plugin.loader;import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import lombok.RequiredArgsConstructor;
import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
import org.apache.skywalking.apm.agent.core.boot.PluginConfig;
import org.apache.skywalking.apm.agent.core.conf.Config;
import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap;/*** The <code>AgentClassLoader</code> represents a classloader, which is in charge of finding plugins and interceptors.*/
public class AgentClassLoader extends ClassLoader {static {/** Try to solve the classloader dead lock. See https://github.com/apache/skywalking/pull/2016*/registerAsParallelCapable();}private static final ILog LOGGER = LogManager.getLogger(AgentClassLoader.class);/*** The default class loader for the agent.*/private static AgentClassLoader DEFAULT_LOADER;private List<File> classpath;private List<Jar> allJars;private ReentrantLock jarScanLock = new ReentrantLock();public static AgentClassLoader getDefault() {return DEFAULT_LOADER;}/*** Init the default class loader.** @throws AgentPackageNotFoundException if agent package is not found.*/public static void initDefaultLoader() throws AgentPackageNotFoundException {if (DEFAULT_LOADER == null) {synchronized (AgentClassLoader.class) {if (DEFAULT_LOADER == null) {DEFAULT_LOADER = new AgentClassLoader(PluginBootstrap.class.getClassLoader());}}}}public AgentClassLoader(ClassLoader parent) throws AgentPackageNotFoundException {super(parent);File agentDictionary = AgentPackagePath.getPath();classpath = new LinkedList<>();Config.Plugin.MOUNT.forEach(mountFolder -> classpath.add(new File(agentDictionary, mountFolder)));}@Overrideprotected Class<?> findClass(String name) throws ClassNotFoundException {List<Jar> allJars = getAllJars();String path = name.replace('.', '/').concat(".class");for (Jar jar : allJars) {JarEntry entry = jar.jarFile.getJarEntry(path);if (entry == null) {continue;}try {URL classFileUrl = new URL("jar:file:" + jar.sourceFile.getAbsolutePath() + "!/" + path);byte[] data;try (final BufferedInputStream is = new BufferedInputStream(classFileUrl.openStream()); final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {int ch;while ((ch = is.read()) != -1) {baos.write(ch);}data = baos.toByteArray();}return processLoadedClass(defineClass(name, data, 0, data.length));} catch (IOException e) {LOGGER.error(e, "find class fail.");}}throw new ClassNotFoundException("Can't find " + name);}@Overrideprotected URL findResource(String name) {List<Jar> allJars = getAllJars();for (Jar jar : allJars) {JarEntry entry = jar.jarFile.getJarEntry(name);if (entry != null) {try {return new URL("jar:file:" + jar.sourceFile.getAbsolutePath() + "!/" + name);} catch (MalformedURLException ignored) {}}}return null;}@Overrideprotected Enumeration<URL> findResources(String name) throws IOException {List<URL> allResources = new LinkedList<>();List<Jar> allJars = getAllJars();for (Jar jar : allJars) {JarEntry entry = jar.jarFile.getJarEntry(name);if (entry != null) {allResources.add(new URL("jar:file:" + jar.sourceFile.getAbsolutePath() + "!/" + name));}}final Iterator<URL> iterator = allResources.iterator();return new Enumeration<URL>() {@Overridepublic boolean hasMoreElements() {return iterator.hasNext();}@Overridepublic URL nextElement() {return iterator.next();}};}private Class<?> processLoadedClass(Class<?> loadedClass) {final PluginConfig pluginConfig = loadedClass.getAnnotation(PluginConfig.class);if (pluginConfig != null) {// Set up the plugin config when loaded by class loader at the first time.// Agent class loader just loaded limited classes in the plugin jar(s), so the cost of this// isAssignableFrom would be also very limited.SnifferConfigInitializer.initializeConfig(pluginConfig.root());}return loadedClass;}private List<Jar> getAllJars() {if (allJars == null) {jarScanLock.lock();try {if (allJars == null) {allJars = doGetJars();}} finally {jarScanLock.unlock();}}return allJars;}private LinkedList<Jar> doGetJars() {LinkedList<Jar> jars = new LinkedList<>();for (File path : classpath) {if (path.exists() && path.isDirectory()) {String[] jarFileNames = path.list((dir, name) -> name.endsWith(".jar"));for (String fileName : jarFileNames) {try {File file = new File(path, fileName);Jar jar = new Jar(new JarFile(file), file);jars.add(jar);LOGGER.info("{} loaded.", file.toString());} catch (IOException e) {LOGGER.error(e, "{} jar file can't be resolved", fileName);}}}}return jars;}@RequiredArgsConstructorprivate static class Jar {private final JarFile jarFile;private final File sourceFile;}
}

PluginResourcesResolver 获取skywalking-plugin.def插件定义

/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements.  See the NOTICE file distributed with* this work for additional information regarding copyright ownership.* The ASF licenses this file to You under the Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with* the License.  You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.**/package org.apache.skywalking.apm.agent.core.plugin;import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
import org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader;/*** Use the current classloader to read all plugin define file. The file must be named 'skywalking-plugin.def'*/
public class PluginResourcesResolver {private static final ILog LOGGER = LogManager.getLogger(PluginResourcesResolver.class);public List<URL> getResources() {List<URL> cfgUrlPaths = new ArrayList<URL>();Enumeration<URL> urls;try {urls = AgentClassLoader.getDefault().getResources("skywalking-plugin.def");while (urls.hasMoreElements()) {URL pluginUrl = urls.nextElement();cfgUrlPaths.add(pluginUrl);LOGGER.info("find skywalking plugin define in {}", pluginUrl);}return cfgUrlPaths;} catch (IOException e) {LOGGER.error("read resources failure.", e);}return null;}
}

相关文章:

agent扩展-自定义外部加载路径

自定义classLoader实现加载外部jar, 以skywalking agent 类加载器为例子 整体思路 扩展findClass &#xff0c;解决loadClass可以查找到扩展findResource&#xff0c;解决getResources可以获取到资源 基本原理 ClassLoader loadClass的加载顺序 findLoadedClass 加载本地已经…...

Elasticsearch使用篇 - 指标聚合

指标聚合 指标聚合从聚合文档中提取出指标进行计算。可以从文档的字段或者使用脚本方式进行提取。 聚合统计可以同时返回明细数据&#xff0c;可以分页查询&#xff0c;可以返回总数量。 可以结合查询条件&#xff0c;限制数据范围&#xff0c;结合倒排索引列式存储。 指标…...

Python生命周期及内存管理

文章目录 一、Python的生命周期 1、概念2、如何监听生命周期二、内存管理 1.存储2.垃圾回收3.引用计数一、生命周期&#xff1a; 1、概念&#xff1a;一个对象从创建到消亡的过程 当一个对象呗创建是&#xff0c;会在内存中分配响应的内存空间进行存储 当这个对象不再使…...

Elasticsearch7.8.0版本进阶——数据写流程

目录一、数据写流程概述二、数据写流程步骤2.1、数据写流程图2.2、数据写流程步骤&#xff08;新建索引和删除文档所需要的步骤顺序&#xff09;2.3、数据写流程的请求参数一、数据写流程概述 新建、删除索引和新建、删除文档的请求都是写操作&#xff0c; 必须在主分片上面完…...

化学试剂Glutaric Acid-PEG-Glutaric Acid,GA-PEG-GA,戊二酸-聚乙二醇-戊二酸

一&#xff1a;产品描述 1、名称 英文&#xff1a;Glutaric Acid-PEG-Glutaric Acid&#xff0c;GA-PEG-GA 中文&#xff1a;戊二酸-聚乙二醇-戊二酸 2、CAS编号&#xff1a;N/A 3、所属分类&#xff1a;Carboxylic acid PEG 4、分子量&#xff1a;可定制&#xff0c; 戊…...

知识图谱业务落地技术推荐之国内知识图谱平台汇总(竞品)[阿里、腾讯、华为等】

各位可以参考国内知识图谱平台产品进行对技术链路搭建和产品参考提供借鉴。...

ABC 289 G - Shopping in AtCoder store 数学推导+凸包

大意&#xff1a; n个顾客&#xff0c;每个人有一个购买的欲望bi,m件物品&#xff0c;每一件物品有一个价值ci,每一个顾客会买商品当且仅当bici>定价. 现在要求对每一个商品定价&#xff0c;求出它的最大销售值&#xff08;数量*定价&#xff09; n,m<2e5 思路&#x…...

ARM Linux 如何在sysfs用户态命令行中控制 GPIO 引脚?

ARM Linux 如何在sysfs用户态命令行中控制 GPIO 引脚&#xff1f;我们在开发工作中&#xff0c;经常需要确定内核gpio驱动&#xff0c;是否有异常&#xff0c;或者在没有应用的情况下&#xff0c;像控制某个外设&#xff0c;这时我们就可以在控制台命令行中&#xff0c;用命令导…...

【Linux】生产者消费者模型 - 详解

目录 一.生产者消费者模型概念 1.为何要使用生产者消费者模型 2.生产者消费者之间的关系 3.生产者消费者模型的优点 二.基于阻塞队列的生产消费模型 1.在阻塞队列中的三种关系 2.BlockingQueue.hpp - 阻塞队列类 3.LockGurad.hpp - RAII互斥锁类 4.Task.hpp - 在阻塞队…...

源码深度解析Spring Bean的加载

在应用spring 的过程中&#xff0c;就会涉及到bean的加载&#xff0c;bean的加载经历一个相当复杂的过程&#xff0c;bean的加载入口如下&#xff1a; 使用getBean&#xff08;&#xff09;方法进行加载Bean&#xff0c;最终调用的是AbstractBeanFactory.doGetBean() 进行Bean的…...

STL——priority_queue

一、priority_queue介绍及使用 1.priority_queue文档介绍 &#xff08;1&#xff09;优先队列是一种容器适配器&#xff0c;根据严格的弱排序标准&#xff0c;它的第一个元素总是它所包含的元素中最大的。 &#xff08;2&#xff09;此上下文类似与堆&#xff0c;在堆中可以…...

Springboot集成工作流Activity

介绍 官网&#xff1a;https://www.activiti.org/ 一 、工作流介绍 1.工作流&#xff08;workflow&#xff09; 就是通过计算机对业务流程自动化执行管理&#xff0c;它主要解决的是“使在多个参与这之间按照某种预定义规则自动化进行传递文档、信息或任务的过程&#xff0c…...

2023软件测试工程师涨薪攻略,3年如何达到月薪30K?

1.软件测试如何实现涨薪 首先涨薪并不是从8000涨到9000这种涨薪&#xff0c;而是从8000涨到15K加到25K的涨薪。基本上三年之内就可以实现。 如果我们只是普通的有应届毕业生或者是普通本科那我们就只能从小公司开始慢慢往上走。 有些同学想去做测试&#xff0c;是希望能够日…...

Java面试——Spring Bean相关知识

目录 1.Bean的定义 2.Bean的生命周期 3.BeanFactory及Factory Bean 4.Bean的作用域 5.Bean的线程安全问题 1.Bean的定义 JavaBean是描述Java的软件组件模型。在Java模型中&#xff0c;通过JavaBean可以无限扩充Java程序的功能&#xff0c;通过JavaBean的组合可以快速的生…...

上班在群里摸鱼,逮到一个字节8年测试开发,聊过之后羞愧难当...

老话说的好&#xff0c;这人呐&#xff0c;一旦在某个领域鲜有敌手了&#xff0c;就会闲得某疼。前几天我在上班摸鱼刷群的时候认识了一位字节测试开发大佬&#xff0c;在字节工作了8年&#xff0c;因为本人天赋比较高&#xff0c;平时工作也兢兢业业&#xff0c;现在企业内有一…...

HTTP、WebSocket和Socket.IO

一、HTTP协议 HTTP协议是Hyper Text Transfer Protocol&#xff08;超文本传输协议&#xff09;。HTTP 协议和 TCP/IP 协议族内的其他众多的协议相同&#xff0c; 用于客户端和服务器之间的通信。请求访问文本或图像等资源的一端称为客户端&#xff0c; 而提供资源响应的一端称…...

Fluent Python 笔记 第 11 章 接口:从协议到抽象基类

本章讨论的话题是接口:从鸭子类型的代表特征动态协议&#xff0c;到使接口更明确、能验证实现是否符合规定的抽象基类(Abstract Base Class&#xff0c;ABC)。 11.1 Python 文化中的接口和协议 对 Python 程序员来说&#xff0c;“X 类对象”“X 协 议”和“X 接口”都是一个…...

【Spark分布式内存计算框架——Spark Core】11. Spark 内核调度(下)

8.5 Spark 基本概念 Spark Application运行时&#xff0c;涵盖很多概念&#xff0c;主要如下表格&#xff1a; 官方文档&#xff1a;http://spark.apache.org/docs/2.4.5/cluster-overview.html#glossary Application&#xff1a;指的是用户编写的Spark应用程序/代码&#x…...

Java中的函数

1.String.trim() : 主要有2个用法&#xff1a; 1、就是去掉字符串中前后的空白&#xff1b;这个方法的主要可以使用在判断用户输入的密码之类的。 2、它不仅可以去除空白&#xff0c;还可以去除字符串中的制表符&#xff0c;如 ‘\t’,\n等。 2.Integer.parseInt() : 字符串…...

实验6-霍纳法则及变治技术

目录 1.霍纳法则(Horners rule) 2.堆排序 3.求a的n次幂 1.霍纳法则(Horners rule) 【问题描述】用霍纳法则求一个多项式在一个给定点的值 【输入形式】输入三行,第一行是一个整数n,表示的是多项式的最高次数;第二行多项式的系数组P[0...n](从低到高存储);第三行是…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

【kafka】Golang实现分布式Masscan任务调度系统

要求&#xff1a; 输出两个程序&#xff0c;一个命令行程序&#xff08;命令行参数用flag&#xff09;和一个服务端程序。 命令行程序支持通过命令行参数配置下发IP或IP段、端口、扫描带宽&#xff0c;然后将消息推送到kafka里面。 服务端程序&#xff1a; 从kafka消费者接收…...

【Oracle APEX开发小技巧12】

有如下需求&#xff1a; 有一个问题反馈页面&#xff0c;要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据&#xff0c;方便管理员及时处理反馈。 我的方法&#xff1a;直接将逻辑写在SQL中&#xff0c;这样可以直接在页面展示 完整代码&#xff1a; SELECTSF.FE…...

iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版​分享

平时用 iPhone 的时候&#xff0c;难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵&#xff0c;或者买了二手 iPhone 却被原来的 iCloud 账号锁住&#xff0c;这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...

测试markdown--肇兴

day1&#xff1a; 1、去程&#xff1a;7:04 --11:32高铁 高铁右转上售票大厅2楼&#xff0c;穿过候车厅下一楼&#xff0c;上大巴车 &#xffe5;10/人 **2、到达&#xff1a;**12点多到达寨子&#xff0c;买门票&#xff0c;美团/抖音&#xff1a;&#xffe5;78人 3、中饭&a…...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

leetcodeSQL解题:3564. 季节性销售分析

leetcodeSQL解题&#xff1a;3564. 季节性销售分析 题目&#xff1a; 表&#xff1a;sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现录音机应用

1. 项目配置与权限设置 1.1 配置module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.MICROPHONE","reason": "录音需要麦克风权限"},{"name": "ohos.permission.WRITE…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

智能仓储的未来:自动化、AI与数据分析如何重塑物流中心

当仓库学会“思考”&#xff0c;物流的终极形态正在诞生 想象这样的场景&#xff1a; 凌晨3点&#xff0c;某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径&#xff1b;AI视觉系统在0.1秒内扫描包裹信息&#xff1b;数字孪生平台正模拟次日峰值流量压力…...