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 ...
随机推荐
- 纯CSS 3D翻转一个面(翻转导航菜单 立方体)
在做练习的时候学到css的翻转导航菜单,原代码有点让人头疼,通过对其css的参数一点点研究了其实现过程. 这里推荐大家研究这个3D翻转动画的代码. 我的github:swarz,欢迎给老弟我++星星 ...
- Codeforces 912A/B
A. Tricky Alchemy 传送门:http://codeforces.com/contest/912/problem/A 参考程序如下: #include <stdio.h> # ...
- 10.使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
出处:http://www.cnblogs.com/lichenwei/p/4145696.html Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由 ...
- QT中tableview不能更新数据,why?
model->select(); //model->removeColumn(0);++++++++++++++++++++ //model->setHeaderData(model ...
- win10使用WampServer部署magento
1.安装wampserver及php.apache.mySQL组件,访问http://www.wampserver.com/en/#download-wrapper,下载以下文件并依次安装: ...
- java editor template Eclipse中的快速Java\JavaScript代码模板使用
java editor template Eclipse中的快速Java\JavaScript代码模板使用 学习了:http://technicalsearch.iteye.com/blog/2150 ...
- [Angular] Advanced DI
In this post, we are going to see how to solve one design pattern challenge. The challenge is what w ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- Android布局文件经验
1.父控件中含有多个子控件时.往往遵循长子优先的原则,即长子假设非常大可能占满父空间.使次子们出局: 2.如果TableLayout有2行,当中一行未设定列间长度比例.而还有一行设定了,则未设定行可能 ...
- 《游戏脚本的设计与开发》-(RPG部分)3.8 通过脚本来自由控制游戏(一)
注意:本系列教程为长篇连载无底洞.半路杀进来的朋友,假设看不懂的话.请从第一章開始看起.文章文件夹请点击以下链接. http://blog.csdn.net/lufy_legend/article/d ...