RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
2014-11-08 15:12
417 查看
spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.spring.remoting下几个类:
发布服务端(Server):RabbitInvokerServiceExporter.java
接口调用客户端(Client):RabbitInvokerProxyFactoryBean.java,RabbitInvokerClientInterceptor.java,
RabbitRpcClient.java(对RpcClient的简单封装,添加了发送消息时的选项:
mandatory--是否强制发送,immediate--是否立即发送,timeoutMs--超时时间)
实例如下创建自动删除非持久队列):
1.测试服务接口TestService.java
3..资源配置application.properties
5.客服端配置applicationContext-rabbitmq-rpc-client.xml
6.启动服务端代码Server.java
运行结果:hello Tom
实例代码:http://download.csdn.net/detail/tianwei7518/8135637
发布服务端(Server):RabbitInvokerServiceExporter.java
接口调用客户端(Client):RabbitInvokerProxyFactoryBean.java,RabbitInvokerClientInterceptor.java,
RabbitRpcClient.java(对RpcClient的简单封装,添加了发送消息时的选项:
mandatory--是否强制发送,immediate--是否立即发送,timeoutMs--超时时间)
实例如下创建自动删除非持久队列):
1.测试服务接口TestService.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc; /** * RPC服务接口 * @author ztw-pc * */ public interface TestService { String say(String msg); }2.测试服务接口实现TestServiceImpl.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc; public class TestServiceImpl implements TestService { public String say(String msg) { return "hello "+msg; } }
3..资源配置application.properties
#============== rabbitmq config ==================== rabbit.hosts=192.168.36.102 rabbit.username=admin rabbit.password=admin rabbit.virtualHost=/ rabbit.exchange=spring-queue-async rabbit.queue=spring-queue-async rabbit.routingKey=spring-queue-async4.服务端配置applicationContext-rabbitmq-rpc-server.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:property-placeholder location="classpath:application.properties"/> <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory"> <property name="connectionFactory"> <bean class="com.rabbitmq.client.ConnectionFactory"> <property name="username" value="${rabbit.username}"/> <property name="password" value="${rabbit.password}"/> <property name="virtualHost" value="${rabbit.virtualHost}"/> </bean> </property> <property name="hosts" value="${rabbit.hosts}"/> </bean> <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory"> <property name="connectionFactory" ref="rabbitConnectionFactory"/> </bean> <bean id="testServiceImpl" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestServiceImpl"/> <bean id="testServiceExport" class="com.rabbitmq.spring.remoting.RabbitInvokerServiceExporter"> <property name="channelFactory" ref="rabbitChannelFactory"/> <property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/> <property name="service" ref="testServiceImpl"/> <property name="exchange" value="${rabbit.exchange}"/> <!-- 必须大写 --> <property name="exchangeType" value="TOPIC"/> <property name="routingKey" value="${rabbit.routingKey}"/> <property name="queueName" value="${rabbit.queue}"/> <property name="poolsize" value="5"/> </bean> </beans>
5.客服端配置applicationContext-rabbitmq-rpc-client.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:property-placeholder location="classpath:application.properties"/> <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory"> <property name="connectionFactory"> <bean class="com.rabbitmq.client.ConnectionFactory"> <property name="username" value="${rabbit.username}"/> <property name="password" value="${rabbit.password}"/> <property name="virtualHost" value="${rabbit.virtualHost}"/> </bean> </property> <property name="hosts" value="${rabbit.hosts}"/> </bean> <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory"> <property name="connectionFactory" ref="rabbitConnectionFactory"/> </bean> <bean id="testService" class="com.rabbitmq.spring.remoting.RabbitInvokerProxyFactoryBean"> <property name="channelFactory" ref="rabbitChannelFactory"/> <property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/> <property name="exchange" value="${rabbit.exchange}"/> <!-- 必须大写 --> <property name="exchangeType" value="TOPIC"/> <property name="routingKey" value="${rabbit.routingKey}"/> <!--optional--> <property name="mandatory" value="true"/> <property name="immediate" value="false"/> <property name="timeoutMs" value="3000"/> <property name="poolSize" value="10"/> </bean> </beans>
6.启动服务端代码Server.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Server { public static void main(String[] args) { new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-server.xml"); } }7.客户端调用代码Client.java
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-client.xml"); TestService testService = (TestService) context.getBean("testService"); System.out.println(testService.say(" Tom")); } }先启动服务端,再运行客户端调用。
运行结果:hello Tom
实例代码:http://download.csdn.net/detail/tianwei7518/8135637
相关文章推荐
- RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
- RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
- RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
- rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用
- 一个简单基于RabbitMQ的RPC(远程调用模型)
- rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用
- 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】
- RabbitMQ实例教程:RPC远程调用消息队列
- facebook的thriff 基于rpc的远程调用实现
- RabbitMQ(七):适用于云计算集群的远程调用(RPC)
- (转)RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)
- RMI远程方法调用示例【基于Spring配置文件】
- Spring中RPC远程调用配置
- 远程调用服务(RPC)和基于消息的通信(Message Queue)对比
- 系统拆分解耦利器之消息队列---RabbitMQ-RPC远程调用
- RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)
- 我是这样学习Linux下C语言编程的-RPC远程调用编程
- RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC)
- Spring in Action 学习笔记—第六章远程调用
- RabbitMQ消息队列(七):适用于云计算集群的远程调用(RPC) [转]