Spring HttpInvoke实现,以及效率提升! 博客分类: Spring
2013-04-24 16:06
435 查看
最近接手服务器总被人质疑效率问题,说到底是质疑Spring HttpInvoke的效率问题。好在经过同事们的努力,找到了问题的根源,最终解决了这个问题。
我也顺道整理一下Spring HttpInvoke——那曾经最为熟悉的东西。
Spring HttpInvoke,一种较为常用的、基于Spring架构的服务器之间的远程调用实现,可以说是轻量级的RMI。
最初,我们使用Spring HttpInvoke同步配置数据,刷新多个服务器上的缓存,当然如果用分布式缓存是不是更好
!
使用Spring HttpInvoke,你可以调用远程接口,进行数据交互、业务逻辑操作等等。
废话不说了,上代码!
用户操作接口:
用户类,注意实现Serializable接口,这是执行远程调用传递数据对象的第一要求——数据对象必须实现Serializable接口,因为,要执行序列化/反序列化操作!
覆盖toString()方法,输出用户信息!
再看UserServiceImpl实现:
只把用户信息打出来即可说明调用效果!
看applicationContext.xml
我们要把userService暴露出去,这样外部就可以通过http接口调用这个接口的实现了。
说说HttpInvokerServiceExporter,这个类用来在服务器端包装需要暴露的接口。
熟悉service,定义具体的实现类!
熟悉serviceInterface指向需要暴露的接口,注意使用value标注接口名称!
最后再看servlet.xml配置
直接将请求指向刚才配置的userService
现在我们之间访问一下http://localhost:8080/spring/service/
这就说明,服务器端配置已经成功了!如果在日志中频繁得到这种异常,那很可能服务器被恶意访问了!
再看客户端实现:
我们做了什么?Nothing!就跟调用一般Spring容器中的实现一样!
再看applicationContext.xml:
这里我们可以通过Spring容器调用userService,而实际上,他是一个HttpInvokerProxyFactoryBean,在这个配置里,定义了访问地址serviceUrl,和访问接口serviceInterface。
执行测试!
如果我们这样写,其实默认调用了SimpleHttpInvokerRequestExecutor做实现,这个实现恐怕只能作为演示来用!
这也是效率问题所在!!!
为提高效率,应该通过Commons-HttpClient!
我们需要做什么?导入这个jar,改改xml就行!
通过HttpClient,我们可以配置超时时间timeout和连接超时connectionTimeout两个属性,这样,服务器执行操作时,如果超时就可以强行释放连接,这样可怜的tomcat不会因为HttpInvoke连接不释放而被累死!
回头看了一眼我N多年前的代码,万岁,我当时确实是这么实现的!好在没有犯低级错误!!!
执行操作!
这时,转为org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor实现了!
不过同事认为,这个效率还是不够高!!!
再改,改什么?还是xml!
改用MultiThreadedHttpConnectionManager,多线程!!!
测试就不说了,实践证明:
默认实现,服务器平均10s左右才能响应一个请求。
多线程实现,服务器平均20ms左右响应一个请求。
这简直不是一个数量级!!!
注意:在HttpClient的3.1版本中,已不支持如下配置,相应的方法已经废弃!
如果仔细看看文档,
引用
HttpClient that uses a default MultiThreadedHttpConnectionManager.
commons 系列的实现怎么会不考虑多线程呢?人家默认实现就是多线程的!同事多虑了!
当然,同事还补充了一句,需要控制连接数!
难怪,这里要设置
默认啥情况?
引用
maxConnectionsPerHost 每个主机的最大并行链接数,默认为2
public static final int DEFAULT_MAX_HOST_CONNECTIONS = 2;
maxTotalConnections 客户端总并行链接最大数,默认为20
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20;
我也顺道整理一下Spring HttpInvoke——那曾经最为熟悉的东西。
Spring HttpInvoke,一种较为常用的、基于Spring架构的服务器之间的远程调用实现,可以说是轻量级的RMI。
最初,我们使用Spring HttpInvoke同步配置数据,刷新多个服务器上的缓存,当然如果用分布式缓存是不是更好
!
使用Spring HttpInvoke,你可以调用远程接口,进行数据交互、业务逻辑操作等等。
废话不说了,上代码!
用户操作接口:
/** * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @since 1.0 */ public interface UserService { /** * 获得用户 * * @param username * 用户名 * @return */ User getUser(String username); }
用户类,注意实现Serializable接口,这是执行远程调用传递数据对象的第一要求——数据对象必须实现Serializable接口,因为,要执行序列化/反序列化操作!
/** * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @since 1.0 */ public class User implements Serializable { private static final long serialVersionUID = 5590768569302443813L; private String username; private Date birthday; /** * @param username * @param birthday */ public User(String username, Date birthday) { this.username = username; this.birthday = birthday; } // 省略 /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return String.format("%s\t%s\t", username, birthday); } }
覆盖toString()方法,输出用户信息!
再看UserServiceImpl实现:
/** * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @since 1.0 */ public class UserServiceImpl implements UserService { private Logger logger = Logger.getLogger(UserServiceImpl.class); /* * (non-Javadoc) * * @see * org.zlex.spring.httpinvoke.service.UserService#getUser(java.lang.String) */ @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; } }
只把用户信息打出来即可说明调用效果!
看applicationContext.xml
<bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service"> <bean class="org.zlex.spring.httpinvoke.service.impl.UserServiceImpl" /> </property> <property name="serviceInterface" value="org.zlex.spring.httpinvoke.service.UserService" /> </bean>
我们要把userService暴露出去,这样外部就可以通过http接口调用这个接口的实现了。
说说HttpInvokerServiceExporter,这个类用来在服务器端包装需要暴露的接口。
熟悉service,定义具体的实现类!
<property name="service"> <bean class="org.zlex.spring.httpinvoke.service.impl.UserServiceImpl" /> </property>
熟悉serviceInterface指向需要暴露的接口,注意使用value标注接口名称!
<property name="serviceInterface" value="org.zlex.spring.httpinvoke.service.UserService" />
最后再看servlet.xml配置
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="*" value-ref="userService" /> </map> </property> </bean>
直接将请求指向刚才配置的userService
现在我们之间访问一下http://localhost:8080/spring/service/
这就说明,服务器端配置已经成功了!如果在日志中频繁得到这种异常,那很可能服务器被恶意访问了!
再看客户端实现:
/** * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> * @since 1.0 */ public class UserServiceTest { private Logger logger = Logger.getLogger(UserServiceTest.class); private ApplicationContext context; private UserService userService; @Before public void initialize() { context = new ClassPathXmlApplicationContext("applicationContext.xml"); userService = (UserService) context.getBean("userService"); } @Test public void getUser() { User user = userService.getUser("zlex"); if (logger.isDebugEnabled()) { logger.debug("user[" + user + "]"); } } }
我们做了什么?Nothing!就跟调用一般Spring容器中的实现一样!
再看applicationContext.xml:
<bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/spring/service" /> <property name="serviceInterface" value="org.zlex.spring.httpinvoke.service.UserService" /> </bean>
这里我们可以通过Spring容器调用userService,而实际上,他是一个HttpInvokerProxyFactoryBean,在这个配置里,定义了访问地址serviceUrl,和访问接口serviceInterface。
执行测试!
如果我们这样写,其实默认调用了SimpleHttpInvokerRequestExecutor做实现,这个实现恐怕只能作为演示来用!
这也是效率问题所在!!!
为提高效率,应该通过Commons-HttpClient!
我们需要做什么?导入这个jar,改改xml就行!
<bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/spring/service" /> <property name="serviceInterface" value="org.zlex.spring.httpinvoke.service.UserService" /> <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>
通过HttpClient,我们可以配置超时时间timeout和连接超时connectionTimeout两个属性,这样,服务器执行操作时,如果超时就可以强行释放连接,这样可怜的tomcat不会因为HttpInvoke连接不释放而被累死!
回头看了一眼我N多年前的代码,万岁,我当时确实是这么实现的!好在没有犯低级错误!!!
执行操作!
这时,转为org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor实现了!
不过同事认为,这个效率还是不够高!!!
再改,改什么?还是xml!
<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左右响应一个请求。
这简直不是一个数量级!!!
注意:在HttpClient的3.1版本中,已不支持如下配置,相应的方法已经废弃!
<property name="connectionTimeout" value="2000" /> <property name="timeout" value="5000" />
如果仔细看看文档,
引用
HttpClient that uses a default MultiThreadedHttpConnectionManager.
commons 系列的实现怎么会不考虑多线程呢?人家默认实现就是多线程的!同事多虑了!
当然,同事还补充了一句,需要控制连接数!
难怪,这里要设置
<bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams"> <property name="maxTotalConnections" value="600" /> <property name="defaultMaxConnectionsPerHost" value="512" /> </bean>
默认啥情况?
引用
maxConnectionsPerHost 每个主机的最大并行链接数,默认为2
public static final int DEFAULT_MAX_HOST_CONNECTIONS = 2;
maxTotalConnections 客户端总并行链接最大数,默认为20
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 20;
相关文章推荐
- Spring HttpInvoke实现,以及效率提升!
- Spring HttpInvoke实现,以及效率提升!
- Spring HttpInvoke实现和效率提升
- Spring Httpinvoke实现对外接口及效率提升
- Spring HttpInvoke 客户端调用代码实现
- redis集群环境搭建以及java中jedis客户端集群代码实现 博客分类: redis
- Spring MVC实现QQ第三方登录 博客分类: spring
- redis集群环境搭建以及java中jedis客户端集群代码实现 博客分类: redis
- spring实现AOP的两种方式以及实现动态代理方式
- paip.提升效率---filter map reduce 的java 函数式编程实现
- Spring整理系列(11)——@Configuration注解、@Bean注解以及配置自动扫描、bean作用域 发表于2016/8/11 15:16:16 10628人阅读 分类: Spring
- httpinvoke客户端代码实现
- 如何在DataGrid控件中实现编辑、删除、分类以及分页操作
- 推荐大家去博客园看我的博客:http://www.cnblogs.com/bj-xy/点击随笔分类,Python基础学习
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- VSM向量空间模型对文本的分类以及简单实现
- SpringBoot开启https以及http重定向
- 事务有哪些特性?spring的事务管理有几种方式实现,如何实现?spring 中常用的两种事务配置方式以及事务的传播性、隔离级别
- 二分类问题打标签label以及求loss的选择——从accuracy底层实现代码理解
- iOS中实现数据交换的几种方法介绍以及其效率对比