APM - 使用JavaAgent+Javassit 插桩C3P0
生活随笔
收集整理的這篇文章主要介紹了
APM - 使用JavaAgent+Javassit 插桩C3P0
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 核心思想
- 編碼實現(xiàn)
- 打包
- 配置驗證
核心思想
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ......很熟悉吧,我們要插樁的這個對象就是 ComboPooledDataSource ,
如何很直觀的展示出來的
實現(xiàn)第一個,那就在ComboPooledDataSource構(gòu)造函數(shù)后下手,將ComboPooledDataSource放到System Properties 中, 然后開啟一個HTTP服務(wù),對外提供服務(wù)訪問即可。
編碼實現(xiàn)
package com.artisan.agent.collect.c3p0;import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import javassist.ClassPool; import javassist.CtClass; import javassist.LoaderClassPath;import java.io.IOException; import java.io.OutputStream; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; import java.net.InetSocketAddress; import java.security.ProtectionDomain; import java.util.concurrent.Executors;/*** @author 小工匠* @version 1.0* @description: C3P0插樁* @date 2020/8/29 9:15* @mark: show me the code , change the world*/ public class C3P0Agent {// 要插裝的類static String targetClass = "com.mchange.v2.c3p0.ComboPooledDataSource";public static void premain(String args , Instrumentation instrumentation){// 類轉(zhuǎn)換器處理插樁邏輯instrumentation.addTransformer(new ClassFileTransformer() {public byte[] transform(ClassLoader loader,String className,Class<?> classBeingRedefined,ProtectionDomain protectionDomain,byte[] classfileBuffer) {// 插樁后的對象byte[] result = null;// 條件判斷if (className != null && className.replace("/", ".").equals(targetClass)){// 實例化pool , 將當(dāng)前的ClassLoader設(shè)置到poolClassPool pool = new ClassPool();pool.insertClassPath(new LoaderClassPath(loader));try {// 獲取目標(biāo)對象CtClass ctl = pool.get(targetClass);// 獲取構(gòu)造函數(shù),插樁 將對象放入系統(tǒng)屬性中ctl.getConstructor("()V") //構(gòu)造函數(shù).insertAfter("System.getProperties().put(\"c3p0Source$agent\", $0);"); // $0 this本身// 轉(zhuǎn)成classresult = ctl.toBytecode();// 暴漏HTTP服務(wù)new C3P0Agent().openHttpServer();} catch (Exception e) {e.printStackTrace();}}return result;}});}/*** 對外提供Http 服務(wù)展示DataSource當(dāng)前狀態(tài)* @throws IOException*/private void openHttpServer() throws IOException {InetSocketAddress addr = new InetSocketAddress(7777);HttpServer server = HttpServer.create(addr, 0);// 設(shè)置上下文server.createContext("/", new MyHttpHandler());server.setExecutor(Executors.newCachedThreadPool());server.start();}private class MyHttpHandler implements HttpHandler {@Overridepublic void handle(HttpExchange exchange) throws IOException {Headers responseHeaders = exchange.getResponseHeaders();responseHeaders.set("Content-Type", "text/plain;charset=UTF-8");exchange.sendResponseHeaders(200, 0);OutputStream responseBody = exchange.getResponseBody();// 輸出c3p0狀態(tài)responseBody.write(C3P0Agent.this.getStatus().getBytes());responseBody.flush();responseBody.close();}}public String getStatus() {Object source2 = System.getProperties().get("c3p0Source$agent");if (source2 == null) {return "未初始任何c3p0數(shù)據(jù)源";}return source2.toString();} }打包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>artisan-apm</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>apm-agent</artifactId><dependencies><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.18.1-GA</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.32</version><scope>provided</scope></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.2</version><configuration><archive><manifestEntries><Project-name>${project.name}</Project-name><Project-version>${project.version}</Project-version><Boot-Class-Path>javassist-3.18.1-GA.jar</Boot-Class-Path><Premain-Class>com.artisan.agent.collect.c3p0.C3P0Agent</Premain-Class><Can-Redefine-Classes>true</Can-Redefine-Classes><Can-Retransform-Classes>true</Can-Retransform-Classes></manifestEntries></archive><skip>true</skip></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><artifactSet><includes><include>org.javassist:javassist</include></includes></artifactSet><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.artisan.agent.collect.c3p0.C3P0Agent</mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins></build></project>打包的時候,連同javassist也打包進去
配置驗證
在啟動的JVM參數(shù)中配置如下信息
-javaagent:D:\IdeaProjects\artisan-apm\apm-agent\target\apm-agent-1.0-SNAPSHOT.jar然后,訪問 http:// localhost:7777
總結(jié)
以上是生活随笔為你收集整理的APM - 使用JavaAgent+Javassit 插桩C3P0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell - 通过hook文件后门实现
- 下一篇: IDEA 启动 Tomcat 乱码 解决