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

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

 

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

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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-3.0.xsd">
	<bean id="connectionFactory"
		class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
		<constructor-arg value="localhost" />
		<property name="username" value="guest" />
		<property name="password" value="guest" />
	</bean>
	<bean id="amqpAdmin"
		class="org.springframework.amqp.rabbit.core.RabbitAdmin">
		<constructor-arg ref="connectionFactory" />
	</bean>
	<bean id="rabbitTemplate"
		class="org.springframework.amqp.rabbit.core.RabbitTemplate">
		<constructor-arg ref="connectionFactory"></constructor-arg>
	</bean>
	<bean id="testService" class="com.abin.test.TestServiceImpl"></bean>

	<bean
		class="org.springframework.amqp.rabbit.remoting.RabbitInvokerServiceExporter">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="serviceInterface"
			value="com.abin.test.TestService" />
		<property name="service" ref="testService" />
		<property name="exchange" value="service_exhange" />
		<property name="exchangeTypes" value="topic" />
		<property name="routingKey" value="routing.example.service" />
		<property name="queueName" value="incoming_queue_name" />
		<property name="poolsize" value="5" />
	</bean>
</beans>

 

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

 

    远程调用配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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-3.0.xsd">
	<!-- 创建connectionFactory -->
	<bean id="connectionFactory"
		class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
		<constructor-arg value="localhost" />
		<property name="username" value="guest" />
		<property name="password" value="guest" />
	</bean>
	<!-- 创建rabbitAdmin 代理类 -->
	<bean id="rabbitAdmin"
		class="org.springframework.amqp.rabbit.core.RabbitAdmin">
		<constructor-arg ref="connectionFactory" />
	</bean>
	<!-- 创建rabbitTemplate 消息模板类 -->
	<bean id="rabbitTemplate"
		class="org.springframework.amqp.rabbit.core.RabbitTemplate">
		<constructor-arg ref="connectionFactory"></constructor-arg>
	</bean>

	<bean id="testService"
		class="org.springframework.amqp.rabbit.remoting.RabbitInvokerProxyFactoryBean">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="serviceInterface"
			value="com.abin.test.TestService">
		</property>
		<property name="exchange" value="service_exhange" />
		<property name="exchangeTypes" value="topic" />
		<property name="routingKey" value="routing.example.service" />
	</bean>

	<bean id="testAction" class="com.abin.action.TestAction">
		<property name="testService" ref="testService" />
	</bean>
</beans>

 

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

上述用到的程序在附件中。还可以参考https://github.com/momania/spring-rabbitmq

你可能感兴趣的:(rabbitmq学习11:基于rabbitmq和spring-amqp的远程接口调用)