RabbitMQ学习之基于spring-rabbitmq的RPC远程调用

spring-rabbitmq中实现远程接口调用,主要在com.rabbitmq.spring.remoting下几个类:
发布服务端(Server):RabbitInvokerServiceExporter.java
接口调用客户端(Client):RabbitInvokerProxyFactoryBean.java,RabbitInvokerClientInterceptor.java,
RabbitRpcClient.java(对RpcClient的简单封装,添加了发送消息时的选项:

mandatory--是否强制发送,immediate--是否立即发送,timeoutMs--超时时间)

实例如下创建自动删除非持久队列)

1.测试服务接口TestService.Java

[java]  view plain  copy
 print ?
  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;  
  2.   
  3. /** 
  4.  * RPC服务接口 
  5.  * @author ztw-pc 
  6.  * 
  7.  */  
  8. public interface TestService {  
  9.     String say(String msg);  
  10. }  
2.测试服务接口实现TestServiceImpl.java

[java]  view plain  copy
 print ?
  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;  
  2.   
  3. public class TestServiceImpl implements TestService {  
  4.   
  5.     public String say(String msg) {  
  6.         return "hello "+msg;  
  7.     }  
  8. }  

3..资源配置application.properties

[plain]  view plain  copy
 print ?
  1. #============== rabbitmq config ====================  
  2. rabbit.hosts=192.168.36.102  
  3. rabbit.username=admin  
  4. rabbit.password=admin  
  5. rabbit.virtualHost=/  
  6. rabbit.exchange=spring-queue-async  
  7. rabbit.queue=spring-queue-async  
  8. rabbit.routingKey=spring-queue-async  
4.服务端配置applicationContext-rabbitmq-rpc-server.xml

[html]  view plain  copy
 print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.        xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  8.   
  9.      <context:property-placeholder location="classpath:application.properties"/>  
  10.        
  11.      <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">  
  12.         <property name="connectionFactory">  
  13.             <bean class="com.rabbitmq.client.ConnectionFactory">  
  14.                <property name="username" value="${rabbit.username}"/>  
  15.                <property name="password" value="${rabbit.password}"/>  
  16.                <property name="virtualHost" value="${rabbit.virtualHost}"/>  
  17.             bean>  
  18.         property>  
  19.         <property name="hosts" value="${rabbit.hosts}"/>  
  20.     bean>  
  21.      <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">  
  22.         <property name="connectionFactory" ref="rabbitConnectionFactory"/>  
  23.     bean>  
  24.       
  25.     <bean id="testServiceImpl" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestServiceImpl"/>  
  26.       
  27.     <bean id="testServiceExport" class="com.rabbitmq.spring.remoting.RabbitInvokerServiceExporter">  
  28.         <property name="channelFactory" ref="rabbitChannelFactory"/>  
  29.         <property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>  
  30.         <property name="service" ref="testServiceImpl"/>  
  31.          <property name="exchange" value="${rabbit.exchange}"/>  
  32.            
  33.         <property name="exchangeType" value="TOPIC"/>  
  34.         <property name="routingKey" value="${rabbit.routingKey}"/>  
  35.         <property name="queueName" value="${rabbit.queue}"/>  
  36.         <property name="poolsize" value="5"/>  
  37.     bean>  
  38.       
  39. beans>  

5.客服端配置applicationContext-rabbitmq-rpc-client.xml

[html]  view plain  copy
 print ?
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.        xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  8.   
  9.      <context:property-placeholder location="classpath:application.properties"/>  
  10.        
  11.      <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">  
  12.         <property name="connectionFactory">  
  13.             <bean class="com.rabbitmq.client.ConnectionFactory">  
  14.                <property name="username" value="${rabbit.username}"/>  
  15.                <property name="password" value="${rabbit.password}"/>  
  16.                <property name="virtualHost" value="${rabbit.virtualHost}"/>  
  17.             bean>  
  18.         property>  
  19.         <property name="hosts" value="${rabbit.hosts}"/>  
  20.     bean>  
  21.      <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">  
  22.         <property name="connectionFactory" ref="rabbitConnectionFactory"/>  
  23.     bean>  
  24.       
  25.     <bean id="testService" class="com.rabbitmq.spring.remoting.RabbitInvokerProxyFactoryBean">  
  26.         <property name="channelFactory" ref="rabbitChannelFactory"/>  
  27.         <property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>  
  28.         <property name="exchange" value="${rabbit.exchange}"/>  
  29.            
  30.         <property name="exchangeType" value="TOPIC"/>  
  31.         <property name="routingKey" value="${rabbit.routingKey}"/>  
  32.           
  33.         <property name="mandatory" value="true"/>  
  34.         <property name="immediate" value="false"/>  
  35.         <property name="timeoutMs" value="3000"/>  
  36.         <property name="poolSize" value="10"/>  
  37.     bean>  
  38.       
  39. beans>  

6.启动服务端代码Server.java

[java]  view plain  copy
 print ?
  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class Server {  
  6.   
  7.     public static void main(String[] args) {  
  8.         new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-server.xml");  
  9.     }  
  10. }  
7.客户端调用代码Client.java

[java]  view plain  copy
 print ?
  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Client {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-client.xml");    
  10.         TestService testService = (TestService) context.getBean("testService");  
  11.         System.out.println(testService.say(" Tom"));  
  12.     }  
  13. }  
先启动服务端,再运行客户端调用。

运行结果:hello  Tom

你可能感兴趣的:(rabbitmq)