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--超時時間)

實例如下創建自動刪除非持久隊列):

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.         <!--optional-->  
  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

實例代碼:http://download.csdn.net/detail/tianwei7518/8135637


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM