Spring HttpInvoke,一种较为常用的、基于Spring架构的服务器之间的远程调用实现,可以说是轻量级的RMI
1.在web.xml配置spring 并添加对应的spring配置文件
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
对应的需要添加 service-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>
2.首先定义接口
public interface UserService {
User getUser(String username);
}
3.定义返回的model
用户类,注意实现Serializable接口,这是执行远程调用传递数据对象的第一要求——数据对象必须实现Serializable接口,因为,要执行序列化/反序列化操作!
public class User implements Serializable {
private static final long serialVersionUID = 5590768569302443813L;
private String username;
private Date birthday;
public User(String username, Date birthday) {
this.username = username;
this.birthday = birthday;
}
@Override
public String toString() {
return String.format("%s\t%s\t", username, birthday);
}
}
4. userservice 实现
public class UserServiceImpl implements UserService {
private Logger logger = Logger.getLogger(UserServiceImpl.class);
@Override
public User getUser(String username) {
if (logger.isDebugEnabled()) {
logger.debug("username:[" + username + "]");
}
User user = new User(username, new Date());
if (logger.isDebugEnabled()) {
logger.debug("user:[" + user + "]");
}
return user;
}
}
5.发不成http服务
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="userService" />
<property name="serviceInterface" value="com.mpupa.service.IUserService" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/httpService">httpService</prop>
</props>
</property>
</bean>
</beans>
6.客户端调用
首先要把 IUserService接口以及需要返回的Model放到客户端,打jar包或者直接把类拷贝过去
然后配置 bean
<bean id="httpService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value> https://apps.dotter.me/consumer/service/userHttpService
</value>
</property>
<property name="serviceInterface" value="com.mpupa.service.IUserService">
</property>
</bean>
在java类里调用
@Resource(name="httpService")
IUserService userService;
User user = userService.getUser("test");
7.优化
如果我们这样写,其实默认调用了SimpleHttpInvokerRequestExecutor做实现,这个实现恐怕只能作为演示来用!
这也是效率问题所在!!!
为提高效率,应该通过Commons-HttpClient!
我们需要做什么?导入这个jar,改改xml就行!
<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value> https://apps.dotter.me/consumer/service/userHttpService
</value>
</property>
<property name="serviceInterface" value="com.mpupa.service.IUserService">
</property>
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<property name="readTimeout" value="5000" />
<property name="connectTimeout" value="5000" />
</bean>
</property>
</bean>
或者
<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value> https://apps.dotter.me/consumer/service/userHttpService
</value>
</property>
<property name="serviceInterface" value="com.mpupa.service.IUserService">
</property>
<property
name="httpInvokerRequestExecutor">
<ref
bean="httpInvokerRequestExecutor" />
</property>
</bean>
<bean
id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<property
name="httpClient">
<bean
class="org.apache.commons.httpclient.HttpClient">
<property
name="connectionTimeout"
value="2000" />
<property
name="timeout"
value="5000" />
</bean>
</property>
</bean>
这时,转为org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor实现了!,通过HttpClient,我们可以配置超时时间timeout和连接超时connectionTimeout两个属性,这样,服务器执行操作时,如果超时就可以强行释放连接,这样可怜的tomcat不会因为HttpInvoke连接不释放而被累死!
还可以控制线程数
<bean
id="httpInvokerRequestExecutor"
class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor">
<property
name="httpClient">
<bean
class="org.apache.commons.httpclient.HttpClient">
<property
name="connectionTimeout"
value="2000" />
<property
name="timeout"
value="5000" />
<property
name="httpConnectionManager">
<ref
bean="multiThreadedHttpConnectionManager" />
</property>
</bean>
</property>
</bean>
<bean
id="multiThreadedHttpConnectionManager"
class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property
name="params">
<bean
class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
<property
name="maxTotalConnections"
value="600" />
<property
name="defaultMaxConnectionsPerHost"
value="512" />
</bean>
</property>
</bean>
改用MultiThreadedHttpConnectionManager,多线程!!!
测试就不说了,实践证明:
默认实现,服务器平均10s左右才能响应一个请求。
多线程实现,服务器平均20ms左右响应一个请求。
这简直不是一个数量级!!!
分享到:
相关推荐
公司内部讲义,比较了SOA,RMI和Spring HttpInvoker。并介绍了Spring HttpInvoker的基本使用方法。
Spring HttpInvoker,是一套基于Maven+Spring+SpringMVC+MyBatis框架,还包含了Invoker的客户端及服务器端的demo实例
Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和返回值是比较复杂的,通过httpInvoker有巨大的优势。 1. 远程访问流程 1) 服务端定义服务接口 2) 服务端实现服务接口 3) 暴露服务...
org.springframework.remoting.httpinvoker最基本的实现例子,这是3个eclipse的JavaEE工程,全部导入即可,能运行。 初学可以轻松了解Spring HttpInvoker 的结构和使用。
NULL 博文链接:https://tou3921.iteye.com/blog/1729111
spring+httpInvoker 实例,含服务端和客户端代码,myEclipse工程,直接导入就可以测试,适合初学者. 提供 普通参数返回值和自定义对象返回值,两个方式.
Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的...
NULL 博文链接:https://lggege.iteye.com/blog/369151
springboot-httpinvoker-demo.zip
和Hessian的轻量级传输协议不同的是,Spring HttpInvoker使用Java序列化来序列化参数和返回值,然后基于HTTP协议传输经序列化后的对象。当参数或返回值是复杂类型,并且不能通过Hessian的序列化机制序列化时,...
http invoker 在开发完http接口后可以做http post测试
基于Spring的HttpInvoker实现改写服务器端调用: HttpInvoker.invoker 方法,设置InvokerProcess处理客户端调用: ProxyFactory.proxy 方法,生成接口的代理对象,直接调用方法客户端和服务器端的接口和实体类要...
NULL 博文链接:https://lggege.iteye.com/blog/404124
Hessian 、 HttpInvoker 、 XFire 、 Axis
里面有两个工程,一个模拟服务端,一个模拟客户端。演示了最近本的httpinvoker的使用
Hessian和Burlap都是基于HTTP的,他们... Spring开发团队意识到RMI服务和基于HTTP的服务之前的空白,Spring的HttpInvoker应运而生。 Spring的HttpInvoker,它基于HTTP之上提供RPC,同时又使用了Java的对象序列化机
接口测试工具,不太好用,不建议使用,自己传着玩儿的
包含Jmail例子,AOP面向切面编程的日志处理例子,HttpInvoker的例子,RMI远程调用的例子,定时任务的例子,自己没事写的,简单易懂
模拟提交GET/POST,鞥呢返回详细头,cookies
可以方便的实现http消息调试,比控制台F12开发者工具中显示的更加详细透彻