Skip to content

Commit

Permalink
Support implicit parameters (#87)
Browse files Browse the repository at this point in the history
1. fix dubbo 2.7.3 Generic bug apache/dubbo#4787
2. support implicit parameters. #85
  • Loading branch information
ningyu1 authored Sep 4, 2019
1 parent dd20b8d commit 5fbadb6
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ public class DubboCommonPanel {
private JTextField connectionsText;
private JComboBox<String> loadbalanceText;
private JComboBox<String> asyncText;
//参数表格
private DefaultTableModel model;
private String[] columnNames = {"paramType", "paramValue"};
private String[] tmpRow = {"", ""};
//隐式参数表格
private DefaultTableModel modelAttachment;
private String[] columnNamesAttachment = {"key", "value"};
private int textColumns = 2;
private JAutoCompleteComboBox<String> interfaceList;
private JAutoCompleteComboBox<String> methodList;
Expand Down Expand Up @@ -262,12 +266,11 @@ public void propertyChange(PropertyChangeEvent evt) {
mh.add(makeHelper("The service method name"));
interfaceSettings.add(mh);

//表格panel
//选项卡
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
//接口参数表格
JPanel tablePanel = new HorizontalPanel();
//Args
JLabel argsLable = new JLabel(" Args:", SwingConstants.RIGHT);
model = new DefaultTableModel();
// model.setDataVector(new String[][]{{"", ""}}, columnNames);
model.setDataVector(null, columnNames);
final JTable table = new JTable(model);
table.setRowHeight(40);
Expand All @@ -293,11 +296,45 @@ public void actionPerformed(ActionEvent arg0) {
});
//表格滚动条
JScrollPane scrollpane = new JScrollPane(table);
tablePanel.add(argsLable);
tablePanel.add(scrollpane);
tablePanel.add(addBtn);
tablePanel.add(delBtn);
interfaceSettings.add(tablePanel);
tabbedPane.add("Args",tablePanel);

//隐式参数表格
JPanel tablePanelAttachment = new HorizontalPanel();
modelAttachment = new DefaultTableModel();
modelAttachment.setDataVector(null, columnNamesAttachment);
final JTable tableAttachment = new JTable(modelAttachment);
tableAttachment.setRowHeight(40);
//失去光标退出编辑
tableAttachment.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
//添加按钮
JButton addBtnAttachment = new JButton("增加");
addBtnAttachment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
modelAttachment.addRow(tmpRow);
}
});
JButton delBtnAttachment = new JButton("删除");
delBtnAttachment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int rowIndex = tableAttachment.getSelectedRow();
if(rowIndex != -1) {
modelAttachment.removeRow(rowIndex);
}
}
});
//表格滚动条
JScrollPane scrollpaneAttachment = new JScrollPane(tableAttachment);
tablePanelAttachment.add(scrollpaneAttachment);
tablePanelAttachment.add(addBtnAttachment);
tablePanelAttachment.add(delBtnAttachment);
tabbedPane.add("Attachment Args",tablePanelAttachment);

interfaceSettings.add(tabbedPane);
return interfaceSettings;
}

Expand Down Expand Up @@ -329,6 +366,10 @@ public void configureInterface(TestElement element) {
columnNames.add("paramType");
columnNames.add("paramValue");
model.setDataVector(paserMethodArgsData(Constants.getMethodArgs(element)), columnNames);
Vector<String> columnNamesAttachment = new Vector<String>();
columnNamesAttachment.add("key");
columnNamesAttachment.add("value");
modelAttachment.setDataVector(paserMethodArgsData(Constants.getAttachmentArgs(element)), columnNamesAttachment);
}

public void modifyRegistry(TestElement element) {
Expand All @@ -353,6 +394,7 @@ public void modifyInterface(TestElement element) {
Constants.setInterfaceName(interfaceText.getText(), element);
Constants.setMethod(methodText.getText(), element);
Constants.setMethodArgs(getMethodArgsData(model.getDataVector()), element);
Constants.setAttachmentArgs(getMethodArgsData(modelAttachment.getDataVector()), element);
}
public void clearRegistry() {
registryProtocolText.setSelectedIndex(0);
Expand All @@ -376,6 +418,7 @@ public void clearInterface() {
interfaceText.setText("");
methodText.setText("");
model.setDataVector(null, columnNames);
modelAttachment.setDataVector(null, columnNamesAttachment);
}

private List<MethodArgument> getMethodArgsData(Vector<Vector<String>> data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
Expand All @@ -37,6 +38,8 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* DubboSample
Expand Down Expand Up @@ -88,6 +91,7 @@ private String getSampleData() {
sb.append("Interface: ").append(Constants.getInterface(this)).append("\n");
sb.append("Method: ").append(Constants.getMethod(this)).append("\n");
sb.append("Method Args: ").append(Constants.getMethodArgs(this).toString());
sb.append("Attachment Args: ").append(Constants.getAttachmentArgs(this).toString());
return sb.toString();
}

Expand Down Expand Up @@ -145,7 +149,14 @@ private Object callDubbo(SampleResult res) {
default:
// direct invoke provider
StringBuffer sb = new StringBuffer();
sb.append(Constants.getRpcProtocol(this)).append(Constants.getAddress(this)).append("/").append(Constants.getInterface(this));
sb.append(Constants.getRpcProtocol(this))
.append(Constants.getAddress(this))
.append("/").append(Constants.getInterface(this));
//# fix dubbo 2.7.3 Generic bug https://github.com/apache/dubbo/pull/4787
String version = Constants.getVersion(this);
if (!StringUtils.isBlank(version)) {
sb.append(":").append(version);
}
log.debug("rpc invoker url : " + sb.toString());
reference.setUrl(sb.toString());
}
Expand Down Expand Up @@ -262,6 +273,11 @@ public String generateKey(org.apache.dubbo.config.ReferenceConfig<?> referenceCo
parameterTypes = paramterTypeList.toArray(new String[paramterTypeList.size()]);
parameterValues = parameterValuesList.toArray(new Object[parameterValuesList.size()]);

List<MethodArgument> attachmentArgs = Constants.getAttachmentArgs(this);
if (attachmentArgs != null && !attachmentArgs.isEmpty()) {
RpcContext.getContext().setAttachments(attachmentArgs.stream().collect(Collectors.toMap(MethodArgument::getParamType, MethodArgument::getParamValue)));
}

res.sampleStart();
Object result = null;
try {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/io/github/ningyu/jmeter/plugin/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public class Constants {
public static final String FIELD_DUBBO_METHOD = "FIELD_DUBBO_METHOD";
public static final String FIELD_DUBBO_METHOD_ARGS = "FIELD_DUBBO_METHOD_ARGS";
public static final String FIELD_DUBBO_METHOD_ARGS_SIZE = "FIELD_DUBBO_METHOD_ARGS_SIZE";
public static final String FIELD_DUBBO_ATTACHMENT_ARGS = "FIELD_DUBBO_ATTACHMENT_ARGS";
public static final String FIELD_DUBBO_ATTACHMENT_ARGS_SIZE = "FIELD_DUBBO_ATTACHMENT_ARGS_SIZE";
public static final String DEFAULT_TIMEOUT = "1000";
public static final String DEFAULT_VERSION = "1.0";
public static final String DEFAULT_RETRIES = "0";
Expand Down Expand Up @@ -354,4 +356,35 @@ public static final void setMethodArgs(List<MethodArgument> methodArgs, TestElem
}
}

/**
* get attachmentArgs
* @return the attachmentArgs
*/
public static final List<MethodArgument> getAttachmentArgs(TestElement element) {
int paramsSize = element.getPropertyAsInt(FIELD_DUBBO_ATTACHMENT_ARGS_SIZE, 0);
List<MethodArgument> list = new ArrayList<MethodArgument>();
for (int i = 1; i <= paramsSize; i++) {
String paramType = element.getPropertyAsString(FIELD_DUBBO_ATTACHMENT_ARGS + "_KEY" + i);
String paramValue = element.getPropertyAsString(FIELD_DUBBO_ATTACHMENT_ARGS + "_VALUE" + i);
MethodArgument args = new MethodArgument(paramType, paramValue);
list.add(args);
}
return list;
}

/**
* set attachmentArgs
* @param methodArgs the attachmentArgs to set
*/
public static final void setAttachmentArgs(List<MethodArgument> methodArgs, TestElement element) {
int size = methodArgs == null ? 0 : methodArgs.size();
element.setProperty(new IntegerProperty(FIELD_DUBBO_ATTACHMENT_ARGS_SIZE, size));
if (size > 0) {
for (int i = 1; i <= methodArgs.size(); i++) {
element.setProperty(new StringProperty(FIELD_DUBBO_ATTACHMENT_ARGS + "_KEY" + i, methodArgs.get(i-1).getParamType()));
element.setProperty(new StringProperty(FIELD_DUBBO_ATTACHMENT_ARGS + "_VALUE" + i, methodArgs.get(i-1).getParamValue()));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.ningyu.jmeter.plugin;

import io.github.ningyu.jmeter.plugin.util.JsonUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericService;
import org.junit.Test;

public class GenericServiceTest {
@Test
public void test() {
ApplicationConfig application = new ApplicationConfig();
application.setName("api-generic-consumer");
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
reference.setUrl("dubbo://192.168.56.1:20880/org.apache.dubbo.samples.basic.api.DemoService");
// reference.setVersion("1.0.0");
reference.setTimeout(2000);
// reference.setGroup("test");
reference.setGeneric(true);
reference.setApplication(application);
reference.setInterface("com.jiuyescm.account.api.IUserService");
GenericService genericService = reference.get();
RpcContext.getContext().setAttachment("test.ningyu","this is attachmentValue");
// Object obj = genericService.$invoke("getUserById", new String[]{Long.class.getName()}, new Long[]{1L});
Object obj = genericService.$invoke("sayHello", new String[]{String.class.getName()}, new String[]{"ningyu"});
String json = JsonUtils.toJson(obj);
System.out.println(json);
}
}

0 comments on commit 5fbadb6

Please sign in to comment.