java rmi 與 spring mvc rest 效能簡單對比
場景描述:
服務端用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 = 0; i < 10; i++) {
new Thread(() -> {
long restStart = System.currentTimeMillis();
String restContent = "";
try {
restContent = 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("本次耗時:{}", (System.currentTimeMillis() - restStart));
}).start();
}
TimeUnit.SECONDS.sleep(3);
System.out.println("--------------------------------->");
log.info("rmi testing ...");
for (int i = 0; i < 10; i++) {
new Thread(() -> {
long rmiStart = System.currentTimeMillis();
int restCode = 0;
try {
RmiRest rr = (RmiRest) Naming.lookup("rmi://localhost:8888/rr");
MyBean b = rr.getBean();
log.info("rmi return -> {}", b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.info("本次耗時:{}", (System.currentTimeMillis() - rmiStart));
}).start();
}
}
}
OK,一段簡單的程式碼,進行一下簡單的對比測試,全程裸奔,那麼看一下結果:
程式猿能用程式碼說明的問題咱就用圖片說明。。。
注:本文不討論rest和RMI 甚至 RPC方案的優劣,不涉及跨平臺等等屬性,僅僅是在純JAVA方案的前提下,測試rest與rmi兩種方案的效能問題,本測試也不是十分嚴謹,事實上,這種測試是很不公平的,可以說拿RMI的優勢來對比REST的劣勢,非常偏,但在純JAVA系統中,只關心效能的情況下,應該還是有一定的參考意義的;
另外,據說jersey的實現方案效能要比spring mvc強,下次做一下這方面的相關測試;