RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
http://blog.csdn.net/zhu_tianwei/article/details/40920985
spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.spring.remoting下几个类:
发布服务端(Server):RabbitInvokerServiceExporter.java
接口调用客户端(Client):RabbitInvokerProxyFactoryBean.java,RabbitInvokerClientInterceptor.java,
RabbitRpcClient.java(对RpcClient的简单封装,添加了发送消息时的选项:
mandatory--是否强制发送,immediate--是否立即发送,timeoutMs--超时时间)
实例如下创建自动删除非持久队列):
- 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-async
4.服务端配置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远程调用的更多相关文章
- 基于http协议实现RPC远程调用
今天简单说一下基本Http协议来实现RPC框架~ 基于Http协议实现RPC框架: 优点: 1.简单.实用.开发方便 缺点: 1.性能不是很稳定,在海量数据时,完全顶不住,容易宕机 2.因为不是走的注 ...
- 【RabbitMQ学习之二】RabbitMQ四种交换机模式应用
环境 win7 rabbitmq-server-3.7.17 Erlang 22.1 一.概念1.队列队列用于临时存储消息和转发消息.队列类型有两种,即时队列和延时队列. 即时队列:队列中的消息会被立 ...
- RabbitMQ学习笔记五:RabbitMQ之优先级消息队列
RabbitMQ优先级队列注意点: 1.只有当消费者不足,不能及时进行消费的情况下,优先级队列才会生效 2.RabbitMQ3.5以后才支持优先级队列 代码在博客:RabbitMQ学习笔记三:Java ...
- 从0到1:全面理解RPC远程调用
上一篇关于 WSGI 的硬核长文,不知道有多少同学,能够从头看到尾的,不管你们有没有看得很过瘾,反正我是写得很爽,总有一种将一样知识吃透了的错觉. 今天我又给自己挖坑了,打算将 rpc 远程调用的知识 ...
- 測试JSON RPC远程调用(JSONclient)
#include <string> #include <iostream> #include <curl/curl.h> /* 标题:JSonclient Auth ...
- 使用Socket&反射&Java流操作进行方法的远程调用(模拟RPC远程调用)
写在前面 阅读本文首先得具备基本的Socket.反射.Java流操作的基本API使用知识:否则本文你可能看不懂... 服务端的端口监听 进行远程调用,那就必须得有客户端和服务端.服务端负责提供服务,客 ...
- Openstack Nova 源码分析 — RPC 远程调用过程
目录 目录 Nova Project Services Project 的程序入口 setuppy Nova中RPC远程过程调用 nova-compute RPC API的实现 novacompute ...
- RabbitMQ学习之基于spring-rabbitmq的消息异步发送
spring-rabbitmq的源码到http://github.com/momania/spring-rabbitmq下载,并可以下载实例代码.由于我使用的rabbitmq版本是3.0.4,部分代码 ...
- RabbitMQ学习系列一安装RabbitMQ服务
RabbitMQ学习系列一:windows下安装RabbitMQ服务 http://www.80iter.com/blog/1437026462550244 Rabbit MQ 是建立在强大的Erla ...
随机推荐
- html表单练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 如何让 Windows 上的命令行工具更好用
侯爵老师视频详解:如何让 Windows 上的命令行工具更好用 很多 Windows 用户在打开这个小黑窗时,都会情不自禁的感慨,「实在是太丑了--」 实际上如果你用的是 Windows 8 或 Wi ...
- Django(七)
一.ModelForm操作及验证 1.class Meta:class Meta: #注意以下字段不能加逗号 model = models.UserInfo #这里的all代指所用的字段,也可以是一个 ...
- P2310 loidc,看看海
P2310 loidc,看看海 题目背景 loidc喜欢大海.在他放假的时候他经常一个人跑到海边独自玩耍. 在浪花的冲击下,他可以忘记打代码的烦躁,真是惬意极了. 虽然今天是周六,但今天可是11.8号 ...
- 【日常学习】【搜索/递归】codevs2802 二的幂次方题解
转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 题目描写叙述 Description 不论什么一个正整数都能够用2的幂次方表示. 比如:13 ...
- python TypeError: 'builtin_function_or_method' object is not iterable keys
statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statin ...
- 使用 AFNetworking的时候,怎样管理 session ID
问: As the title implies, I am using AFNetworking in an iOS project in which the application talks to ...
- u-boot学习(五):u-boot启动内核
u-boot的目的是启动内核.内核位于Flash中,那么u-boot就要将内核转移到内存中.然后执行命令执行之.这些操作是由bootcmd命令完毕的. bootcmd=nand read.jffs2 ...
- 在Windows 8.1系统上配置免安装版mysql-5.6.21-winx64
1.到官网上下载MySQL 下载地址为:http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-winx64.zip 2.解压文件到D盘 当然你可以 ...
- Java web測试分为6个部分
1.功能測试 2.性能測试(包含负载/压力測试)3.用户界面測试 4. 兼容性測试 5. 安全測试 6.接口測试 1 功能測试 1.1 链接測试 链接測试可分为三个方面. 首先,測试全部链接是 ...