场景描述:
服务端用rest和rmi发布两个接口,两个接口提供同样的功能,获取同一个bean;
服务端用httpclient和rmi分别起十个线程调用,打印时间;
废话少说,程序猿用代码说话:
首先定义一个Bean:
package net.tt.rest.domain;
import java.io.Serializable;
import lombok.Data;
/**
*
* @author tbc
* @version 1.0 {2016年6月13日 上午11:29:56}
*/
@Data
public class MyBean implements Serializable {
private static final long serialVersionUID = -3442523325988510637L;
private String uuid;
private String name;
private String value;
public static MyBeanBuilder builder() {
return new MyBeanBuilder();
}
public static class MyBeanBuilder {
private String uuid;
private String name;
private String value;
public MyBeanBuilder uuid(String uuid) {
this.uuid = uuid;
return this;
}
public MyBeanBuilder name(String name) {
this.name = name;
return this;
}
public MyBeanBuilder value(String value) {
this.value = value;
return this;
}
public MyBean build() {
MyBean b = new MyBean();
b.setName(name);
b.setUuid(uuid);
b.setValue(value);
return b;
}
}
}
要发布的接口:
package net.tt.rest.service;
import java.rmi.Remote;
import java.rmi.RemoteException;
import net.tt.rest.domain.MyBean;
/**
*
* @author tbc
* @version 1.0 {2016年6月13日 上午10:14:43}
*/
public interface RmiRest extends Remote {
MyBean getBean() throws RemoteException;
}
接口实现:
package net.tt.rest.service.impl;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import net.tt.rest.domain.MyBean;
import net.tt.rest.service.RmiRest;
/**
* UnicastRemoteObject用于导出的远程对象和获得与该远程对象通信的存根。
*
* @author tbc
* @version 1.0 {2016年6月13日 上午10:39:26}
*/
@Service("rmiRestImpl")
public class RmiRestImpl extends UnicastRemoteObject implements RmiRest {
private static final long serialVersionUID = 1L;
private Logger log = LoggerFactory.getLogger(RmiRestImpl.class);
public RmiRestImpl() throws RemoteException {
}
@Override
public MyBean getBean() throws RemoteException {
MyBean bean = MyBean.builder()//
.uuid(UUID.randomUUID().toString())//
.name("MyName")//
.value("thid is a test!")//
.build();
return bean;
}
}
spring mvc controller ->
package net.tt.rest.controller;
import java.rmi.RemoteException;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Maps;
import net.tt.rest.domain.MyBean;
import net.tt.rest.service.RmiRest;
/**
*
* @author tbc
* @version 1.0 {2016年6月13日 上午10:57:03}
*/
@RestController
public class MyRestController {
private Logger log = LoggerFactory.getLogger(MyRestController.class);
@Resource(name = "rmiRestImpl")
RmiRest rmiRest;
@RequestMapping(value = "bean", method = { RequestMethod.GET })
public MyBean getBean() throws RemoteException {
return rmiRest.getBean();
}
}
监听器,发布RMI :
package net.tt.rest.listener;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tt.rest.service.RmiRest;
import net.tt.rest.service.impl.RmiRestImpl;
/**
*
* @author tbc
* @version 1.0 {2016年6月13日 上午10:44:13}
*/
@WebListener("rmi")
public class MyServletContextListener implements ServletContextListener {
private Logger log = LoggerFactory.getLogger(MyServletContextListener.class);
@Override
public void contextInitialized(ServletContextEvent sce) {
log.info("启动监听,启动rmi服务...");
try {
RmiRest rr = new RmiRestImpl();
LocateRegistry.createRegistry(8888);
Naming.bind("rmi://localhost:8888/rr", rr);
log.info("远程RmiRest对象绑定成功--->");
} catch (RemoteException e) {
log.error("创建远程对象发生异常");
e.printStackTrace();
} catch (MalformedURLException e) {
log.error("发生重复绑定对象异常!");
e.printStackTrace();
} catch (AlreadyBoundException e) {
log.error("发生URL畸形异常!");
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
log.info("销毁监听器...");
}
}
OK,到此服务端代码就完成了,至于spring及监听的相关配置,不是本文重点,不再赘述,启动jetty,OK,成功!
下面写客户端代码进行测试:
package net.tt.mytest.rmirest;
import java.io.IOException;
import java.rmi.Naming;
import java.util.concurrent.TimeUnit;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tt.rest.domain.MyBean;
import net.tt.rest.service.RmiRest;
/**
*
* @author tbc
* @version 1.0 {2016年6月13日 上午11:15:05}
*/
public class RmiRestTest {
private static Logger log = LoggerFactory.getLogger(RmiRestTest.class);
public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
log.info("springMvcRest testing ...");
for (int i &#61; 0; i <10; i&#43;&#43;) {
new Thread(() -> {
long restStart &#61; System.currentTimeMillis();
String restContent &#61; "";
try {
restContent &#61; Request.Get("http://localhost:8080/spring/bean")//
.execute()//
.returnContent()//
.asString();
log.info("rest return -> {}", restContent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// log.info("exec result : {}", restCode);
log.info("本次耗时&#xff1a;{}", (System.currentTimeMillis() - restStart));
}).start();
}
TimeUnit.SECONDS.sleep(3);
System.out.println("--------------------------------->");
log.info("rmi testing ...");
for (int i &#61; 0; i <10; i&#43;&#43;) {
new Thread(() -> {
long rmiStart &#61; System.currentTimeMillis();
int restCode &#61; 0;
try {
RmiRest rr &#61; (RmiRest) Naming.lookup("rmi://localhost:8888/rr");
MyBean b &#61; rr.getBean();
log.info("rmi return -> {}", b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("本次耗时&#xff1a;{}", (System.currentTimeMillis() - rmiStart));
}).start();
}
}
}
OK&#xff0c;一段简单的代码&#xff0c;进行一下简单的对比测试&#xff0c;全程裸奔&#xff0c;那么看一下结果&#xff1a;
程序猿能用代码说明的问题咱就用图片说明。。。
注&#xff1a;本文不讨论rest和RMI 甚至 RPC方案的优劣&#xff0c;不涉及跨平台等等属性&#xff0c;仅仅是在纯JAVA方案的前提下&#xff0c;测试rest与rmi两种方案的性能问题&#xff0c;本测试也不是十分严谨&#xff0c;事实上&#xff0c;这种测试是很不公平的&#xff0c;可以说拿RMI的优势来对比REST的劣势&#xff0c;非常偏&#xff0c;但在纯JAVA系统中&#xff0c;只关心性能的情况下&#xff0c;应该还是有一定的参考意义的&#xff1b;
另外&#xff0c;据说jersey的实现方案性能要比spring mvc强&#xff0c;下次做一下这方面的相关测试&#xff1b;
转自https://blog.csdn.net/tbc3697/article/details/51656637