rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用
2016-04-22 14:31
691 查看
此远程接口调用是基于RPC的
先来看看提供暴露接口方法的配置
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<bean id="amqpAdmin"
class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<bean id="rabbitTemplate"
class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory"></constructor-arg>
</bean>
<bean id="testService" class="com.abin.test.TestServiceImpl"></bean>
<bean
class="org.springframework.amqp.rabbit.remoting.RabbitInvokerServiceExporter">
<property name="connectionFactory" ref="connectionFactory" />
<property name="serviceInterface"
value="com.abin.test.TestService" />
<property name="service" ref="testService" />
<property name="exchange" value="service_exhange" />
<property name="exchangeTypes" value="topic" />
<property name="routingKey" value="routing.example.service" />
<property name="queueName" value="incoming_queue_name" />
<property name="poolsize" value="5" />
</bean>
</beans>
RabbitInvokerServiceExporter类用于把接口services放到一个类型为“direct”的queue或者exchange中,并处理远程接口调用的回调。
远程调用配置如下:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 创建connectionFactory -->
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<!-- 创建rabbitAdmin 代理类 -->
<bean id="rabbitAdmin"
class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<!-- 创建rabbitTemplate 消息模板类 -->
<bean id="rabbitTemplate"
class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory"></constructor-arg>
</bean>
<bean id="testService"
class="org.springframework.amqp.rabbit.remoting.RabbitInvokerProxyFactoryBean">
<property name="connectionFactory" ref="connectionFactory" />
<property name="serviceInterface"
value="com.abin.test.TestService">
</property>
<property name="exchange" value="service_exhange" />
<property name="exchangeTypes" value="topic" />
<property name="routingKey" value="routing.example.service" />
</bean>
<bean id="testAction" class="com.abin.action.TestAction">
<property name="testService" ref="testService" />
</bean>
</beans>
RabbitInvokerProxyFactoryBean类通过拦截器方法调用在rabbitmq中已提供的远程接口信息。
先来看看提供暴露接口方法的配置
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<bean id="amqpAdmin"
class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<bean id="rabbitTemplate"
class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory"></constructor-arg>
</bean>
<bean id="testService" class="com.abin.test.TestServiceImpl"></bean>
<bean
class="org.springframework.amqp.rabbit.remoting.RabbitInvokerServiceExporter">
<property name="connectionFactory" ref="connectionFactory" />
<property name="serviceInterface"
value="com.abin.test.TestService" />
<property name="service" ref="testService" />
<property name="exchange" value="service_exhange" />
<property name="exchangeTypes" value="topic" />
<property name="routingKey" value="routing.example.service" />
<property name="queueName" value="incoming_queue_name" />
<property name="poolsize" value="5" />
</bean>
</beans>
RabbitInvokerServiceExporter类用于把接口services放到一个类型为“direct”的queue或者exchange中,并处理远程接口调用的回调。
远程调用配置如下:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 创建connectionFactory -->
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<!-- 创建rabbitAdmin 代理类 -->
<bean id="rabbitAdmin"
class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<!-- 创建rabbitTemplate 消息模板类 -->
<bean id="rabbitTemplate"
class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory"></constructor-arg>
</bean>
<bean id="testService"
class="org.springframework.amqp.rabbit.remoting.RabbitInvokerProxyFactoryBean">
<property name="connectionFactory" ref="connectionFactory" />
<property name="serviceInterface"
value="com.abin.test.TestService">
</property>
<property name="exchange" value="service_exhange" />
<property name="exchangeTypes" value="topic" />
<property name="routingKey" value="routing.example.service" />
</bean>
<bean id="testAction" class="com.abin.action.TestAction">
<property name="testService" ref="testService" />
</bean>
</beans>
RabbitInvokerProxyFactoryBean类通过拦截器方法调用在rabbitmq中已提供的远程接口信息。
相关文章推荐
- Rabbitmq集群搭建笔记
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 一起动手编写Android图片加载框架
- 基于.NET平台常用的框架和开源程序整理
- 列举PHP的Yii 2框架的开发优势
- Windows窗体的.Net框架绘图技术实现方法
- 浅谈JavaScript 框架分类
- 轻量级javascript 框架Backbone使用指南
- javascript实现框架高度随内容改变的方法
- JS刷新框架外页面七种实现代码
- 超赞的动手创建JavaScript框架的详细教程
- 深入探讨前端框架react
- 简单介绍不用库(框架)自己写ajax
- 利用ASP.NET MVC+EasyUI+SqlServer搭建企业开发框架
- asp.net4.0框架下验证机制失效的原因及处理办法
- 插件管理框架 for Delphi(二)
- 零基础学习AJAX之AJAX框架
- Ajax 框架学习笔记
- Flex中最好的MVC框架Mate框架