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

时间:2021-01-01 08:49:01

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

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
  2. /**
  3. * RPC服务接口
  4. * @author ztw-pc
  5. *
  6. */
  7. public interface TestService {
  8. String say(String msg);
  9. }

2.测试服务接口实现TestServiceImpl.java

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
  2. public class TestServiceImpl implements TestService {
  3. public String say(String msg) {
  4. return "hello "+msg;
  5. }
  6. }

3..资源配置application.properties

  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

  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. <context:property-placeholder location="classpath:application.properties"/>
  9. <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
  10. <property name="connectionFactory">
  11. <bean class="com.rabbitmq.client.ConnectionFactory">
  12. <property name="username" value="${rabbit.username}"/>
  13. <property name="password" value="${rabbit.password}"/>
  14. <property name="virtualHost" value="${rabbit.virtualHost}"/>
  15. </bean>
  16. </property>
  17. <property name="hosts" value="${rabbit.hosts}"/>
  18. </bean>
  19. <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
  20. <property name="connectionFactory" ref="rabbitConnectionFactory"/>
  21. </bean>
  22. <bean id="testServiceImpl" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestServiceImpl"/>
  23. <bean id="testServiceExport" class="com.rabbitmq.spring.remoting.RabbitInvokerServiceExporter">
  24. <property name="channelFactory" ref="rabbitChannelFactory"/>
  25. <property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>
  26. <property name="service" ref="testServiceImpl"/>
  27. <property name="exchange" value="${rabbit.exchange}"/>
  28. <!-- 必须大写  -->
  29. <property name="exchangeType" value="TOPIC"/>
  30. <property name="routingKey" value="${rabbit.routingKey}"/>
  31. <property name="queueName" value="${rabbit.queue}"/>
  32. <property name="poolsize" value="5"/>
  33. </bean>
  34. </beans>

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

  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. <context:property-placeholder location="classpath:application.properties"/>
  9. <bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
  10. <property name="connectionFactory">
  11. <bean class="com.rabbitmq.client.ConnectionFactory">
  12. <property name="username" value="${rabbit.username}"/>
  13. <property name="password" value="${rabbit.password}"/>
  14. <property name="virtualHost" value="${rabbit.virtualHost}"/>
  15. </bean>
  16. </property>
  17. <property name="hosts" value="${rabbit.hosts}"/>
  18. </bean>
  19. <bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
  20. <property name="connectionFactory" ref="rabbitConnectionFactory"/>
  21. </bean>
  22. <bean id="testService" class="com.rabbitmq.spring.remoting.RabbitInvokerProxyFactoryBean">
  23. <property name="channelFactory" ref="rabbitChannelFactory"/>
  24. <property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>
  25. <property name="exchange" value="${rabbit.exchange}"/>
  26. <!-- 必须大写  -->
  27. <property name="exchangeType" value="TOPIC"/>
  28. <property name="routingKey" value="${rabbit.routingKey}"/>
  29. <!--optional-->
  30. <property name="mandatory" value="true"/>
  31. <property name="immediate" value="false"/>
  32. <property name="timeoutMs" value="3000"/>
  33. <property name="poolSize" value="10"/>
  34. </bean>
  35. </beans>

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

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class Server {
  4. public static void main(String[] args) {
  5. new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-server.xml");
  6. }
  7. }

7.客户端调用代码Client.java

  1. package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Client {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-client.xml");
  7. TestService testService = (TestService) context.getBean("testService");
  8. System.out.println(testService.say(" Tom"));
  9. }
  10. }

先启动服务端,再运行客户端调用。

运行结果:hello  Tom

实例代码:http://download.csdn.net/detail/tianwei7518/8135637