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