rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用

 此远程接口调用是基于RPC的

 

   先来看看提供暴露接口方法的配置

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  5.     <bean id="connectionFactory"  
  6.         class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">  
  7.         <constructor-arg value="localhost" />  
  8.         <property name="username" value="guest" />  
  9.         <property name="password" value="guest" />  
  10.     </bean>  
  11.     <bean id="amqpAdmin"  
  12.         class="org.springframework.amqp.rabbit.core.RabbitAdmin">  
  13.         <constructor-arg ref="connectionFactory" />  
  14.     </bean>  
  15.     <bean id="rabbitTemplate"  
  16.         class="org.springframework.amqp.rabbit.core.RabbitTemplate">  
  17.         <constructor-arg ref="connectionFactory"></constructor-arg>  
  18.     </bean>  
  19.     <bean id="testService" class="com.abin.test.TestServiceImpl"></bean>  
  20.   
  21.     <bean  
  22.         class="org.springframework.amqp.rabbit.remoting.RabbitInvokerServiceExporter">  
  23.         <property name="connectionFactory" ref="connectionFactory" />  
  24.         <property name="serviceInterface"  
  25.             value="com.abin.test.TestService" />  
  26.         <property name="service" ref="testService" />  
  27.         <property name="exchange" value="service_exhange" />  
  28.         <property name="exchangeTypes" value="topic" />  
  29.         <property name="routingKey" value="routing.example.service" />  
  30.         <property name="queueName" value="incoming_queue_name" />  
  31.         <property name="poolsize" value="5" />  
  32.     </bean>  
  33. </beans>  

 

    RabbitInvokerServiceExporter类用于把接口services放到一个类型为“direct”的queue或者exchange中,并处理远程接口调用的回调。

 

    远程调用配置如下:

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  5.     <!-- 创建connectionFactory -->  
  6.     <bean id="connectionFactory"  
  7.         class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">  
  8.         <constructor-arg value="localhost" />  
  9.         <property name="username" value="guest" />  
  10.         <property name="password" value="guest" />  
  11.     </bean>  
  12.     <!-- 创建rabbitAdmin 代理类 -->  
  13.     <bean id="rabbitAdmin"  
  14.         class="org.springframework.amqp.rabbit.core.RabbitAdmin">  
  15.         <constructor-arg ref="connectionFactory" />  
  16.     </bean>  
  17.     <!-- 创建rabbitTemplate 消息模板类 -->  
  18.     <bean id="rabbitTemplate"  
  19.         class="org.springframework.amqp.rabbit.core.RabbitTemplate">  
  20.         <constructor-arg ref="connectionFactory"></constructor-arg>  
  21.     </bean>  
  22.   
  23.     <bean id="testService"  
  24.         class="org.springframework.amqp.rabbit.remoting.RabbitInvokerProxyFactoryBean">  
  25.         <property name="connectionFactory" ref="connectionFactory" />  
  26.         <property name="serviceInterface"  
  27.             value="com.abin.test.TestService">  
  28.         </property>  
  29.         <property name="exchange" value="service_exhange" />  
  30.         <property name="exchangeTypes" value="topic" />  
  31.         <property name="routingKey" value="routing.example.service" />  
  32.     </bean>  
  33.   
  34.     <bean id="testAction" class="com.abin.action.TestAction">  
  35.         <property name="testService" ref="testService" />  
  36.     </bean>  
  37. </beans>  

 

 RabbitInvokerProxyFactoryBean类通过拦截器方法调用在rabbitmq中已提供的远程接口信息。

你可能感兴趣的:(框架,rabbitmq)