原文: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

(转载)springboot集成httpinvoker的客户端的更多相关文章

  1. SpringBoot集成Zipkin实现分布式全链路监控

    目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller ...

  2. springboot集成Kafka

    kafka和MQ的区别: 1)在架构模型方面, RabbitMQ遵循AMQP协议,RabbitMQ的broker由Exchange,Binding,queue组成,其中exchange和binding ...

  3. springboot集成websocket的两种实现方式

    WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...

  4. springboot集成mybatis(逆向工程),热部署以及整合Swagger2

    本文是作者原创,版权归作者所有.若要转载,请注明出处. springboot集成mybatis和mybatis-generator插件 1.新建Springboot项目(略) 2.导入相关依赖 < ...

  5. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  6. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  7. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  8. 基于Springboot集成security、oauth2实现认证鉴权、资源管理

    1.Oauth2简介 OAuth(开放授权)是一个开放标准,允许用户授权第三方移动应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方移动应用或分享他们数据的所有内容,OAu ...

  9. 流式大数据计算实践(5)----HBase使用&SpringBoot集成

    一.前言 1.上文中我们搭建好了一套HBase集群环境,这一文我们学习一下HBase的基本操作和客户端API的使用 二.shell操作 先通过命令进入HBase的命令行操作 /work/soft/hb ...

随机推荐

  1. BeanPostProcessor bean 的后置处理器

    一. 自定 bean 的后置处理器 MyBeanPostProcessor 类.当你在初始化容器中的 bean 之前和之后,都会调用该处理器中的方法 @Component //将该后后置处理器加入到容 ...

  2. 利用Selenium实现图片文件上传的两种方式介绍

    在实现UI自动化测试过程中,有一类需求是实现图片上传,这种需求根据开发的实现方式,UI的实现方式也会不同. 一.直接利用Selenium实现 这种方式是最简单的一种实现方式,但是依赖于开发的实现. 当 ...

  3. ioremap映射函数

    一.ioremap() 函数 Linux在io.h头文件中声明了函数ioremap(),用来将I/O内存资源的物理地址映射到核心虚地址空间(3GB-4GB)中(这里是内核空间),原型如下: 1.ior ...

  4. HDFS学习笔记(1)初探HDFS

    Hadoop分布式文件系统(Hadoop Distributed File System, HDFS) 分布式文件系统是一种同意文件通过网络在多台主机上分享的文件系统.可让多机器上的多用户分享文件和存 ...

  5. hdoj--1754--I Hate It(线段树)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. Pycharm使用入门

    Python安装与Pycharm使用入门 一.安装Python 1.Linux下安装 一般系统默认已安装2.6.6版本,升级成2.7版本, 但 2.6 不能删除,因为系统对它有依赖,epel源里最新的 ...

  7. (转)用Lottie制作动画,我的月薪翻了一番!!

    Lottie是Airbnb发布的开源动画库. 帮助动效落地.学会使用Lottie,会极大地提高工作效率. Lottie是一种新的开发动画的方式. 学会使用Lottie,会极大改善你和开发小哥哥撕逼的情 ...

  8. ORA-16019 和 ORA-16018 错误的处理方法(转)

    一. ORA-16019 和 ORA-16018 错误产生描述 同事在修改归档目录,一不小心把参数设置错误了, 他设置的是log_archive_dest参数. 这个参数和默认log_archive_ ...

  9. (转载)Android自定义标签列表控件LabelsView解析

    Android自定义标签列表控件LabelsView解析 作者 donkingliang 关注 2017.03.15 20:59* 字数 759 阅读 406评论 0喜欢 3 无论是在移动端的App, ...

  10. IntelliJ Idea使用代码折叠

    VS风格: //region    xxxxxxxxxxxxxx ............. //endregion 快捷键: Ctrl+Shift+”+/-”,全部展开.折叠