diff --git a/README.md b/README.md index ade26c3..fa6ff62 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,36 @@ ![](http://nepxion.gitee.io/discovery/docs/discovery-doc/Banner.png) # Discovery【探索】云原生微服务解决方案 -![Total visits](https://visitor-badge.laobi.icu/badge?page_id=Nepxion&title=total%20visits) [![Total lines](https://tokei.rs/b1/github/Nepxion/Discovery?category=lines)](https://tokei.rs/b1/github/Nepxion/Discovery?category=lines) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?label=license)](https://github.com/Nepxion/Discovery/blob/6.x.x/LICENSE) [![Maven Central](https://img.shields.io/maven-central/v/com.nepxion/discovery.svg?label=maven)](https://search.maven.org/artifact/com.nepxion/discovery) [![Javadocs](http://www.javadoc.io/badge/com.nepxion/discovery-plugin-framework-starter.svg)](http://www.javadoc.io/doc/com.nepxion/discovery-plugin-framework-starter) [![Build Status](https://travis-ci.org/Nepxion/Discovery.svg?branch=6.x.x)](https://travis-ci.org/Nepxion/Discovery) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/5c42eb719ef64def9cad773abd877e8b)](https://www.codacy.com/gh/Nepxion/Discovery/dashboard?utm_source=github.com&utm_medium=referral&utm_content=Nepxion/Discovery&utm_campaign=Badge_Grade) [![Stars](https://img.shields.io/github/stars/Nepxion/Discovery.svg?label=Stars&tyle=flat&logo=GitHub)](https://github.com/Nepxion/Discovery/stargazers) [![Stars](https://gitee.com/Nepxion/Discovery/badge/star.svg?theme=gvp)](https://gitee.com/Nepxion/Discovery/stargazers) +![Total visits](https://visitor-badge.laobi.icu/badge?page_id=Nepxion&title=total%20visits) [![Total lines](https://tokei.rs/b1/github/Nepxion/Discovery?category=lines)](https://tokei.rs/b1/github/Nepxion/Discovery?category=lines) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?label=license)](https://github.com/Nepxion/Discovery/blob/6.x.x/LICENSE) [![Maven Central](https://img.shields.io/maven-central/v/com.nepxion/discovery.svg?label=maven)](https://search.maven.org/artifact/com.nepxion/discovery) [![Javadocs](http://www.javadoc.io/badge/com.nepxion/discovery-plugin-framework-starter.svg)](http://www.javadoc.io/doc/com.nepxion/discovery-plugin-framework-starter) [![Build Status](https://github.com/Nepxion/Discovery/workflows/build/badge.svg)](https://github.com/Nepxion/Discovery/actions) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/5c42eb719ef64def9cad773abd877e8b)](https://www.codacy.com/gh/Nepxion/Discovery/dashboard?utm_source=github.com&utm_medium=referral&utm_content=Nepxion/Discovery&utm_campaign=Badge_Grade) [![Stars](https://img.shields.io/github/stars/Nepxion/Discovery.svg?label=Stars&tyle=flat&logo=GitHub)](https://github.com/Nepxion/Discovery/stargazers) [![Stars](https://gitee.com/Nepxion/Discovery/badge/star.svg?theme=gvp)](https://gitee.com/Nepxion/Discovery/stargazers) -[![Spring Boot](https://img.shields.io/maven-central/v/org.springframework.boot/spring-boot-dependencies.svg?label=Spring%20Boot&logo=Spring)](https://search.maven.org/artifact/org.springframework.boot/spring-boot-dependencies) [![Spring Cloud](https://img.shields.io/maven-central/v/org.springframework.cloud/spring-cloud-dependencies.svg?label=Spring%20Cloud&logo=Spring)](https://search.maven.org/artifact/org.springframework.cloud/spring-cloud-dependencies) [![Spring Cloud Alibaba](https://img.shields.io/maven-central/v/com.alibaba.cloud/spring-cloud-alibaba-dependencies.svg?label=Spring%20Cloud%20Alibaba&logo=Spring)](https://search.maven.org/artifact/com.alibaba.cloud/spring-cloud-alibaba-dependencies) [![Nepxion Discovery](https://img.shields.io/maven-central/v/com.nepxion/discovery.svg?label=Nepxion%20Discovery&logo=Anaconda)](https://search.maven.org/artifact/com.nepxion/discovery) - -[![Discovery PPT](https://img.shields.io/badge/Discovery%20-PPT-brightgreen?logo=Microsoft%20PowerPoint)](http://nepxion.gitee.io/discovery/docs/link-doc/discovery-ppt.html) [![Discovery WIKI](https://img.shields.io/badge/Discovery%20-WIKI-brightgreen?logo=Microsoft%20Edge)](http://nepxion.gitee.io/discovery/) [![Discovery Platform WIKI](https://img.shields.io/badge/Discovery%20Platform%20-WIKI-brightgreen?logo=Microsoft%20Edge)](http://nepxion.gitee.io/discoveryplatform) [![Polaris WIKI](https://img.shields.io/badge/Polaris%20-WIKI-brightgreen?logo=Microsoft%20Edge)](http://polaris-paas.gitee.io/polaris-sdk) +[![Wiki](https://badgen.net/badge/icon/wiki?icon=wiki&label=GitHub)](https://github.com/Nepxion/Discovery/wiki) [![Wiki](https://badgen.net/badge/icon/wiki?icon=wiki&label=Gitee)](https://gitee.com/nepxion/Discovery/wikis/pages?sort_id=3993615&doc_id=1124387) [![Discovery PPT](https://img.shields.io/badge/Discovery%20-ppt-brightgreen?logo=Microsoft%20PowerPoint)](http://nepxion.gitee.io/discovery/docs/link-doc/discovery-ppt.html) [![Discovery Page](https://img.shields.io/badge/Discovery%20-page-brightgreen?logo=Microsoft%20Edge)](http://nepxion.gitee.io/discovery/) [![Discovery Platform Page](https://img.shields.io/badge/Discovery%20Platform%20-page-brightgreen?logo=Microsoft%20Edge)](http://nepxion.gitee.io/discoveryplatform) [![Polaris Page](https://img.shields.io/badge/Polaris%20-page-brightgreen?logo=Microsoft%20Edge)](http://polaris-paas.gitee.io/polaris-sdk)             如果您觉得本框架具有一定的参考价值和借鉴意义,请帮忙在页面右上角 [**Star**] -![](http://nepxion.gitee.io/discovery/docs/icon-doc/star1.png) 首席作者简介 +## 简介 + +### 作者简介 - Nepxion开源社区创始人 - 2020年阿里巴巴中国云原生峰会出品人 - 2020年被Nacos和Spring Cloud Alibaba纳入相关开源项目 - 2021年阿里巴巴技术峰会上海站演讲嘉宾 - 2021年荣获陆奇博士主持的奇绩资本,进行风险投资的关注和调研 - 2021年入选Gitee最有价值开源项目 -- Nacos Group Member、Spring Cloud Alibaba Member +- 阿里巴巴官方书籍《Nacos架构与原理》作者之一 +- Spring Cloud Alibaba Steering Committer、Nacos Group Member - Spring Cloud Alibaba、Nacos、Sentinel、OpenTracing Committer & Contributor -![](http://nepxion.gitee.io/discovery/docs/icon-doc/star4.png) 商业化合作 - +### 商业合作 ① Discovery系列 | 框架名称 | 框架版本 | 支持Spring Cloud版本 | 使用许可 | | --- | --- | --- | --- | | Discovery | 1.x.x ~ 6.x.x | Camden ~ Hoxton | 开源,永久免费 | -| DiscoveryX | 7.x.x | 202x | 闭源,商业许可 | +| DiscoveryX | 7.x.x ~ 9.x.x | 2020 ~ 2022 | 闭源,商业许可 | ② Polaris系列 @@ -46,36 +46,46 @@ Polaris为Discovery高级定制版,特色功能 | 框架名称 | 框架版本 | 支持Discovery版本 | 支持Spring Cloud版本 | 使用许可 | | --- | --- | --- | --- | --- | | Polaris | 1.x.x | 6.x.x | Finchley ~ Hoxton | 闭源,商业许可 | -| Polaris | 2.x.x | 7.x.x | 202x | 闭源,商业许可 | +| Polaris | 2.x.x | 7.x.x ~ 9.x.x | 2020 ~ 2022 | 闭源,商业许可 | 有商业版需求的企业和用户,请添加微信1394997,联系作者,洽谈合作事宜 +### 入门资料 ![](http://nepxion.gitee.io/discovery/docs/discovery-doc/Logo64.png) Discovery【探索】企业级云原生微服务开源解决方案 -① 解决方案 +① 快速入门 +- [快速入门Github版](https://github.com/Nepxion/Discovery/wiki) +- [快速入门Gitee版](https://gitee.com/Nepxion/Discovery/wikis/pages) + +② 解决方案 - [解决方案WIKI版](http://nepxion.com/discovery) - [解决方案PPT版](http://nepxion.gitee.io/discovery/docs/link-doc/discovery-ppt.html) -② 平台界面 -- [平台界面WIKI版](http://nepxion.com/discovery-platform) +③ 最佳实践 +- [最佳实践PPT版](http://nepxion.gitee.io/discovery/docs/link-doc/discovery-ppt-1.html) -③ 快速入门 -- [快速入门Github版](https://github.com/Nepxion/Discovery/wiki) -- [快速入门Gitee版](https://gitee.com/Nepxion/Discovery/wikis/pages) +④ 平台界面 +- [平台界面WIKI版](http://nepxion.com/discovery-platform) -④ 框架源码 +⑤ 框架源码 - [框架源码Github版](https://github.com/Nepxion/Discovery) - [框架源码Gitee版](https://gitee.com/Nepxion/Discovery) -⑤ 指南示例源码 +⑥ 指南示例源码 - [指南示例源码Github版](https://github.com/Nepxion/DiscoveryGuide) - [指南示例源码Gitee版](https://gitee.com/Nepxion/DiscoveryGuide) -⑥ 指南示例说明 -- 对于入门级玩家,参考[6.x.x指南示例极简版](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x-simple),分支为6.x.x-simple -- 对于熟练级玩家,参考[6.x.x指南示例精进版](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x),分支为6.x.x。除上述《极简版》功能外,涉及到指南篇里的绝大多数高级功能 -- 对于骨灰级玩家,参考[6.x.x指南示例高级版](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x-complex),分支为6.x.x-complex。除上述《精进版》功能外,涉及到指南篇里的ActiveMQ、MongoDB、RabbitMQ、Redis、RocketMQ、MySQL等高级调用链和蓝绿灰度调用链的整合 -- 上述指南实例分支是针对Spring Cloud旧版本。对于Spring Cloud 202x版本,参考[7.x.x指南示例精进版](https://github.com/Nepxion/DiscoveryGuide/tree/master),分支为master +⑦ 指南示例说明 +- Spring Cloud Finchley ~ Hoxton版本 + - [极简版指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x-simple),分支为6.x.x-simple + - [极简版域网关部署指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x-simple-domain-gateway),分支为6.x.x-simple-domain-gateway + - [极简版非域网关部署指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x-simple-non-domain-gateway),分支为6.x.x-simple-non-domain-gateway + - [集成版指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x),分支为6.x.x + - [高级版指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/6.x.x-complex),分支为6.x.x-complex +- Spring Cloud 202x版本 + - [极简版指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/master-simple),分支为master-simple + - [极简版本地化指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/master-simple-native),分支为master-simple-native + - [集成版指南示例](https://github.com/Nepxion/DiscoveryGuide/tree/master),分支为master ![](http://nepxion.gitee.io/discovery/docs/polaris-doc/Logo64.png) Polaris【北极星】企业级云原生微服务商业解决方案 @@ -91,42 +101,10 @@ Polaris为Discovery高级定制版,特色功能 - [指南示例源码Gitee版](https://gitee.com/polaris-paas/polaris-guide) ④ 指南示例说明 -- Spring Cloud旧版本,参考[1.x.x指南示例](https://github.com/polaris-paas/polaris-guide/tree/1.x.x),分支为1.x.x -- Spring Cloud新版本,参考[2.x.x指南示例](https://github.com/polaris-paas/polaris-guide/tree/master),分支为master - -![](http://nepxion.gitee.io/discovery/docs/icon-doc/Logo64.png) Discovery【探索】和Polaris【北极星】架构体系 - -① Discovery【探索】和Polaris【北极星】联合架构图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/Architecture.jpg) - -② Discovery【探索】和Polaris【北极星】联合拓扑图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/Topology.jpg) - -③ Polaris【北极星】分层架构图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/Layer.jpg) - -④ Discovery【探索】实施方案图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/All.jpg) - -⑤ Discovery【探索】域网关实施图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/DomainEnable.jpg) - -⑥ Discovery【探索】非域网关实施图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/DomainDisable.jpg) - -⑦ Discovery【探索】全局订阅实施图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/GlobalSub.jpg) - -⑧ Discovery【探索】配置中心发布订阅图 - -![](http://nepxion.gitee.io/discovery/docs/polaris-doc/Config.jpg) +- Spring Cloud Finchley ~ Hoxton版本 + - [指南示例](https://github.com/polaris-paas/polaris-guide/tree/1.x.x),分支为1.x.x +- Spring Cloud 202x版本 + - [指南示例](https://github.com/polaris-paas/polaris-guide/tree/master),分支为master ## 请联系我 微信、钉钉、公众号和文档 diff --git a/discovery-agent-example/discovery-agent-example-agent/pom.xml b/discovery-agent-example/discovery-agent-example-agent/pom.xml index 1f59c68..95da492 100644 --- a/discovery-agent-example/discovery-agent-example-agent/pom.xml +++ b/discovery-agent-example/discovery-agent-example-agent/pom.xml @@ -9,7 +9,7 @@ com.nepxion discovery-agent-example - 1.1.0 + 1.3.0 diff --git a/discovery-agent-example/discovery-agent-example-application/pom.xml b/discovery-agent-example/discovery-agent-example-application/pom.xml index bbdb292..110410e 100644 --- a/discovery-agent-example/discovery-agent-example-application/pom.xml +++ b/discovery-agent-example/discovery-agent-example-application/pom.xml @@ -9,7 +9,7 @@ com.nepxion discovery-agent-example - 1.1.0 + 1.3.0 diff --git a/discovery-agent-example/discovery-agent-example-sdk/pom.xml b/discovery-agent-example/discovery-agent-example-sdk/pom.xml index 697fda3..212f054 100644 --- a/discovery-agent-example/discovery-agent-example-sdk/pom.xml +++ b/discovery-agent-example/discovery-agent-example-sdk/pom.xml @@ -9,6 +9,6 @@ com.nepxion discovery-agent-example - 1.1.0 + 1.3.0 \ No newline at end of file diff --git a/discovery-agent-example/pom.xml b/discovery-agent-example/pom.xml index edeaf4b..c38f832 100644 --- a/discovery-agent-example/pom.xml +++ b/discovery-agent-example/pom.xml @@ -9,7 +9,7 @@ com.nepxion discovery-agent - 1.1.0 + 1.3.0 diff --git a/discovery-agent-starter-plugin-mdc/pom.xml b/discovery-agent-starter-plugin-mdc/pom.xml index 7dcb7f1..70fb603 100644 --- a/discovery-agent-starter-plugin-mdc/pom.xml +++ b/discovery-agent-starter-plugin-mdc/pom.xml @@ -11,7 +11,7 @@ com.nepxion discovery-agent - 1.1.0 + 1.3.0 diff --git a/discovery-agent-starter-plugin-strategy/pom.xml b/discovery-agent-starter-plugin-strategy/pom.xml index c1853b9..4a9dc7e 100644 --- a/discovery-agent-starter-plugin-strategy/pom.xml +++ b/discovery-agent-starter-plugin-strategy/pom.xml @@ -11,7 +11,7 @@ com.nepxion discovery-agent - 1.1.0 + 1.3.0 diff --git a/discovery-agent-starter/pom.xml b/discovery-agent-starter/pom.xml index 0282344..0e2b7d0 100644 --- a/discovery-agent-starter/pom.xml +++ b/discovery-agent-starter/pom.xml @@ -11,7 +11,7 @@ com.nepxion discovery-agent - 1.1.0 + 1.3.0 diff --git a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/constant/DiscoveryAgentConstant.java b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/constant/DiscoveryAgentConstant.java index 5f83d46..84c00ec 100644 --- a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/constant/DiscoveryAgentConstant.java +++ b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/constant/DiscoveryAgentConstant.java @@ -10,7 +10,7 @@ */ public class DiscoveryAgentConstant { - public static final String DISCOVERY_AGENT_VERSION = "1.1.0"; + public static final String DISCOVERY_AGENT_VERSION = "1.3.0"; public static final String SPRING_APPLICATION_DISCOVERY_AGENT_VERSION = "spring.application.discovery.agent.version"; } \ No newline at end of file diff --git a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadConstant.java b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadConstant.java index 93c3cd3..b8e6df7 100644 --- a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadConstant.java +++ b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadConstant.java @@ -17,7 +17,11 @@ public class ThreadConstant { public static final String THREAD_SCAN_PACKAGES = "thread.scan.packages"; public static final String THREAD_REQUEST_DECORATOR_ENABLED = "thread.request.decorator.enabled"; public static final String RUNNABLE_CLASS_NAME = "java.lang.Runnable"; + public static final String RUNNABLE_IMPL_METHOD_NAME = "run"; public static final String CALLABLE_CLASS_NAME = "java.util.concurrent.Callable"; + public static final String CALLABLE_IMPL_METHOD_NAME = "call"; + public static final String SUPPLIER_CLASS_NAME = "java.util.function.Supplier"; + public static final String SUPPLIER_IMPL_METHOD_NAME = "get"; public static final String THREAD_SCAN_PACKAGES_DELIMITERS = ";"; public static final String CONSTRUCTOR_INTERCEPTOR = String.format("%s.before(this);\n", ThreadConstructorInterceptor.class.getName()); diff --git a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadPlugin.java b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadPlugin.java index 4c33014..6d9de08 100644 --- a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadPlugin.java +++ b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadPlugin.java @@ -8,28 +8,16 @@ * @author zifeihan * @version 1.0 */ - -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtConstructor; -import javassist.CtField; -import javassist.CtMethod; - -import java.lang.reflect.Method; -import java.security.ProtectionDomain; import java.util.HashSet; import java.util.List; import java.util.Properties; -import com.nepxion.discovery.agent.async.AsyncContextAccessor; -import com.nepxion.discovery.agent.callback.TransformCallback; import com.nepxion.discovery.agent.callback.TransformTemplate; import com.nepxion.discovery.agent.config.Config; import com.nepxion.discovery.agent.logger.AgentLogger; import com.nepxion.discovery.agent.matcher.MatcherFactory; import com.nepxion.discovery.agent.matcher.MatcherOperator; import com.nepxion.discovery.agent.plugin.Plugin; -import com.nepxion.discovery.agent.util.ClassInfo; import com.nepxion.discovery.agent.util.StringUtil; public class ThreadPlugin extends Plugin { @@ -61,112 +49,39 @@ public void install(TransformTemplate transformTemplate) { RunnableTransformCallback runnableTransformCallback = new RunnableTransformCallback(); CallableTransformCallback callableTransformCallback = new CallableTransformCallback(); + SupplierTransformCallback supplierTransformCallback = new SupplierTransformCallback(); for (String basePackage : scanPackages) { MatcherOperator runnableInterfaceMatcherOperator = MatcherFactory.newPackageBasedMatcher(basePackage, ThreadConstant.RUNNABLE_CLASS_NAME); MatcherOperator callableInterfaceMatcherOperator = MatcherFactory.newPackageBasedMatcher(basePackage, ThreadConstant.CALLABLE_CLASS_NAME); + MatcherOperator supplierInterfaceMatcherOperator = MatcherFactory.newPackageBasedMatcher(basePackage, ThreadConstant.SUPPLIER_CLASS_NAME); transformTemplate.transform(runnableInterfaceMatcherOperator, runnableTransformCallback); transformTemplate.transform(callableInterfaceMatcherOperator, callableTransformCallback); + transformTemplate.transform(supplierInterfaceMatcherOperator, supplierTransformCallback); } LOG.info(String.format("%s install successfully", this.getClass().getSimpleName())); } - public static class RunnableTransformCallback implements TransformCallback { + public static class RunnableTransformCallback extends ThreadTransformCallback { @Override - public byte[] doInTransform(ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { - try { - ClassInfo classInfo = new ClassInfo(className, classfileBuffer, classLoader); - CtClass ctClass = classInfo.getCtClass(); - - addField(ctClass, AsyncContextAccessor.class); - - CtConstructor[] declaredConstructors = ctClass.getDeclaredConstructors(); - for (CtConstructor ctConstructor : declaredConstructors) { - ctConstructor.insertAfter(ThreadConstant.CONSTRUCTOR_INTERCEPTOR); - } - - CtMethod ctMethod = ctClass.getDeclaredMethod("run"); - if (null != ctMethod) { - ctMethod.insertBefore(ThreadConstant.RUN_BEFORE_INTERCEPTOR); - ctMethod.insertAfter(ThreadConstant.RUN_AFTER_INTERCEPTOR); - } - - return ctClass.toBytecode(); - } catch (Exception e) { - LOG.warn(String.format("Transform %s error,message:", className), e); - - return null; - } + public String getImplMethodName() { + return ThreadConstant.RUNNABLE_IMPL_METHOD_NAME; } } - public static void addField(CtClass ctClass, Class clazz) { - try { - ClassPool aDefault = ClassPool.getDefault(); - CtClass makeInterface = aDefault.makeInterface(clazz.getName()); - ctClass.addInterface(makeInterface); - - Method[] methods = clazz.getDeclaredMethods(); - if (methods.length != 2) { - throw new IllegalArgumentException("accessorType has to declare 2 methods. " + clazz.getName() + " has " + methods.length); - } - - Method getter; - Method setter; - - if (methods[0].getParameterTypes().length == 0) { - getter = methods[0]; - setter = methods[1]; - } else { - getter = methods[1]; - setter = methods[0]; - } + public static class CallableTransformCallback extends ThreadTransformCallback { - Class fieldType = getter.getReturnType(); - String fieldName = fieldType.getSimpleName().toLowerCase(); - String field = String.format("private %s %s;", fieldType.getName(), fieldName); - - CtField f1 = CtField.make(field, ctClass); - ctClass.addField(f1); - - String getMethod = String.format("public %s %s(){return %s;}", fieldType.getName(), getter.getName(), fieldName); - String setMethod = String.format("public void %s(%s %s){this.%s = %s;}", setter.getName(), fieldType.getName(), fieldName, fieldName, fieldName); - - CtMethod m1 = CtMethod.make(getMethod, ctClass); - CtMethod m2 = CtMethod.make(setMethod, ctClass); - ctClass.addMethod(m1); - ctClass.addMethod(m2); - } catch (Exception e) { - LOG.warn(String.format("Add field %s error, message:", clazz.getName()), e); + @Override + public String getImplMethodName() { + return ThreadConstant.CALLABLE_IMPL_METHOD_NAME; } } - public static class CallableTransformCallback implements TransformCallback { - @Override - public byte[] doInTransform(ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { - try { - ClassInfo classInfo = new ClassInfo(className, classfileBuffer, classLoader); - CtClass ctClass = classInfo.getCtClass(); - - addField(ctClass, AsyncContextAccessor.class); - - CtConstructor[] declaredConstructors = ctClass.getDeclaredConstructors(); - for (CtConstructor ctConstructor : declaredConstructors) { - ctConstructor.insertAfter(ThreadConstant.CONSTRUCTOR_INTERCEPTOR); - } - - CtMethod ctMethod = ctClass.getDeclaredMethod("call"); - if (null != ctMethod) { - ctMethod.insertBefore(ThreadConstant.RUN_BEFORE_INTERCEPTOR); - ctMethod.insertAfter(ThreadConstant.RUN_AFTER_INTERCEPTOR); - } + public static class SupplierTransformCallback extends ThreadTransformCallback { - return ctClass.toBytecode(); - } catch (Exception e) { - LOG.warn(String.format("Transform %s error, message:", className), e); - - return null; - } + @Override + public String getImplMethodName() { + return ThreadConstant.SUPPLIER_IMPL_METHOD_NAME; } } } \ No newline at end of file diff --git a/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadTransformCallback.java b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadTransformCallback.java new file mode 100644 index 0000000..a611960 --- /dev/null +++ b/discovery-agent-starter/src/main/java/com/nepxion/discovery/agent/plugin/thread/ThreadTransformCallback.java @@ -0,0 +1,99 @@ +package com.nepxion.discovery.agent.plugin.thread; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author zifeihan + * @version 1.0 + */ + +import javassist.ClassPool; +import javassist.CtClass; +import javassist.CtConstructor; +import javassist.CtField; +import javassist.CtMethod; + +import java.lang.reflect.Method; +import java.security.ProtectionDomain; + +import com.nepxion.discovery.agent.async.AsyncContextAccessor; +import com.nepxion.discovery.agent.callback.TransformCallback; +import com.nepxion.discovery.agent.logger.AgentLogger; +import com.nepxion.discovery.agent.util.ClassInfo; + +public abstract class ThreadTransformCallback implements TransformCallback { + private static final AgentLogger LOG = AgentLogger.getLogger(ThreadTransformCallback.class.getName()); + + @Override + public byte[] doInTransform(ClassLoader classLoader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { + try { + ClassInfo classInfo = new ClassInfo(className, classfileBuffer, classLoader); + CtClass ctClass = classInfo.getCtClass(); + + addField(ctClass, AsyncContextAccessor.class); + + CtConstructor[] declaredConstructors = ctClass.getDeclaredConstructors(); + for (CtConstructor ctConstructor : declaredConstructors) { + ctConstructor.insertAfter(ThreadConstant.CONSTRUCTOR_INTERCEPTOR); + } + + String implMethodName = getImplMethodName(); + CtMethod ctMethod = ctClass.getDeclaredMethod(implMethodName); + if (null != ctMethod) { + ctMethod.insertBefore(ThreadConstant.RUN_BEFORE_INTERCEPTOR); + ctMethod.insertAfter(ThreadConstant.RUN_AFTER_INTERCEPTOR); + } + + return ctClass.toBytecode(); + } catch (Exception e) { + LOG.warn(String.format("Transform %s error, message:", className), e); + + return null; + } + } + + public void addField(CtClass ctClass, Class clazz) { + try { + ClassPool aDefault = ClassPool.getDefault(); + CtClass makeInterface = aDefault.makeInterface(clazz.getName()); + ctClass.addInterface(makeInterface); + + Method[] methods = clazz.getDeclaredMethods(); + if (methods.length != 2) { + throw new IllegalArgumentException("accessorType has to declare 2 methods. " + clazz.getName() + " has " + methods.length); + } + + Method getter; + Method setter; + + if (methods[0].getParameterTypes().length == 0) { + getter = methods[0]; + setter = methods[1]; + } else { + getter = methods[1]; + setter = methods[0]; + } + + Class fieldType = getter.getReturnType(); + String fieldName = fieldType.getSimpleName().toLowerCase(); + String field = String.format("private %s %s;", fieldType.getName(), fieldName); + + CtField f1 = CtField.make(field, ctClass); + ctClass.addField(f1); + + String getMethod = String.format("public %s %s(){return %s;}", fieldType.getName(), getter.getName(), fieldName); + String setMethod = String.format("public void %s(%s %s){this.%s = %s;}", setter.getName(), fieldType.getName(), fieldName, fieldName, fieldName); + + CtMethod m1 = CtMethod.make(getMethod, ctClass); + CtMethod m2 = CtMethod.make(setMethod, ctClass); + ctClass.addMethod(m1); + ctClass.addMethod(m2); + } catch (Exception e) { + LOG.warn(String.format("Add field %s error, message:", clazz.getName()), e); + } + } + + public abstract String getImplMethodName(); +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index fddef1b..bdca16d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ Nepxion Discovery Agent pom 4.0.0 - 1.1.0 + 1.3.0 Nepxion Discovery Agent is a java agent to resolve loss of ThreadLocal in cross-thread scenario, such as Spring Async、Hystrix Thread、Runnable、Callable、Single Thread、Thread Pool、MDC http://www.nepxion.com @@ -18,7 +18,7 @@
- 6.12.1 + 6.21.0 3.23.2-GA @@ -156,9 +156,9 @@ - https://github.com/Nepxion/Discovery - scm:git:https://github.com/Nepxion/Discovery.git - scm:git:https://github.com/Nepxion/Discovery.git + https://github.com/Nepxion/DiscoveryAgent + scm:git:https://github.com/Nepxion/DiscoveryAgent.git + scm:git:https://github.com/Nepxion/DiscoveryAgent.git v${project.version} diff --git a/version.bat b/version.bat index 2eab537..1f6d54a 100644 --- a/version.bat +++ b/version.bat @@ -15,5 +15,5 @@ @title Nepxion Discovery @color 0a -call mvn versions:set -DgenerateBackupPoms=false -DnewVersion=1.1.0 +call mvn versions:set -DgenerateBackupPoms=false -DnewVersion=1.3.0 pause \ No newline at end of file