springBoot服务调用-rmi,httpinvoke,hession
完整代码下载链接:
https://github.com/2010yhh/springBoot-demos/tree/master/springboot-invoke
环境
idea2018,jdk1.8,
springboot版本:springboot1.5.9.RELEASE,
远程服务调用:测试3种rmi,httpinvoke,hession方式。
1.Rmi
服务端服务暴露配置:
<!--spring httpInvoker -->
<bean name="testServiceImpl" class="com.ctg.test.api.serviceimpl.TestServiceImpl"/>
<bean id="testService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="testServiceImpl"/>
</property>
<property name="serviceInterface" value="com.ctg.test.api.service.TestService"></property>
</bean>
客户端调用:
使用RmiProxyFactoryBean(这里没有使用配置bean的方式)
2.httpinvoke
服务端服务暴露配置:
<!--spring httpInvoker -->
<bean name="testServiceImpl" class="com.ctg.test.api.serviceimpl.TestServiceImpl"/>
<bean id="testService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="testServiceImpl"/>
</property>
<property name="serviceInterface" value="com.ctg.test.api.service.TestService"></property>
</bean>
客户端调用:
HttpInvokerProxyFactoryBean
3.hessin
服务端服务暴露配置:
<!-- spring hessian -->
<bean id="testServiceImpl3" class="com.ctg.test.api.serviceimpl.TestServiceImpl3"></bean>
<bean name="/testService3" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="testServiceImpl3" />
<property name="serviceInterface" value="com.ctg.test.api.service.TestService3" />
</bean>
客户端调用:
HessianProxyFactory
注意:3种调用方式,使用时,url写法不一样,
@Controller
public class TestController {
@RequestMapping("/testHttpinvoke")
@ResponseBody
public ResponseDo testHttpinvoke(String name)throws RemoteException
{
String url="http://127.0.0.1:8761/api/testService";
ResponseDo responseDo=new ResponseDo();
responseDo.setResult("service is null");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowdayTime = dateFormat.format(new Date());
responseDo.setDate(nowdayTime);
TestService testService= HttpInvokerProxyUtil.getInstance().doRefer(TestService.class,url);
if(testService!=null){
responseDo= testService.getResponse(name);
}
return responseDo;
}
@RequestMapping("/testRmi")
@ResponseBody
public ResponseDo testRmi(String name)throws RemoteException
{
String url="rmi://127.0.0.1:8760/testService2";
ResponseDo responseDo=new ResponseDo();
responseDo.setResult("service is null");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowdayTime = dateFormat.format(new Date());
responseDo.setDate(nowdayTime);
TestService2 testService2= RmiProxyUtil.getInstance().doRefer(TestService2.class,url);
if(testService2!=null){
responseDo= testService2.getResponse(name);
}
return responseDo;
}
@RequestMapping("/testHessian")
@ResponseBody
public ResponseDo testHessian(String name)throws RemoteException,MalformedURLException
{
String url = "http://localhost:8761/api/testService3";
ResponseDo responseDo=new ResponseDo();
responseDo.setResult("service is null");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowdayTime = dateFormat.format(new Date());
responseDo.setDate(nowdayTime);
TestService3 testService3 =HessinProxyUtil.getInstance().doRefer(TestService3.class,url);
if(testService3!=null){
responseDo= testService3.getResponse(name);
}
return responseDo;
}
}
4.测试
启动服务端,启动客户端:
测试结果:
rmi:
httpinvoke:
hessin:
用jmeter压测发现,数据量小时,rmi和httpinvoke,hessin平均调用耗时差不多;数据量大时,应该是rmi效率>hessin>httpinvoke
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/18397.html