From 6ec5bca5e8022985d9a1e40d304f35ec2ca46b99 Mon Sep 17 00:00:00 2001 From: "yangminglang816@163.com" Date: Thu, 2 Nov 2017 11:45:45 +0800 Subject: [PATCH] 1.For springboot, add EVENT BEFORE_BEAN_REGIST to perform bytecode rewriting before spring's bean regist. 2.For springboot, do component profile after ApplicationContext's finishRefresh method. 3.Change InterceptContext's storage mode from ThreadLocal to alibaba's TTL. 4.Add isHookEventDone method to class HookProxy, ensure each operation event be called exactly once. 5.repair the defect that request couldn't be mapped to custom App 'com.creditease.uav' in tomcat6 --- .../uav/hook/dubbo/DubboHookProxy.java | 4 + .../async/HttpAsyncClientHookProxy.java | 5 + .../httpclients/sync/HttpClientHookProxy.java | 5 + .../sync/HttpClient3HookProxy.java | 5 + .../uav/hook/jaxws/JaxWSHookProxy.java | 7 +- .../uav/hook/jdbc/JdbcHookProxy.java | 33 ++---- .../hook/jdbc/pools/AbsDBPoolHookProxy.java | 4 + .../hook/jdbc/pools/dbcp/DBCPHookProxy.java | 12 +- .../jdbc/pools/hikari/HikariHookProxy.java | 10 +- .../jdbc/pools/mybatis/MybatisHookProxy.java | 14 +-- .../mongoclients/MongoClientHookProxy.java | 10 +- .../uav/hook/rabbitmq/RabbitmqHookProxy.java | 5 + .../uav/hook/rocketmq/RocketmqHookProxy.java | 5 + .../hook/redis/aredis/AredisHookProxy.java | 5 + .../uav/hook/redis/jedis/JedisHookProxy.java | 5 + .../hook/redis/lettuce/LettuceHookProxy.java | 5 + .../adaptors/SpringBootTomcatAdaptor.java | 104 +++++++----------- .../listeners/WebServiceListener.java | 16 ++- .../pom.xml | 24 +++- .../fat/dubbo/IMyDubboService.java | 32 ++++++ .../fat/dubbo/MyDubboService.java | 48 ++++++++ .../springbootFat/CxfConfig.java | 44 ++++---- .../src/main/java/config.xml | 10 -- .../application.properties | 0 .../src/main/resources/config.xml | 43 ++++++++ .../monitor/appfra/hook/spi/HookProxy.java | 16 +++ .../StandardInterceptContextHelper.java | 3 +- .../spi/InterceptContext.java | 2 +- .../listeners/AppFrkHookFactoryListener.java | 3 + .../interceptor/SpringBootTomcatPlusIT.java | 58 +++++++--- .../tomcat/plus/interceptor/TomcatPlusIT.java | 4 +- 31 files changed, 370 insertions(+), 171 deletions(-) create mode 100644 com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/IMyDubboService.java create mode 100644 com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/MyDubboService.java delete mode 100644 com.creditease.uav.monitorframework.springbootFat/src/main/java/config.xml rename com.creditease.uav.monitorframework.springbootFat/src/main/{java => resources}/application.properties (100%) create mode 100644 com.creditease.uav.monitorframework.springbootFat/src/main/resources/config.xml diff --git a/com.creditease.uav.hook.dubbo/src/main/java/com/creditease/uav/hook/dubbo/DubboHookProxy.java b/com.creditease.uav.hook.dubbo/src/main/java/com/creditease/uav/hook/dubbo/DubboHookProxy.java index 3fcc6230..e53f386c 100644 --- a/com.creditease.uav.hook.dubbo/src/main/java/com/creditease/uav/hook/dubbo/DubboHookProxy.java +++ b/com.creditease.uav.hook.dubbo/src/main/java/com/creditease/uav/hook/dubbo/DubboHookProxy.java @@ -51,6 +51,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); break; @@ -69,6 +70,9 @@ public void start(HookContext context, ClassLoader webapploader) { private void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/async/HttpAsyncClientHookProxy.java b/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/async/HttpAsyncClientHookProxy.java index 96ec06db..66c979dd 100644 --- a/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/async/HttpAsyncClientHookProxy.java +++ b/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/async/HttpAsyncClientHookProxy.java @@ -57,6 +57,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); break; @@ -76,6 +77,10 @@ public void start(HookContext context, ClassLoader webapploader) { public void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/sync/HttpClientHookProxy.java b/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/sync/HttpClientHookProxy.java index 8a80a577..b7197b45 100644 --- a/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/sync/HttpClientHookProxy.java +++ b/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients/sync/HttpClientHookProxy.java @@ -56,6 +56,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); break; @@ -76,6 +77,10 @@ public void start(HookContext context, ClassLoader webapploader) { public void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients3/sync/HttpClient3HookProxy.java b/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients3/sync/HttpClient3HookProxy.java index 031e84aa..de0149d0 100644 --- a/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients3/sync/HttpClient3HookProxy.java +++ b/com.creditease.uav.hook.httpclients/src/main/java/com/creditease/uav/hook/httpclients3/sync/HttpClient3HookProxy.java @@ -56,6 +56,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_RESOURCE_INIT: break; case WEBCONTAINER_INIT: @@ -76,6 +77,10 @@ public void start(HookContext context, ClassLoader webapploader) { public void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); String basePath = (String) ic.get(InterceptConstants.BASEPATH); diff --git a/com.creditease.uav.hook.jaxws/src/main/java/com/creditease/uav/hook/jaxws/JaxWSHookProxy.java b/com.creditease.uav.hook.jaxws/src/main/java/com/creditease/uav/hook/jaxws/JaxWSHookProxy.java index b66b65cc..3420e959 100644 --- a/com.creditease.uav.hook.jaxws/src/main/java/com/creditease/uav/hook/jaxws/JaxWSHookProxy.java +++ b/com.creditease.uav.hook.jaxws/src/main/java/com/creditease/uav/hook/jaxws/JaxWSHookProxy.java @@ -51,6 +51,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event event = context.get(Event.class); switch (event) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: insertIntercepter(context, webapploader); break; @@ -70,6 +71,10 @@ public void start(HookContext context, ClassLoader webapploader) { private void insertIntercepter(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); @@ -87,7 +92,7 @@ public void doInstallDProxy(ClassLoader webapploader, final String appid) { dpInstall.setTargetClassLoader(webapploader); /** - * install proxy to InternalHttpAsyncClient + * install proxy to javax.xml.ws.Service */ dpInstall.installProxy("javax.xml.ws.Service", new String[] { "com.creditease.uav.hook.jaxws.interceptors" }, new DynamicProxyProcessor() { diff --git a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/JdbcHookProxy.java b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/JdbcHookProxy.java index 332c0715..cd946e3c 100644 --- a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/JdbcHookProxy.java +++ b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/JdbcHookProxy.java @@ -44,17 +44,9 @@ public class JdbcHookProxy extends HookProxy { private final DynamicProxyInstaller dpInstall; - private boolean isInjectDrvMgr = false; - private boolean isInjectDruid = false; - private boolean isInjectDBCP2 = false; - private boolean isInjectTomcatDBCP2 = false; - private boolean isInjectHikari = false; - private boolean isInjectDataSource = false; - @SuppressWarnings("rawtypes") public JdbcHookProxy(String id, Map config) { super(id, config); - dpInstall = new DynamicProxyInstaller(); } @@ -64,6 +56,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event event = context.get(Event.class); InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); switch (event) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_RESOURCE_INIT: this.injectDriverManager(webapploader, ic); this.injectDBCP2(webapploader, ic); @@ -113,12 +106,10 @@ private String getAppID(InterceptContext ic) { */ private void injectDriverManager(ClassLoader webapploader, InterceptContext ic) { - if (this.isInjectDrvMgr == true) { + if (isHookEventDone("isInjectDrvMgr")) { return; } - this.isInjectDrvMgr = true; - JdbcDriverIT jdbcDriverIT = new JdbcDriverIT(this.getAppID(ic)); jdbcDriverIT.initSomeDrivers(webapploader); @@ -149,12 +140,10 @@ private void injectDataSource(InterceptContext ic) { return; } - if (this.isInjectDataSource == true) { + if (isHookEventDone("isInjectDataSource")) { return; } - this.isInjectDataSource = true; - JdbcDriverIT jdbcDriverIT = new JdbcDriverIT(this.getAppID(ic)); ServerVendor vendor = (ServerVendor) this.getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR); @@ -188,12 +177,10 @@ private void injectTomcatDBCP2(ClassLoader webapploader, InterceptContext ic) { return; } - if (this.isInjectTomcatDBCP2 == true) { + if (isHookEventDone("isInjectTomcatDBCP2")) { return; } - this.isInjectTomcatDBCP2 = true; - final String appid = this.getAppID(ic); /** @@ -242,12 +229,10 @@ private void injectDBCP2(ClassLoader webapploader, InterceptContext ic) { return; } - if (this.isInjectDBCP2 == true) { + if (isHookEventDone("isInjectDBCP2")) { return; } - this.isInjectDBCP2 = true; - final String appid = this.getAppID(ic); /** @@ -294,12 +279,10 @@ private void injectHikari(ClassLoader webapploader, InterceptContext ic) { return; } - if (isInjectHikari == true) { + if (isHookEventDone("isInjectHikari")) { return; } - isInjectHikari = true; - final String appid = this.getAppID(ic); final String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); @@ -357,12 +340,10 @@ private void injectDruid(ClassLoader webapploader, InterceptContext ic) { return; } - if (this.isInjectDruid == true) { + if (isHookEventDone("isInjectDruid")) { return; } - this.isInjectDruid = true; - final String appid = this.getAppID(ic); /** diff --git a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/AbsDBPoolHookProxy.java b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/AbsDBPoolHookProxy.java index 9460cb88..1ee192d6 100644 --- a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/AbsDBPoolHookProxy.java +++ b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/AbsDBPoolHookProxy.java @@ -56,6 +56,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event event = context.get(Event.class); InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); switch (event) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: initHook(ic); break; @@ -89,6 +90,9 @@ public void start(HookContext context, ClassLoader webapploader) { */ private void initHook(InterceptContext ic) { + if (isHookEventDone("initHook")) { + return; + } String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); String basePath = (String) ic.get(InterceptConstants.BASEPATH); appid = MonitorServerUtil.getApplicationId(contextPath, basePath); diff --git a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/dbcp/DBCPHookProxy.java b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/dbcp/DBCPHookProxy.java index f8354e56..c2f88042 100644 --- a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/dbcp/DBCPHookProxy.java +++ b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/dbcp/DBCPHookProxy.java @@ -46,8 +46,6 @@ public class DBCPHookProxy extends AbsDBPoolHookProxy { protected DynamicProxyInstaller dpInstall; - private boolean isInit = false; - @SuppressWarnings("rawtypes") public DBCPHookProxy(String id, Map config) { super(id, config); @@ -62,6 +60,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event event = context.get(Event.class); switch (event) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_RESOURCE_INIT: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); @@ -93,16 +92,15 @@ public void start(HookContext context, ClassLoader webapploader) { */ private void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); String basePath = (String) ic.get(InterceptConstants.BASEPATH); appid = MonitorServerUtil.getApplicationId(contextPath, basePath); - if (isInit == true) { - return; - } - - isInit = true; /** * set the webapploader is the target classloader */ diff --git a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/hikari/HikariHookProxy.java b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/hikari/HikariHookProxy.java index 8bdee82f..2dd5ca39 100644 --- a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/hikari/HikariHookProxy.java +++ b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/hikari/HikariHookProxy.java @@ -58,6 +58,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event event = context.get(Event.class); switch (event) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_RESOURCE_INIT: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); @@ -89,16 +90,15 @@ public void start(HookContext context, ClassLoader webapploader) { */ private void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); String basePath = (String) ic.get(InterceptConstants.BASEPATH); appid = MonitorServerUtil.getApplicationId(contextPath, basePath); - if (isInit == true) { - return; - } - - isInit = true; // /** // * set the webapploader is the target classloader // */ diff --git a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/mybatis/MybatisHookProxy.java b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/mybatis/MybatisHookProxy.java index c2e6eef5..d8b2c772 100644 --- a/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/mybatis/MybatisHookProxy.java +++ b/com.creditease.uav.hook.jdbc/src/main/java/com/creditease/uav/hook/jdbc/pools/mybatis/MybatisHookProxy.java @@ -49,7 +49,6 @@ public class MybatisHookProxy extends AbsDBPoolHookProxy { protected DynamicProxyInstaller dpInstall; private ClassLoader webapploaderForMybatis = null; - private boolean isInit = false; @SuppressWarnings("rawtypes") public MybatisHookProxy(String id, Map config) { @@ -65,6 +64,7 @@ public void start(HookContext context, ClassLoader webapploader) { webapploaderForMybatis = webapploader; Event event = context.get(Event.class); switch (event) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_RESOURCE_INIT: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); @@ -111,17 +111,15 @@ protected void collectDBPoolMetrics(MonitorElement clientElem) { private void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); String basePath = (String) ic.get(InterceptConstants.BASEPATH); appid = MonitorServerUtil.getApplicationId(contextPath, basePath); - if (isInit == true) { - return; - } - - isInit = true; - dpInstall.setTargetClassLoader(webapploader); dpInstall.installProxy("org.apache.ibatis.datasource.pooled.PooledDataSource", @@ -157,7 +155,7 @@ private void collectDataSourceStat(MonitorElementInstance inst, DataSource pds, ReflectHelper.invoke(className, pds, prefix + collectMtrx[i], null, null, webapploader)); } - Object poolState = ReflectHelper.invoke(className, pds, "getPoolState", null, null,webapploader); + Object poolState = ReflectHelper.invoke(className, pds, "getPoolState", null, null, webapploader); for (; i < collectMtrx.length; i++) { diff --git a/com.creditease.uav.hook.mongoclients/src/main/java/com/creditease/uav/hook/mongoclients/MongoClientHookProxy.java b/com.creditease.uav.hook.mongoclients/src/main/java/com/creditease/uav/hook/mongoclients/MongoClientHookProxy.java index 8ee927f3..31cf3688 100644 --- a/com.creditease.uav.hook.mongoclients/src/main/java/com/creditease/uav/hook/mongoclients/MongoClientHookProxy.java +++ b/com.creditease.uav.hook.mongoclients/src/main/java/com/creditease/uav/hook/mongoclients/MongoClientHookProxy.java @@ -22,11 +22,9 @@ import java.util.Map; -import com.creditease.monitor.UAVServer; import com.creditease.monitor.appfra.hook.spi.HookConstants; import com.creditease.monitor.appfra.hook.spi.HookContext; import com.creditease.monitor.appfra.hook.spi.HookProxy; -import com.creditease.monitor.captureframework.spi.CaptureConstants; import com.creditease.monitor.interceptframework.spi.InterceptConstants; import com.creditease.monitor.interceptframework.spi.InterceptContext; import com.creditease.monitor.interceptframework.spi.InterceptContext.Event; @@ -59,6 +57,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); break; @@ -78,14 +77,9 @@ public void start(HookContext context, ClassLoader webapploader) { public void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { - /** - * NOTE: this is a special process for springboot, because the MongoClient hook happens in transform - */ - if (UAVServer.ServerVendor.SPRINGBOOT == UAVServer.instance() - .getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR)) { + if (isHookEventDone("InsertInterceptToClients")) { return; } - InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rabbitmq/RabbitmqHookProxy.java b/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rabbitmq/RabbitmqHookProxy.java index ae90ae03..bec0a692 100644 --- a/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rabbitmq/RabbitmqHookProxy.java +++ b/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rabbitmq/RabbitmqHookProxy.java @@ -57,6 +57,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); break; @@ -76,6 +77,10 @@ public void start(HookContext context, ClassLoader webapploader) { public void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rocketmq/RocketmqHookProxy.java b/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rocketmq/RocketmqHookProxy.java index 2dc1f78a..085ae525 100644 --- a/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rocketmq/RocketmqHookProxy.java +++ b/com.creditease.uav.hook.mq/src/main/java/com/creditease/uav/hook/rocketmq/RocketmqHookProxy.java @@ -56,6 +56,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: InsertInterceptToClients(context, webapploader); break; @@ -75,6 +76,10 @@ public void start(HookContext context, ClassLoader webapploader) { public void InsertInterceptToClients(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("InsertInterceptToClients")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/aredis/AredisHookProxy.java b/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/aredis/AredisHookProxy.java index 5a68edf3..438d6ea7 100644 --- a/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/aredis/AredisHookProxy.java +++ b/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/aredis/AredisHookProxy.java @@ -48,6 +48,10 @@ public AredisHookProxy(String id, @SuppressWarnings("rawtypes") Map config) { protected void insertIntercepter(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("insertIntercepter")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); @@ -96,6 +100,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: insertIntercepter(context, webapploader); break; diff --git a/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/jedis/JedisHookProxy.java b/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/jedis/JedisHookProxy.java index 22ea72d7..c9e7a8db 100644 --- a/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/jedis/JedisHookProxy.java +++ b/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/jedis/JedisHookProxy.java @@ -50,6 +50,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: insertIntercepter(context, webapploader); break; @@ -90,6 +91,10 @@ public void stop(HookContext context, ClassLoader webapploader) { protected void insertIntercepter(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("insertIntercepter")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); diff --git a/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/lettuce/LettuceHookProxy.java b/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/lettuce/LettuceHookProxy.java index 77044e8b..0f5941b7 100644 --- a/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/lettuce/LettuceHookProxy.java +++ b/com.creditease.uav.hook.redis/src/main/java/com/creditease/uav/hook/redis/lettuce/LettuceHookProxy.java @@ -46,6 +46,10 @@ public LettuceHookProxy(String id, @SuppressWarnings("rawtypes") Map config) { protected void insertIntercepter(HookContext context, ClassLoader webapploader) { + if (isHookEventDone("insertIntercepter")) { + return; + } + InterceptContext ic = (InterceptContext) context.get(HookConstants.INTERCEPTCONTEXT); String contextPath = (String) ic.get(InterceptConstants.CONTEXTPATH); @@ -94,6 +98,7 @@ public void start(HookContext context, ClassLoader webapploader) { Event evt = context.get(Event.class); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_INIT: insertIntercepter(context, webapploader); break; diff --git a/com.creditease.uav.monitorframework.agent/src/main/java/com/creditease/uav/monitorframework/adaptors/SpringBootTomcatAdaptor.java b/com.creditease.uav.monitorframework.agent/src/main/java/com/creditease/uav/monitorframework/adaptors/SpringBootTomcatAdaptor.java index 695c3b73..2a6632de 100644 --- a/com.creditease.uav.monitorframework.agent/src/main/java/com/creditease/uav/monitorframework/adaptors/SpringBootTomcatAdaptor.java +++ b/com.creditease.uav.monitorframework.agent/src/main/java/com/creditease/uav/monitorframework/adaptors/SpringBootTomcatAdaptor.java @@ -89,55 +89,22 @@ public byte[] onLoadClass(ClassLoader clsLoader, String uavMofRoot, String class final AbstractAdaptor aa = this; - /* - * Springboot's AutoConfig would load MongoClient before webContainer started, so the hook should be done in - * transform - */ - if ("com.mongodb.MongoClient".equals(className)) { - hookJarMap = getHookJarMap(); - - String jarFileLoc = hookJarMap.get("com.mongodb.Mongo"); - - try { - installHookJars(clsLoader, jarFileLoc, uavMofRoot); - } - catch (Exception e) { - e.printStackTrace(); - return null; - } - return this.inject(className, new String[] { "com.creditease.uav.hook.mongoclients.interceptors" }, - new AdaptorProcessor() { - - @Override - public void process(CtMethod m) throws Exception { - - aa.addLocalVar(m, "mObj", - "com.creditease.uav.hook.mongoclients.interceptors.MongoClientIT"); - m.insertAfter("{mObj=new MongoClientIT(\"" + getAppid() + "\");$_=mObj.doInstall($_);}"); - - } - - @Override - public String getMethodName() { - - return "getDatabase"; - } - - }); - - } - else if (className.equals("org.springframework.context.support.AbstractApplicationContext")) + if (className.equals("org.springframework.context.support.AbstractApplicationContext")) { return this.inject(className, new String[] { "com.creditease.tomcat.plus.interceptor" }, new AdaptorProcessor() { + /** + * we need startServer before ApplicationContext's refresh cause some hook operation could + * happen when refresh. + */ @Override public void process(CtMethod m) throws Exception { aa.addLocalVar(m, "mObj", "com.creditease.tomcat.plus.interceptor.SpringBootTomcatPlusIT"); m.insertBefore( - "{mObj=new SpringBootTomcatPlusIT();mObj.setAppid(this.getEnvironment().getProperty(\"server.context-path\"));}"); + "{mObj=new SpringBootTomcatPlusIT();mObj.startServer(this.getEnvironment().getProperty(\"server.port\"),this.getEnvironment().getProperty(\"server.context-path\"));}"); } @Override @@ -148,28 +115,7 @@ public String getMethodName() { }); } - else if (className.equals("org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer")) { - - return this.inject(className, new String[] { "com.creditease.tomcat.plus.interceptor" }, - new AdaptorProcessor() { - - @Override - public void process(CtMethod m) throws Exception { - - aa.addLocalVar(m, "mObj", "com.creditease.tomcat.plus.interceptor.SpringBootTomcatPlusIT"); - m.insertBefore( - "{mObj=new SpringBootTomcatPlusIT();mObj.startServer(this.tomcat.getService().findConnectors()[0].getPort());}"); - } - - @Override - public String getMethodName() { - - return "initialize"; - } - }); - - } else if (className.equals("org.apache.catalina.core.StandardEngineValve")) { return this.inject(className, new String[] { "com.creditease.tomcat.plus.interceptor" }, new AdaptorProcessor() { @@ -339,15 +285,43 @@ public void process(CtMethod m) throws Exception { } }); } + else if (className.equals("org.springframework.boot.context.embedded.EmbeddedWebApplicationContext")) { + return this.inject(className, new String[] { "com.creditease.tomcat.plus.interceptor" }, + new AdaptorProcessor[] { new AdaptorProcessor() { - return null; - } + @Override + public String getMethodName() { - private String getAppid() { + return "finishRefresh"; + } + + @Override + public void process(CtMethod m) throws Exception { - return System.getProperty("com.creditease.uav.springboot.appid") == null ? "" - : System.getProperty("com.creditease.uav.springboot.appid"); + aa.addLocalVar(m, "mObj", "com.creditease.tomcat.plus.interceptor.SpringBootTomcatPlusIT"); + m.insertAfter("{mObj=new SpringBootTomcatPlusIT();mObj.onSpringFinishRefresh();}"); + } + + }, new AdaptorProcessor() { + @Override + public String getMethodName() { + + return "postProcessBeanFactory"; + } + + @Override + public void process(CtMethod m) throws Exception { + + aa.addLocalVar(m, "mObj", "com.creditease.tomcat.plus.interceptor.SpringBootTomcatPlusIT"); + m.insertBefore( + "{mObj=new SpringBootTomcatPlusIT();mObj.onSpringBeanRegist(this.getEnvironment().getProperty(\"server.context-path\"));}"); + } + + } }); + } + + return null; } } diff --git a/com.creditease.uav.monitorframework.dproxy/src/main/java/com/creditease/uav/monitorframework/webservice/listeners/WebServiceListener.java b/com.creditease.uav.monitorframework.dproxy/src/main/java/com/creditease/uav/monitorframework/webservice/listeners/WebServiceListener.java index 6c76d4f8..9250c134 100644 --- a/com.creditease.uav.monitorframework.dproxy/src/main/java/com/creditease/uav/monitorframework/webservice/listeners/WebServiceListener.java +++ b/com.creditease.uav.monitorframework.dproxy/src/main/java/com/creditease/uav/monitorframework/webservice/listeners/WebServiceListener.java @@ -22,6 +22,8 @@ import java.util.List; +import com.creditease.monitor.UAVServer; +import com.creditease.monitor.captureframework.spi.CaptureConstants; import com.creditease.monitor.interceptframework.spi.InterceptConstants; import com.creditease.monitor.interceptframework.spi.InterceptContext; import com.creditease.monitor.interceptframework.spi.InterceptContext.Event; @@ -47,8 +49,14 @@ public WebServiceListener() { public boolean isEventListener(Event event) { switch (event) { - case WEBCONTAINER_INIT: + case SPRING_BEAN_REGIST: return true; + case WEBCONTAINER_INIT: + if (UAVServer.ServerVendor.SPRINGBOOT != UAVServer.instance() + .getServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR)) { + return true; + } + break; case WEBCONTAINER_RESOURCE_CREATE: break; case AFTER_SERVET_INIT: @@ -119,8 +127,10 @@ public void handleEvent(InterceptContext context) { ClassLoader webapploader = (ClassLoader) context.get(InterceptConstants.WEBAPPLOADER); // switch event - Event evt = context.getEvent(); - switch (evt) { + Event event = context.getEvent(); + switch (event) { + case SPRING_BEAN_REGIST: + case WEBCONTAINER_INIT: insertWebServiceInterceptor(context, webapploader); break; diff --git a/com.creditease.uav.monitorframework.springbootFat/pom.xml b/com.creditease.uav.monitorframework.springbootFat/pom.xml index 820106cf..debc1ceb 100644 --- a/com.creditease.uav.monitorframework.springbootFat/pom.xml +++ b/com.creditease.uav.monitorframework.springbootFat/pom.xml @@ -8,7 +8,7 @@ com.example starter 0.0.1-SNAPSHOT - war + jar springbootStart @@ -16,6 +16,22 @@ org.springframework.boot spring-boot-starter-web + + + com.alibaba + dubbo + 2.5.3 + + + spring + org.springframework + + + commons-logging + commons-logging + + + junit @@ -101,6 +117,12 @@ com.creditease.moniter com.creditease.uav.cache.redis 1.0 + + + commons-logging + commons-logging + + biz.paluch.redis diff --git a/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/IMyDubboService.java b/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/IMyDubboService.java new file mode 100644 index 00000000..85b2d00f --- /dev/null +++ b/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/IMyDubboService.java @@ -0,0 +1,32 @@ +/*- + * << + * UAVStack + * == + * Copyright (C) 2016 - 2017 UAVStack + * == + * Licensed 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 com.creditease.monitorframework.fat.dubbo; + +import java.io.IOException; + +public interface IMyDubboService { + + String sayHello(String name); + + String sayException(String name) throws IOException; + + String sayUncatchException(String name); +} diff --git a/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/MyDubboService.java b/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/MyDubboService.java new file mode 100644 index 00000000..5800bb5f --- /dev/null +++ b/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/monitorframework/fat/dubbo/MyDubboService.java @@ -0,0 +1,48 @@ +/*- + * << + * UAVStack + * == + * Copyright (C) 2016 - 2017 UAVStack + * == + * Licensed 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 com.creditease.monitorframework.fat.dubbo; + +import java.io.IOException; + +public class MyDubboService implements IMyDubboService { + + @Override + public String sayHello(String name) { + + // TODO Auto-generated method stub + return name; + } + + @Override + public String sayException(String name) throws IOException { + + throw new IOException("专门测试用的异常"); + } + + @Override + public String sayUncatchException(String name) { + + String exceptionInt = "test"; + int num = Integer.parseInt(exceptionInt); + return name + num; + } + +} diff --git a/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/uav/monitorframework/springbootFat/CxfConfig.java b/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/uav/monitorframework/springbootFat/CxfConfig.java index 7f475f9d..e9297fb4 100644 --- a/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/uav/monitorframework/springbootFat/CxfConfig.java +++ b/com.creditease.uav.monitorframework.springbootFat/src/main/java/com/creditease/uav/monitorframework/springbootFat/CxfConfig.java @@ -1,5 +1,9 @@ package com.creditease.uav.monitorframework.springbootFat; +import javax.xml.ws.Endpoint; + +import org.apache.cxf.bus.spring.SpringBus; +import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; @@ -13,24 +17,24 @@ public ServletRegistrationBean dispatcherCXFServlet() { return new ServletRegistrationBean(new CXFServlet(), "/soap/*"); } - // - // @Bean(name = SpringBus.DEFAULT_BUS_ID) - // public SpringBus springBus() { - // - // return new SpringBus(); - // } - // - // @Bean - // public UserService userService() { - // - // return new UserServiceImpl(); - // } - // - // @Bean - // public Endpoint endpoint() { - // - // EndpointImpl endpoint = new EndpointImpl(new SpringBus(), userService()); - // endpoint.publish("/user"); - // return endpoint; - // } + + @Bean(name = SpringBus.DEFAULT_BUS_ID) + public SpringBus springBus() { + + return new SpringBus(); + } + + @Bean + public UserService userService() { + + return new UserServiceImpl(); + } + + @Bean + public Endpoint endpoint() { + + EndpointImpl endpoint = new EndpointImpl(new SpringBus(), userService()); + endpoint.publish("/user"); + return endpoint; + } } \ No newline at end of file diff --git a/com.creditease.uav.monitorframework.springbootFat/src/main/java/config.xml b/com.creditease.uav.monitorframework.springbootFat/src/main/java/config.xml deleted file mode 100644 index f1de412f..00000000 --- a/com.creditease.uav.monitorframework.springbootFat/src/main/java/config.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - \ No newline at end of file diff --git a/com.creditease.uav.monitorframework.springbootFat/src/main/java/application.properties b/com.creditease.uav.monitorframework.springbootFat/src/main/resources/application.properties similarity index 100% rename from com.creditease.uav.monitorframework.springbootFat/src/main/java/application.properties rename to com.creditease.uav.monitorframework.springbootFat/src/main/resources/application.properties diff --git a/com.creditease.uav.monitorframework.springbootFat/src/main/resources/config.xml b/com.creditease.uav.monitorframework.springbootFat/src/main/resources/config.xml new file mode 100644 index 00000000..5a55ea03 --- /dev/null +++ b/com.creditease.uav.monitorframework.springbootFat/src/main/resources/config.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/appfra/hook/spi/HookProxy.java b/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/appfra/hook/spi/HookProxy.java index 83dac2f0..8acb6636 100644 --- a/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/appfra/hook/spi/HookProxy.java +++ b/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/appfra/hook/spi/HookProxy.java @@ -21,6 +21,7 @@ package com.creditease.monitor.appfra.hook.spi; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import com.creditease.uav.common.BaseComponent; @@ -30,6 +31,8 @@ public abstract class HookProxy extends BaseComponent { protected final String id; + protected Map isHookEventDone = new HashMap(); + protected final Map config; public HookProxy(String id, Map config) { @@ -82,4 +85,17 @@ public boolean isRun(HookContext context) { return false; } + + /** + * check if hook event is done + * + * @param event + * @return + */ + public boolean isHookEventDone(String event) { + + Boolean result = isHookEventDone.get(event); + isHookEventDone.put(event, true); + return (result == null) ? false : result; + } } diff --git a/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/StandardInterceptContextHelper.java b/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/StandardInterceptContextHelper.java index eee0ccff..c51a19cd 100644 --- a/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/StandardInterceptContextHelper.java +++ b/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/StandardInterceptContextHelper.java @@ -23,12 +23,13 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import com.alibaba.ttl.TransmittableThreadLocal; import com.creditease.monitor.interceptframework.spi.InterceptContext; import com.creditease.monitor.interceptframework.spi.InterceptContext.Event; public class StandardInterceptContextHelper { - private static ThreadLocal ThreadCaptureContextHelper = new ThreadLocal(); + private static TransmittableThreadLocal ThreadCaptureContextHelper = new TransmittableThreadLocal(); protected static InterceptContext getContext(Event event, boolean isCreateForNone) { diff --git a/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/spi/InterceptContext.java b/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/spi/InterceptContext.java index a4f1046d..197449cb 100644 --- a/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/spi/InterceptContext.java +++ b/com.creditease.uav.monitorframework/src/main/java/com/creditease/monitor/interceptframework/spi/InterceptContext.java @@ -25,7 +25,7 @@ public interface InterceptContext { public enum Event { - WEBCONTAINER_RESOURCE_INIT, WEBCONTAINER_RESOURCE_CREATE, WEBCONTAINER_INIT, WEBCONTAINER_STARTED, WEBCONTAINER_STOPPED, AFTER_SERVET_INIT, BEFORE_SERVLET_DESTROY, GLOBAL_FILTER_REQUEST, GLOBAL_FILTER_RESPONSE + SPRING_BEAN_REGIST, WEBCONTAINER_RESOURCE_INIT, WEBCONTAINER_RESOURCE_CREATE, WEBCONTAINER_INIT, WEBCONTAINER_STARTED, WEBCONTAINER_STOPPED, AFTER_SERVET_INIT, BEFORE_SERVLET_DESTROY, GLOBAL_FILTER_REQUEST, GLOBAL_FILTER_RESPONSE } public T get(Class c); diff --git a/com.creditease.uav.monitorframework/src/main/java/com/creditease/uav/appserver/listeners/AppFrkHookFactoryListener.java b/com.creditease.uav.monitorframework/src/main/java/com/creditease/uav/appserver/listeners/AppFrkHookFactoryListener.java index 72adc56b..44137ec2 100644 --- a/com.creditease.uav.monitorframework/src/main/java/com/creditease/uav/appserver/listeners/AppFrkHookFactoryListener.java +++ b/com.creditease.uav.monitorframework/src/main/java/com/creditease/uav/appserver/listeners/AppFrkHookFactoryListener.java @@ -56,6 +56,7 @@ public void handleEvent(InterceptContext context) { Event evt = context.getEvent(); switch (evt) { + case SPRING_BEAN_REGIST: case WEBCONTAINER_RESOURCE_INIT: case WEBCONTAINER_RESOURCE_CREATE: case WEBCONTAINER_INIT: @@ -181,6 +182,8 @@ private void startAppFrkHook(Event evt, ClassLoader webapploader, InterceptConte public boolean isEventListener(Event event) { switch (event) { + case SPRING_BEAN_REGIST: + break; case WEBCONTAINER_INIT: break; case AFTER_SERVET_INIT: diff --git a/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/SpringBootTomcatPlusIT.java b/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/SpringBootTomcatPlusIT.java index 7a3bb44a..8b5a4337 100644 --- a/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/SpringBootTomcatPlusIT.java +++ b/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/SpringBootTomcatPlusIT.java @@ -27,6 +27,7 @@ import org.apache.catalina.core.StandardWrapper; import org.apache.catalina.loader.WebappClassLoader; +import com.creditease.agent.helpers.DataConvertHelper; import com.creditease.agent.helpers.ReflectHelper; import com.creditease.monitor.UAVServer; import com.creditease.monitor.captureframework.spi.CaptureConstants; @@ -35,22 +36,27 @@ import com.creditease.monitor.interceptframework.spi.InterceptContext; import com.creditease.monitor.interceptframework.spi.InterceptContext.Event; import com.creditease.tomcat.plus.util.TomcatLog; +import com.creditease.uav.util.MonitorServerUtil; public class SpringBootTomcatPlusIT extends TomcatPlusIT { /** * startUAVServer */ - public void startServer(int port) { + public void startServer(String port, String contextPath) { // integrate Tomcat log UAVServer.instance().setLog(new TomcatLog("MonitorServer")); // start Monitor Server when server starts UAVServer.instance().start(new Object[] { UAVServer.ServerVendor.SPRINGBOOT }); - + // set appid + setAppid(contextPath); // set the connector port - UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_LISTEN_PORT, port); - + UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_LISTEN_PORT, + DataConvertHelper.toInt(port, 8080)); + InterceptSupport iSupport = InterceptSupport.instance(); + // this context will be transmited from springboot mainThread to webcontainerInit thread then back to mainThread + iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED); } /** @@ -67,7 +73,7 @@ else if (contextPath.indexOf("/") == 0) { contextPath = contextPath.substring(1); } - System.setProperty("com.creditease.uav.springboot.appid", contextPath); + System.setProperty("com.creditease.uav.appid", MonitorServerUtil.getApplicationId(contextPath, "")); } @@ -252,7 +258,7 @@ public void onAppStart(Object... args) { StandardContext sc = (StandardContext) args[0]; InterceptSupport iSupport = InterceptSupport.instance(); - InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_STARTED); + InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED); /** * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do @@ -285,8 +291,9 @@ else if (basePath.lastIndexOf("/") == (basePath.length() - 1) } context.put(InterceptConstants.BASEPATH, basePath); + // we don't doIntercept here cause some pre-profile(like dubbo) not happen yet, profile will be done after + // finishRefresh - iSupport.doIntercept(context); } /** @@ -301,15 +308,14 @@ public void onAppStop(Object... args) { InterceptSupport iSupport = InterceptSupport.instance(); InterceptContext context = iSupport.createInterceptContext(Event.WEBCONTAINER_STOPPED); - /** - * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do - * anything on this we may use its webappclassloader's parent as the classloader - */ - if (null == context || null == sc) { return; } - + + /** + * NOTE: spring boot rewrite the tomcat webappclassloader, makes the addURL for nothing, then we can't do + * anything on this we may use its webappclassloader's parent as the classloader + */ context.put(InterceptConstants.WEBAPPLOADER, sc.getLoader().getClassLoader().getParent()); context.put(InterceptConstants.WEBWORKDIR, sc.getWorkPath()); @@ -371,4 +377,30 @@ public void onServletStop(Object... args) { iSupport.doIntercept(context); } + /** + * springboot load beans before web container start, hook opr should be done before beanRegist in case of duplicate + * definition ,so we define SPRING_BEAN_REGIST event to trigger hook + */ + public void onSpringBeanRegist(String contextPath) { + + InterceptSupport iSupport = InterceptSupport.instance(); + InterceptContext context = iSupport.createInterceptContext(Event.SPRING_BEAN_REGIST); + context.put(InterceptConstants.WEBAPPLOADER, Thread.currentThread().getContextClassLoader()); + context.put(InterceptConstants.CONTEXTPATH, contextPath); + context.put(InterceptConstants.BASEPATH, ""); + iSupport.doIntercept(context); + } + + /** + * ComponentProfile will be done after springboot finish it's context's refresh, cause every pre-profile(like dubbo) + * is ready. + * + */ + public void onSpringFinishRefresh() { + + InterceptSupport iSupport = InterceptSupport.instance(); + InterceptContext context = iSupport.getThreadLocalContext(Event.WEBCONTAINER_STARTED); + + iSupport.doIntercept(context); + } } diff --git a/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/TomcatPlusIT.java b/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/TomcatPlusIT.java index 09d63857..22ca29ca 100644 --- a/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/TomcatPlusIT.java +++ b/com.creditease.uav.tomcat.plus.core/src/main/java/com/creditease/tomcat/plus/interceptor/TomcatPlusIT.java @@ -448,7 +448,7 @@ public void onServletStop(Object... args) { @SuppressWarnings("unused") public void onDeployUAVApp(Object... args) { - if (System.getProperty("com.creditease.uav.iapp.install") != null) { + if (System.getProperty("com.creditease.uav.iapp.install") != null) { return; } @@ -485,7 +485,7 @@ else if (curVersion.equals("7") && versions[1].equals("0") ReflectHelper.invoke("org.apache.catalina.startup.HostConfig", hc, "deployDirectory", new Class[] { String.class, File.class, String.class }, - new Object[] { "com.creditease.uav", dir, mofRoot + "/com.creditease.uav" }, + new Object[] { "/com.creditease.uav", dir, mofRoot + "/com.creditease.uav" }, hc.getClass().getClassLoader()); break; case 1: