Skip to content

Commit

Permalink
support define. #26
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyunabc committed Sep 10, 2021
1 parent 999ef22 commit 259d495
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ importPackages=com.alibaba.fastjson
`MonitorFilter`里调用了在 plugin里加载的`com.test.dubbo.RpcUtils`


## 配置define 工具类

在增强代码之后,如果把逻辑全部写到`@Instrument`里:

* 增强代码会太复杂
* 有重复的逻辑需要重用
* `@Instrument`里插入的代码缺少行号

那么可以定义一些工具类,在运行时动态 define 到应用的 ClassLoader 里。

参考: `dubbo-test-instrument/src/main/resources/instrument.properties``define`配置。


## 编译开发

* 本项目依赖 bytekit: https://github.com/alibaba/bytekit ,可能需要先`mvn clean install` bytekit
Expand Down
18 changes: 18 additions & 0 deletions dubbo-test-instrument/src/main/java/com/hello/DubboUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hello;

import org.apache.dubbo.rpc.Invocation;

/**
*
* <pre>
* 在instrument.properties 里通过define字段指定的类,会在运行时define到应用的ClassLoader里。
* 可以在增强的代码里直接使用
* </pre>
*/
public class DubboUtils {

public static void test(Invocation invocation) {
System.err.println("DubboUtils: " + invocation.getServiceName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.alibaba.bytekit.agent.inst.Instrument;
import com.alibaba.bytekit.agent.inst.InstrumentApi;
import com.hello.DubboUtils;

/**
* @see org.apache.dubbo.rpc.Invoker
Expand All @@ -23,6 +24,7 @@ public abstract class Invoker {
* @throws RpcException
*/
public Result invoke(Invocation invocation) throws RpcException {
DubboUtils.test(invocation);
System.err.println("invoker class: " + this.getClass().getName());
Result result = InstrumentApi.invokeOrigin();
System.err.println("result:" + result + ", invoker class: " + this.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
instrument=org.apache.dubbo.monitor.support.MonitorFilter,org.apache.dubbo.rpc.test.Invoker
instrument=org.apache.dubbo.monitor.support.MonitorFilter,org.apache.dubbo.rpc.test.Invoker
define=com.hello.DubboUtils
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>
<oneagent.deps.package>com.alibaba.oneagent.deps</oneagent.deps.package>
<bytekit.version>0.0.5</bytekit.version>
<bytekit.version>0.0.8-SNAPSHOT</bytekit.version>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit 259d495

Please sign in to comment.