原文:https://blog.csdn.net/geanwan/article/details/51505679
由于新项目采用了springboot,需要调用之前远程服务(之前项目用的spring的httpinvoker),现说明下在springboot下配置httpinvoker的客户端使用,其中会穿插基于spring3的httpinvoker的配置,以做对比,关于httpinvoker的服务端的配置后续再更新。
首先是基于spring3的httpinvoker的客户端一般配置
ctx-remote.xml
<bean id="basic-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:base-service.properties</value> </list> </property> </bean> <bean id="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="${server.uri}/remoting/invoker/userService" /> <property name="serviceInterface" value="com.aa.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="1000" /> <property name="timeout" value="2000" /> <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>
默认情况下,客户端的HttpInvokerProxy使用J2SE的HTTP Client来建立连接,即org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor,可以通过设置httpInvokerRequestExecutor属性来改变默认配置,spring提供了另外一种HttpClient,org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor。在HttpClient中使用多线程的一个主要原因是可以一次执行多个方法。在执行期间,每一个方法都使用一个HttpConnection实例。由于在同一时间多个连接只能安全地用于单一线程和方法和有限的资源,我们就必须确保连接分配给正确的方法。而MultiThreadedHttpConnectionManager完全可以代替我们完成这一项工作,这样我们就不必去考虑多线程带来安全的问题。
访问方式
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:ctx-remote.xml" ); service = (UserService) applicationContext.getBean( "userService" );
相关详情可以参考点击打开链接
springboot实现httpinvoker的客户端
由于springboot是spring4的下一个新项目,无配置化,所以用class文件去描述相关的bean。
HttpInvokerRequestInit.Java
import org.apache.http.client.HttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor; import org.springframework.stereotype.Component; @Component public class HttpInvokerRequestInit { //@Autowired //private MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager; @SuppressWarnings("deprecation") @Bean public HttpComponentsHttpInvokerRequestExecutor httpInvokerRequestExecutor(){ HttpComponentsHttpInvokerRequestExecutor bean = new HttpComponentsHttpInvokerRequestExecutor(); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(); cm.setMaxTotal(100); HttpClient httpclient = new DefaultHttpClient(cm); //httpClient.setConnectionTimeout(1000); //httpClient.setTimeout(2000); bean.setHttpClient(httpclient); return bean; } }
由于之前的spring3中的CommonsHttpInvokerRequestExecutor在spring4已经不支持了,所以替换成了org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor,同时HttpComponentsHttpInvokerRequestExecutor不支持org.apache.commons.httpclient.HttpClient,所以又新增
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
这里用ThreadSafeClientConnManager来替换MultiThreadedHttpConnectionManager,具体可以查看Apache的API。
RemotingServiceInit.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor; import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean; import org.springframework.stereotype.Component; import com.aa.service.UserService; @Component public class RemotingServiceInit { @Value("${spring.remoteurl}") private String remoteurl; @Autowired private HttpComponentsHttpInvokerRequestExecutor httpInvokerRequestExecutor; @Bean public HttpInvokerProxyFactoryBean userService(){ HttpInvokerProxyFactoryBean bean = new HttpInvokerProxyFactoryBean(); bean.setServiceUrl(remoteurl+"/remoting/invoker/userService"); bean.setServiceInterface(UserService.class); bean.setHttpInvokerRequestExecutor(httpInvokerRequestExecutor); return bean; } }
其中serviceurl为服务端对应的service的声明的url。还有因为httpinvoker的机制,我在当前工程导入了service和传输对象的集合jar包。
调用方式
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.aa.bean.User; import com.aa.service.UserService; @Controller public class LoginController { @Autowired private UserService userService; @RequestMapping("/login") public String login(){ User r1 = (User)userService.FetchDataFromServer(); System.out.println(r1.getName()); return "index"; } }
这里通过spring自动依赖注入的方式获取userService的实例。
更多参考链接
1.http://liuinsect.iteye.com/blog/1886237
2.http://www.apihome.cn/api/spring/HttpInvokerRequestExecutor.html
3.http://www.blogjava.net/wangxinsh55/archive/2012/07/16/383209.html