Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: redisson/kafka-provider子插件录制和mock #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ cp ./repeater-logback.xml ${REPEATER_TARGET_DIR}/cfg/repeater-logback.xml \
&& cp ../repeater-plugins/mybatis-plugin/target/mybatis-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/mybatis-plugin.jar \
&& cp ../repeater-plugins/dubbo-plugin/target/dubbo-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/dubbo-plugin.jar \
&& cp ../repeater-plugins/redis-plugin/target/redis-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/redis-plugin.jar \
&& cp ../repeater-plugins/redisson-plugin/target/redisson-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/redisson-plugin.jar \
&& cp ../repeater-plugins/kafka-plugin/target/kafka-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/kafka-plugin.jar \
&& cp ../repeater-plugins/http-plugin/target/http-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/http-plugin.jar \
&& cp ../repeater-plugins/hibernate-plugin/target/hibernate-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/hibernate-plugin.jar \
&& cp ../repeater-plugins/spring-data-jpa-plugin/target/spring-data-jpa-plugin-*-jar-with-dependencies.jar ${REPEATER_TARGET_DIR}/plugins/spring-data-jpa-plugin.jar
2 changes: 2 additions & 0 deletions bin/repeater-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"java-subInvoke",
"mybatis",
"ibatis",
"redisson",
"kafka-provider",
"dubbo-provider",
"dubbo-consumer"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class InvokeType implements java.io.Serializable {

public static InvokeType REDIS = new InvokeType("redis");

public static InvokeType REDISSON = new InvokeType("redisson");

public static InvokeType KAFKA = new InvokeType("kafka");

public static InvokeType DUBBO = new InvokeType("dubbo");

public static InvokeType HIBERNATE = new InvokeType("hibernate");
Expand Down
36 changes: 36 additions & 0 deletions repeater-plugins/kafka-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>repeater-plugins</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>kafka-plugin</artifactId>

<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.alibaba.jvm.sandbox.repeater.plugin.kafka;

import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.AbstractInvokePluginAdapter;
import com.alibaba.jvm.sandbox.repeater.plugin.core.model.EnhanceModel;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
import com.alibaba.jvm.sandbox.repeater.plugin.spi.InvokePlugin;
import com.google.common.collect.Lists;

import org.kohsuke.MetaInfServices;
import java.util.*;

/**
* {@link KafkaProviderPlugin} kafka-producer子插件
*
* @author quansong
* @version 1.0
*/
@MetaInfServices(InvokePlugin.class)
public class KafkaProviderPlugin extends AbstractInvokePluginAdapter {

@Override
protected List<EnhanceModel> getEnhanceModels() {
EnhanceModel redisson = EnhanceModel.builder()
.classPattern("org.apache.kafka.clients.producer.KafkaProducer")
.methodPatterns(EnhanceModel.MethodPattern.transform("doSend"))
.watchTypes(Event.Type.BEFORE, Event.Type.RETURN, Event.Type.THROWS)
.build();
return Lists.newArrayList(redisson);
}


@Override
protected InvocationProcessor getInvocationProcessor() {
return new KafkaProviderProcessor(getType());
}

@Override
public InvokeType getType() {
return InvokeType.KAFKA;
}

@Override
public String identity() {
return "kafka-provider";
}

@Override
public boolean isEntrance() {
return false;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.jvm.sandbox.repeater.plugin.kafka;

import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.api.DefaultInvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;

/**
* {@link KafkaProviderProcessor} kafka-producer子插件
*
* @author quansong
* @version 1.0
*/
class KafkaProviderProcessor extends DefaultInvocationProcessor {

KafkaProviderProcessor(InvokeType type) {
super(type);
}

@Override
public Object[] assembleRequest(BeforeEvent event) {
// KafkaProducer#doSend(ProducerRecord<K, V> record, Callback callback)
// callback存在不可序序列化异常
return new Object[]{event.argumentArray[0]};
}

}
2 changes: 2 additions & 0 deletions repeater-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<module>http-plugin</module>
<module>dubbo-plugin</module>
<module>redis-plugin</module>
<module>redisson-plugin</module>
<module>kafka-plugin</module>
<module>hibernate-plugin</module>
<module>spring-data-jpa-plugin</module>
<module>socketio-plugin</module>
Expand Down
36 changes: 36 additions & 0 deletions repeater-plugins/redisson-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>repeater-plugins</artifactId>
<groupId>com.alibaba.jvm.sandbox</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>redisson-plugin</artifactId>

<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.jvm.sandbox.repeater.plugin.redisson;

import com.alibaba.jvm.sandbox.api.ProcessControlException;
import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationListener;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.api.DefaultEventListener;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.*;

/**
* {@link RedissonListener} redis客户端redisson子插件
*
* @author quansong
* @version 1.0
*/
public class RedissonListener extends DefaultEventListener {

private static final String REDISSON_CLAAS_PATH = "org.redisson.Redisson";

RedissonListener(InvokeType invokeType,
boolean entrance,
InvocationListener listener,
InvocationProcessor processor) {
super(invokeType, entrance, listener, processor);
}

/**
* 处理before事件
* 通配符过滤Redisson类
*
* @param event before事件
*/
@Override
protected void doBefore(BeforeEvent event) throws ProcessControlException {
if(REDISSON_CLAAS_PATH.equalsIgnoreCase(event.javaClassName)){
return;
}
super.doBefore(event);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.alibaba.jvm.sandbox.repeater.plugin.redisson;

import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.api.listener.EventListener;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationListener;
import com.alibaba.jvm.sandbox.repeater.plugin.api.InvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.AbstractInvokePluginAdapter;
import com.alibaba.jvm.sandbox.repeater.plugin.core.model.EnhanceModel;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
import com.alibaba.jvm.sandbox.repeater.plugin.spi.InvokePlugin;
import com.google.common.collect.Lists;

import org.kohsuke.MetaInfServices;
import java.util.*;

/**
* {@link RedissonPlugin} redis客户端redisson子插件
*
* @author quansong
* @version 1.0
*/
@MetaInfServices(InvokePlugin.class)
public class RedissonPlugin extends AbstractInvokePluginAdapter {

@Override
protected List<EnhanceModel> getEnhanceModels() {
EnhanceModel redisson = EnhanceModel.builder()
.classPattern("org.redisson.Redisson*")
.methodPatterns(EnhanceModel.MethodPattern.transform("*"))
.watchTypes(Event.Type.BEFORE, Event.Type.RETURN, Event.Type.THROWS)
.build();
return Lists.newArrayList(redisson);
}


@Override
protected InvocationProcessor getInvocationProcessor() {
return new RedissonProcessor(getType());
}

@Override
public InvokeType getType() {
return InvokeType.REDISSON;
}

@Override
public String identity() {
return "redisson";
}

@Override
public boolean isEntrance() {
return false;
}


@Override
protected EventListener getEventListener(InvocationListener listener) {
return new RedissonListener(getType(), isEntrance(), listener, getInvocationProcessor());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.alibaba.jvm.sandbox.repeater.plugin.redisson;

import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.api.DefaultInvocationProcessor;
import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;

/**
* {@link RedissonProcessor} redis客户端redisson子插件
*
* @author quansong
* @version 1.0
*/
class RedissonProcessor extends DefaultInvocationProcessor {

RedissonProcessor(InvokeType type) {
super(type);
}

@Override
public Object[] assembleRequest(BeforeEvent event) {
Object mapperMethod = event.target;
Object name = null;
Field field = FieldUtils.getField(mapperMethod.getClass(), "name", true);
if(field == null){
return new Object[]{event.argumentArray};
}
try {
name = field.get(mapperMethod);
return new Object[]{name, event.argumentArray};
} catch (IllegalAccessException e) {
return new Object[]{event.argumentArray};
}
}

}