Skip to content

Commit d644bb0

Browse files
authored
Merge pull request #51 from zifeiyu0531/main
dubbox 代码优化
2 parents 03204ac + 66c6835 commit d644bb0

File tree

13 files changed

+67
-118
lines changed

13 files changed

+67
-118
lines changed

polaris-agent-adapter/pinpoint/pinpoint-dubbox-plugin/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
<name>Pinpoint Dubbox Plugin</name>
1515

1616
<dependencies>
17+
<!-- dubbo -->
18+
<dependency>
19+
<groupId>com.alibaba</groupId>
20+
<artifactId>dubbo</artifactId>
21+
<version>${alibaba.dubbo.version}</version>
22+
<scope>provided</scope>
23+
</dependency>
1724
<dependency>
1825
<groupId>com.tencent.polaris</groupId>
1926
<artifactId>polaris-agent-dubbox-plugin</artifactId>

polaris-agent-adapter/pinpoint/pinpoint-dubbox-plugin/src/main/java/cn/polarismesh/agent/pinpoint/dubbox/DubboPlugin.java

+25-15
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616

1717
package cn.polarismesh.agent.pinpoint.dubbox;
1818

19-
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.DubboClusterInvokerInterceptor;
20-
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.DubboExporterInterceptor;
21-
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.DubboInvokeInterceptor;
22-
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.DubboInvokerInterceptor;
23-
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.DubboRegistryInterceptor;
24-
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.DubboUrlInterceptor;
19+
import cn.polarismesh.agent.pinpoint.dubbox.Interceptor.*;
20+
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.registry.RegistryFactory;
22+
import com.alibaba.dubbo.registry.integration.RegistryProtocol;
23+
import com.alibaba.dubbo.rpc.Invocation;
24+
import com.alibaba.dubbo.rpc.Invoker;
25+
import com.alibaba.dubbo.rpc.cluster.Directory;
26+
import com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker;
27+
import com.alibaba.dubbo.rpc.protocol.AbstractExporter;
28+
import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol;
29+
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
30+
import com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol;
31+
import com.alibaba.dubbo.rpc.protocol.memcached.MemcachedProtocol;
32+
import com.alibaba.dubbo.rpc.protocol.redis.RedisProtocol;
33+
import com.alibaba.dubbo.rpc.protocol.thrift.ThriftProtocol;
2534
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
2635
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
2736
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod;
@@ -31,6 +40,7 @@
3140
import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformTemplateAware;
3241
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPlugin;
3342
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext;
43+
3444
import java.security.ProtectionDomain;
3545
import java.util.Map;
3646

@@ -59,9 +69,9 @@ private void addTransformers() {
5969
transformTemplate.transform("com.alibaba.dubbo.rpc.protocol.thrift.ThriftProtocol", ProtocolTransform.class);
6070

6171
transformTemplate.transform("com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker",
62-
AbstractClusterInvokerTransform.class);
72+
ClusterInvokerTransform.class);
6373

64-
transformTemplate.transform("com.alibaba.dubbo.rpc.protocol.AbstractExporter", AbstractExporterTransform.class);
74+
transformTemplate.transform("com.alibaba.dubbo.rpc.protocol.AbstractExporter", ExporterTransform.class);
6575

6676
transformTemplate.transform("com.alibaba.dubbo.common.URL", UrlConstructorTransform.class);
6777
}
@@ -74,7 +84,7 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, Strin
7484
throws InstrumentException {
7585
final InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
7686
InstrumentMethod invokeMethod = target
77-
.getDeclaredMethod("setRegistryFactory", "com.alibaba.dubbo.registry.RegistryFactory");
87+
.getDeclaredMethod("setRegistryFactory", RegistryFactory.class.getCanonicalName());
7888
if (invokeMethod != null) {
7989
invokeMethod.addInterceptor(DubboRegistryInterceptor.class);
8090
}
@@ -90,42 +100,42 @@ public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, Strin
90100
throws InstrumentException {
91101
final InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
92102
InstrumentMethod invokeMethod = target
93-
.getDeclaredMethod("refer", "java.lang.Class", "com.alibaba.dubbo.common.URL");
103+
.getDeclaredMethod("refer", Class.class.getCanonicalName(), URL.class.getCanonicalName());
94104
if (invokeMethod != null) {
95105
invokeMethod.addInterceptor(DubboInvokerInterceptor.class);
96106
}
97107
return target.toBytecode();
98108
}
99109
}
100110

101-
public static class AbstractClusterInvokerTransform implements TransformCallback {
111+
public static class ClusterInvokerTransform implements TransformCallback {
102112

103113
@Override
104114
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className,
105115
Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)
106116
throws InstrumentException {
107117
final InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
108118
InstrumentMethod constructor = target
109-
.getConstructor("com.alibaba.dubbo.rpc.cluster.Directory", "com.alibaba.dubbo.common.URL");
119+
.getConstructor(Directory.class.getCanonicalName(), URL.class.getCanonicalName());
110120
if (constructor != null) {
111121
constructor.addInterceptor(DubboClusterInvokerInterceptor.class);
112122
}
113-
InstrumentMethod invokeMethod = target.getDeclaredMethod("invoke", "com.alibaba.dubbo.rpc.Invocation");
123+
InstrumentMethod invokeMethod = target.getDeclaredMethod("invoke", Invocation.class.getCanonicalName());
114124
if (invokeMethod != null) {
115125
invokeMethod.addInterceptor(DubboInvokeInterceptor.class);
116126
}
117127
return target.toBytecode();
118128
}
119129
}
120130

121-
public static class AbstractExporterTransform implements TransformCallback {
131+
public static class ExporterTransform implements TransformCallback {
122132

123133
@Override
124134
public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className,
125135
Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)
126136
throws InstrumentException {
127137
final InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
128-
InstrumentMethod constructor = target.getConstructor("com.alibaba.dubbo.rpc.Invoker");
138+
InstrumentMethod constructor = target.getConstructor(Invoker.class.getCanonicalName());
129139
if (constructor != null) {
130140
constructor.addInterceptor(DubboExporterInterceptor.class);
131141
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
22

3-
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4-
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
3+
public class DubboClusterInvokerInterceptor extends DubboInterceptorTemplate {
54

6-
public class DubboClusterInvokerInterceptor implements AroundInterceptor {
7-
8-
private AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9-
10-
@Override
11-
public void before(Object target, Object[] args) {
12-
interceptor.before(target, args);
13-
}
14-
15-
@Override
16-
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17-
interceptor.after(target, args, result, throwable);
18-
}
19-
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
22

3-
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4-
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
3+
public class DubboExporterInterceptor extends DubboInterceptorTemplate {
54

6-
public class DubboExporterInterceptor implements AroundInterceptor {
7-
8-
private AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9-
10-
@Override
11-
public void before(Object target, Object[] args) {
12-
interceptor.before(target, args);
13-
}
14-
15-
@Override
16-
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17-
interceptor.after(target, args, result, throwable);
18-
}
195
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
2+
3+
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4+
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
5+
6+
public abstract class DubboInterceptorTemplate implements AroundInterceptor {
7+
8+
private AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9+
10+
@Override
11+
public void before(Object target, Object[] args) {
12+
interceptor.before(target, args);
13+
}
14+
15+
@Override
16+
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17+
interceptor.after(target, args, result, throwable);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
22

3-
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4-
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
3+
public class DubboInvokeInterceptor extends DubboInterceptorTemplate {
54

6-
public class DubboInvokeInterceptor implements AroundInterceptor {
7-
8-
private AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9-
10-
@Override
11-
public void before(Object target, Object[] args) {
12-
interceptor.before(target, args);
13-
}
14-
15-
@Override
16-
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17-
interceptor.after(target, args, result, throwable);
18-
}
195
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
22

3-
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4-
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
3+
public class DubboInvokerInterceptor extends DubboInterceptorTemplate {
54

6-
public class DubboInvokerInterceptor implements AroundInterceptor {
7-
8-
private AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9-
10-
@Override
11-
public void before(Object target, Object[] args) {
12-
interceptor.before(target, args);
13-
}
14-
15-
@Override
16-
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17-
interceptor.after(target, args, result, throwable);
18-
}
195
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
22

3-
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4-
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
3+
public class DubboRegistryInterceptor extends DubboInterceptorTemplate {
54

6-
public class DubboRegistryInterceptor implements AroundInterceptor {
7-
8-
private AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9-
10-
@Override
11-
public void before(Object target, Object[] args) {
12-
interceptor.before(target, args);
13-
}
14-
15-
@Override
16-
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17-
interceptor.after(target, args, result, throwable);
18-
}
19-
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package cn.polarismesh.agent.pinpoint.dubbox.Interceptor;
22

3-
import cn.polarismesh.agent.plugin.dubbox.interceptor.AbstractInterceptor;
4-
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
3+
public class DubboUrlInterceptor extends DubboInterceptorTemplate {
54

6-
public class DubboUrlInterceptor implements AroundInterceptor {
7-
8-
private final AbstractInterceptor interceptor = InterceptorFactory.getInterceptor(this.getClass());
9-
10-
@Override
11-
public void before(Object target, Object[] args) {
12-
interceptor.before(target, args);
13-
}
14-
15-
@Override
16-
public void after(Object target, Object[] args, Object result, Throwable throwable) {
17-
interceptor.after(target, args, result, throwable);
18-
}
195
}

polaris-agent-core/plugins/dubbo2/pom.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414

1515
<name>Polaris Agent Dubbo2 Plugin</name>
1616

17-
<properties>
18-
<dubbo.version>2.7.15</dubbo.version>
19-
</properties>
20-
2117
<dependencies>
2218
<!-- dubbo2 -->
2319
<dependency>
2420
<groupId>org.apache.dubbo</groupId>
2521
<artifactId>dubbo</artifactId>
26-
<version>${dubbo.version}</version>
22+
<version>${apache.dubbo.version}</version>
2723
<scope>provided</scope>
2824
</dependency>
2925
<dependency>

polaris-agent-core/plugins/dubbox/pom.xml

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@
1414

1515
<name>Polaris Agent Dubbox Plugin</name>
1616

17-
<properties>
18-
<dubbo.version>2.6.12</dubbo.version>
19-
</properties>
20-
2117
<dependencies>
2218
<!-- dubbo -->
2319
<dependency>
2420
<groupId>com.alibaba</groupId>
2521
<artifactId>dubbo</artifactId>
26-
<version>${dubbo.version}</version>
22+
<version>${alibaba.dubbo.version}</version>
2723
<scope>provided</scope>
2824
</dependency>
2925
<dependency>

polaris-agent-core/plugins/dubbox/src/main/java/cn/polarismesh/agent/plugin/dubbox/interceptor/DubboInvokeInterceptor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.polarismesh.agent.plugin.dubbox.polaris.PolarisSingleton;
44
import com.alibaba.dubbo.common.URL;
55
import com.alibaba.dubbo.rpc.Invocation;
6+
import com.alibaba.dubbo.rpc.RpcResult;
67

78
/**
89
* 统计时延信息、上报服务调用结果
@@ -32,6 +33,6 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
3233
URL url = invocation.getInvoker().getUrl();
3334
PolarisSingleton.getPolarisOperation()
3435
.reportInvokeResult(url.getServiceInterface(), invocation.getMethodName(), url.getHost(), url.getPort(),
35-
delay, null == throwable, null != throwable ? -1 : 0);
36+
delay, null == throwable && !((RpcResult) result).hasException(), null != throwable || ((RpcResult) result).hasException() ? -1 : 0);
3637
}
3738
}

pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<maven.shaded.plugin.version>3.2.4</maven.shaded.plugin.version>
3434
<org.codehaus.mojo.version>3.0.0</org.codehaus.mojo.version>
3535
<slf4j.vesion>1.7.30</slf4j.vesion>
36+
<!-- alibaba dubbo (dubbox) -->
37+
<alibaba.dubbo.version>2.6.12</alibaba.dubbo.version>
38+
<!-- apache dubbo (dubbo2) -->
39+
<apache.dubbo.version>>2.7.15</apache.dubbo.version>
3640
</properties>
3741

3842
<dependencyManagement>

0 commit comments

Comments
 (0)