完成Dubbox的Rest调用测试

This commit is contained in:
zhangxin10 2015-11-19 07:56:31 +08:00
parent ea6f1ca758
commit e63bfe365c
7 changed files with 200 additions and 17 deletions

View File

@ -16,6 +16,12 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>Company nexus</id>
<url>http://223.202.119.155:18081/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -40,6 +46,48 @@
<version>3.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.7.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.7.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.7.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.0.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.0.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
@ -52,6 +100,17 @@
<version>0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbox</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>

View File

@ -5,13 +5,7 @@ import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
import com.ai.cloud.skywalking.model.ContextData;
import com.ai.cloud.skywalking.model.Identification;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcInvocation;
import com.alibaba.dubbo.rpc.*;
@Activate
public class SWDubboEnhanceFilter implements Filter {
@ -22,7 +16,7 @@ public class SWDubboEnhanceFilter implements Filter {
Result result = null;
if (isConsumer) {
RPCBuriedPointSender sender = new RPCBuriedPointSender();
ContextData contextData = sender.beforeSend(createIdentification(invoker));
ContextData contextData = sender.beforeSend(createIdentification(invoker, invocation));
// 追加参数
RpcInvocation rpcInvocation = (RpcInvocation) invocation;
rpcInvocation.setAttachment("contextData", contextData.toString());
@ -50,7 +44,7 @@ public class SWDubboEnhanceFilter implements Filter {
contextData = new ContextData(contextDataStr);
}
rpcBuriedPointReceiver.beforeReceived(contextData, createIdentification(invoker));
rpcBuriedPointReceiver.beforeReceived(contextData, createIdentification(invoker, invocation));
try {
//执行结果
@ -71,13 +65,22 @@ public class SWDubboEnhanceFilter implements Filter {
return result;
}
private static Identification createIdentification(Invoker<?> invoker) {
StringBuilder businessKey = new StringBuilder();
businessKey.append("IP:" + invoker.getUrl().getAddress());
businessKey.append("Host:" + invoker.getUrl().getHost());
businessKey.append("Port:" + invoker.getUrl().getPort());
businessKey.append("Protocol:" + invoker.getUrl().getProtocol());
return Identification.newBuilder().viewPoint(invoker.getUrl().getServiceInterface()).businessKey(businessKey.
toString()).spanType('D').build();
private static Identification createIdentification(Invoker<?> invoker, Invocation invocation) {
StringBuilder viewPoint = new StringBuilder();
viewPoint.append(invoker.getUrl().getProtocol() + "://");
viewPoint.append(invoker.getUrl().getHost());
viewPoint.append(":" + invoker.getUrl().getPort());
viewPoint.append(invoker.getUrl().getAbsolutePath());
viewPoint.append(invocation.getMethodName() + "(");
for (Class classes : invocation.getParameterTypes()) {
viewPoint.append(classes.getSimpleName() + ",");
}
if (invocation.getParameterTypes().length > 0) {
viewPoint.delete(viewPoint.length() - 1, viewPoint.length());
}
viewPoint.append(")");
return Identification.newBuilder().viewPoint(viewPoint.toString()).spanType('D').build();
}
}

View File

@ -0,0 +1,60 @@
package com.ai.cloud.skywalking.plugin.test.dubbox.rest.consumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class DubboxRestConsumer {
private static final Log logger = LogFactory.getLog(DubboxRestConsumer.class);
public static String sendPostRequest(String url, String data, Map<String, String> header) throws IOException,
URISyntaxException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(new URL(url).toURI());
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
StringEntity dataEntity = new StringEntity(data, ContentType.APPLICATION_JSON);
httpPost.setEntity(dataEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent()));
StringBuffer buffer = new StringBuffer();
String tempStr;
while ((tempStr = reader.readLine()) != null)
buffer.append(tempStr);
return buffer.toString();
} else {
throw new RuntimeException("error code " + response.getStatusLine().getStatusCode()
+ ":" + response.getStatusLine().getReasonPhrase());
}
} finally {
response.close();
httpclient.close();
}
}
public static void main(String[] args) throws IOException, URISyntaxException {
String url = "http://192.168.1.102:20880/skywalking/rest-a/doBusiness";
String data = "{\"paramA\":\"BBBB\"}";
sendPostRequest(url, data, new HashMap<String, String>());
}
}

View File

@ -0,0 +1,12 @@
package com.ai.cloud.skywalking.plugin.test.dubbox.rest.impl;
import com.ai.cloud.skywalking.plugin.test.dubbox.rest.interfaces.IDubboxRestInterA;
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class DubboxRestInterAImpl implements IDubboxRestInterA {
public String doBusiness(String paramA) {
System.out.println("param : " + paramA);
return "{\"content\":\"" + paramA + "\"}";
}
}

View File

@ -0,0 +1,17 @@
package com.ai.cloud.skywalking.plugin.test.dubbox.rest.impl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboxRestStart {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new
ClassPathXmlApplicationContext("classpath*:provider/dubbox-provider.xml");
classPathXmlApplicationContext.start();
while (true) {
Thread.sleep(100000L);
}
}
}

View File

@ -0,0 +1,17 @@
package com.ai.cloud.skywalking.plugin.test.dubbox.rest.interfaces;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/rest-a")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
public interface IDubboxRestInterA {
@Path("/doBusiness")
@POST
String doBusiness(String paramA);
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="skywalking-dubbo-rest-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="rest" port="20880" server="tomcat" contextpath="skywalking"/>
<dubbo:provider filter="swEnhanceFilter"/>
<bean id="dubboxRestInterA" class="com.ai.cloud.skywalking.plugin.test.dubbox.rest.impl.DubboxRestInterAImpl"/>
<dubbo:service interface="com.ai.cloud.skywalking.plugin.test.dubbox.rest.interfaces.IDubboxRestInterA" ref="dubboxRestInterA"/>
</beans>