http://zhidao.baidu.com/link?url=6FrnwvBQEZhjM-ooNCuiAra7T6qi9FsFhFvkHBKaOjqovZR86OCsIePi-05nM-fxRrlInEGbElSxlhgO6X7JsaGNdQdNrQ2xE58wglgeQO3
http://blog.csdn.net/liaq325/article/details/8281550
摘自以上
spring httpInvoke
spring httpInvoke 基于spring架构的服务器之间的远程调用实现。通过spring httpInvoke,可以调用远程接口,进行数据交互、业务逻辑操作
服务器端:(被调用一方)
- public class User implements Serializable{//必须实现serializable接口,远程调用的基础
- private String username;
- private Date birthday;
- //构造方法
- //set get 方法
- }
- public interface UserService{
- User getUser(String username);
- }
- public UserServiceImpl implements UserService{
- //实现userService
- }
重要的配置文件来了。。。。remote-servlet.xml放在项目根目录下面,跟web.xml相同的级别暴露给调用端:服务的实现,接口
- <bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
- <property name="service">
- <bean class="com.cd.Liaq.UserServiceImpl"/>
- </property>
- <property name="serviceInterface">
- <value>com.cd.Liaq.UserService</value>
- </property>
- </bean>
暴露了服务的实现和接口,那么怎么访问服务呢?spring封装访问url- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- 第一种:<property name="urlMap">
- <map>
- <entry key="TestUser" value-ref="userService"/>
- </map>
- </property>
- 第二种:<prop key="/TestUser">userService</prop>
- </bean>
web.xml:配置dispatcherServlet共调用一方使用- <!-- spring远程调用 -->
- <servlet>
- <servlet-name>remote</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>remote</servlet-name>
- <url-pattern>/remoting/*</url-pattern>
- </servlet-mapping>
到处为止:被调用端一方完毕!!!!客户端调用:- <!-- 通过http连接远程系统 -->
- <bean id="memberService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <property name="serviceUrl">
- <value>http://192.9.200.123:8080/MemberSystem/remoting/memberService</value>
- </property>
- <property name="serviceInterface">
- <value>com.cd.Liaq.UserService</value>
- </property>
- </bean>
通过spring容器调用UserService,用到HttpInvokerProxyFactoryBean工厂,配置serviceUrl和serviceInterface为了提高效率:客户端使用Commons-HttpClient,导入改包,改写配置- <bean id="memberService"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <property name="serviceUrl">
- <value>http://192.9.200.123:8080/MemberSystem/remoting/memberService</value>
- </property>
- <property name="serviceInterface">
- <value>com.cd.Liaq.UserService</value>
- </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>
配置超时时间timeout和连接超时connectionTimeout两个属性优化执行器:多线程===被调用端响应时间缩短很多- <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>
httpClient的3.1版本不支持这种配置- <property name="connectionTimeout" value="2000" />
- <property name="timeout" value="5000" />
另外httpClient本身也是多线程的。。HttpClient that uses a default MultiThreadedHttpConnectionManage<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;