您的位置:首页 > 理论基础 > 计算机网络

spring httpInvoker 性能优化

2016-06-01 14:49 531 查看
Spring HttpInvoke,一种较为常用的、基于Spring架构的服务器之间的远程调用实现,可以说是轻量级的RMI

 

1.在web.xml配置spring 并添加对应的spring配置文件

 

Xml代码  


<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代码  


<?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.首先定义接口

Java代码  


public interface UserService {     

    

     

    User getUser(String username);     

}   

 3.定义返回的model

用户类,注意实现Serializable接口,这是执行远程调用传递数据对象的第一要求——数据对象必须实现Serializable接口,因为,要执行序列化/反序列化操作!

Java代码  


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 实现

Java代码  


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代码  


<?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

 

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>  

</bean>  

 

   在java类里调用

  

Java代码  


@Resource(name="httpService")  

    IUserService userService;  

User user = userService.getUser("test");  

 

 7.优化

    如果我们这样写,其实默认调用了SimpleHttpInvokerRequestExecutor做实现,这个实现恐怕只能作为演示来用! 

这也是效率问题所在!!! 

为提高效率,应该通过Commons-HttpClient! 

我们需要做什么?导入这个jar,改改xml就行!

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>  

 

或者

  

Java代码  


<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连接不释放而被累死!

还可以控制线程数

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左右响应一个请求。 

这简直不是一个数量级!!!

MultiThreadedHttpConnectionManager的使用原理参考:http://liuinsect.iteye.com/blog/1886237
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: