RabbitMQ学习之基于spring-rabbitmq的RPC远程调用
2016-04-21 16:38
711 查看
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?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
/**
* RPC服务接口
* @author ztw-pc
*
*/
public interface TestService {
String say(String msg);
}
2.测试服务接口实现TestServiceImpl.java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
public class TestServiceImpl implements TestService {
public String say(String msg) {
return "hello "+msg;
}
}
3..资源配置application.properties
[plain] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
#============== rabbitmq config ====================
rabbit.hosts=192.168.36.102
rabbit.username=admin
rabbit.password=admin
rabbit.virtualHost=/
rabbit.exchange=spring-queue-async
rabbit.queue=spring-queue-async
rabbit.routingKey=spring-queue-async
4.服务端配置applicationContext-rabbitmq-rpc-server.xml
[html] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
<property name="connectionFactory">
<bean class="com.rabbitmq.client.ConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="virtualHost" value="${rabbit.virtualHost}"/>
</bean>
</property>
<property name="hosts" value="${rabbit.hosts}"/>
</bean>
<bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="testServiceImpl" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestServiceImpl"/>
<bean id="testServiceExport" class="com.rabbitmq.spring.remoting.RabbitInvokerServiceExporter">
<property name="channelFactory" ref="rabbitChannelFactory"/>
<property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>
<property name="service" ref="testServiceImpl"/>
<property name="exchange" value="${rabbit.exchange}"/>
<!-- 必须大写 -->
<property name="exchangeType" value="TOPIC"/>
<property name="routingKey" value="${rabbit.routingKey}"/>
<property name="queueName" value="${rabbit.queue}"/>
<property name="poolsize" value="5"/>
</bean>
</beans>
5.客服端配置applicationContext-rabbitmq-rpc-client.xml
[html] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
<property name="connectionFactory">
<bean class="com.rabbitmq.client.ConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="virtualHost" value="${rabbit.virtualHost}"/>
</bean>
</property>
<property name="hosts" value="${rabbit.hosts}"/>
</bean>
<bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="testService" class="com.rabbitmq.spring.remoting.RabbitInvokerProxyFactoryBean">
<property name="channelFactory" ref="rabbitChannelFactory"/>
<property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>
<property name="exchange" value="${rabbit.exchange}"/>
<!-- 必须大写 -->
<property name="exchangeType" value="TOPIC"/>
<property name="routingKey" value="${rabbit.routingKey}"/>
<!--optional-->
<property name="mandatory" value="true"/>
<property name="immediate" value="false"/>
<property name="timeoutMs" value="3000"/>
<property name="poolSize" value="10"/>
</bean>
</beans>
6.启动服务端代码Server.java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Server {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-server.xml");
}
}
7.客户端调用代码Client.java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-client.xml");
TestService testService = (TestService) context.getBean("testService");
System.out.println(testService.say(" Tom"));
}
}
先启动服务端,再运行客户端调用。
运行结果:hello Tom
发布服务端(Server):RabbitInvokerServiceExporter.java
接口调用客户端(Client):RabbitInvokerProxyFactoryBean.java,RabbitInvokerClientInterceptor.java,
RabbitRpcClient.java(对RpcClient的简单封装,添加了发送消息时的选项:
mandatory--是否强制发送,immediate--是否立即发送,timeoutMs--超时时间)
实例如下创建自动删除非持久队列):
1.测试服务接口TestService.Java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
/**
* RPC服务接口
* @author ztw-pc
*
*/
public interface TestService {
String say(String msg);
}
2.测试服务接口实现TestServiceImpl.java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
public class TestServiceImpl implements TestService {
public String say(String msg) {
return "hello "+msg;
}
}
3..资源配置application.properties
[plain] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
#============== rabbitmq config ====================
rabbit.hosts=192.168.36.102
rabbit.username=admin
rabbit.password=admin
rabbit.virtualHost=/
rabbit.exchange=spring-queue-async
rabbit.queue=spring-queue-async
rabbit.routingKey=spring-queue-async
4.服务端配置applicationContext-rabbitmq-rpc-server.xml
[html] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
<property name="connectionFactory">
<bean class="com.rabbitmq.client.ConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="virtualHost" value="${rabbit.virtualHost}"/>
</bean>
</property>
<property name="hosts" value="${rabbit.hosts}"/>
</bean>
<bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="testServiceImpl" class="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestServiceImpl"/>
<bean id="testServiceExport" class="com.rabbitmq.spring.remoting.RabbitInvokerServiceExporter">
<property name="channelFactory" ref="rabbitChannelFactory"/>
<property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>
<property name="service" ref="testServiceImpl"/>
<property name="exchange" value="${rabbit.exchange}"/>
<!-- 必须大写 -->
<property name="exchangeType" value="TOPIC"/>
<property name="routingKey" value="${rabbit.routingKey}"/>
<property name="queueName" value="${rabbit.queue}"/>
<property name="poolsize" value="5"/>
</bean>
</beans>
5.客服端配置applicationContext-rabbitmq-rpc-client.xml
[html] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="rabbitConnectionFactory" class="com.rabbitmq.spring.connection.RabbitConnectionFactory">
<property name="connectionFactory">
<bean class="com.rabbitmq.client.ConnectionFactory">
<property name="username" value="${rabbit.username}"/>
<property name="password" value="${rabbit.password}"/>
<property name="virtualHost" value="${rabbit.virtualHost}"/>
</bean>
</property>
<property name="hosts" value="${rabbit.hosts}"/>
</bean>
<bean id="rabbitChannelFactory" class="com.rabbitmq.spring.channel.RabbitChannelFactory">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
</bean>
<bean id="testService" class="com.rabbitmq.spring.remoting.RabbitInvokerProxyFactoryBean">
<property name="channelFactory" ref="rabbitChannelFactory"/>
<property name="serviceInterface" value="cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc.TestService"/>
<property name="exchange" value="${rabbit.exchange}"/>
<!-- 必须大写 -->
<property name="exchangeType" value="TOPIC"/>
<property name="routingKey" value="${rabbit.routingKey}"/>
<!--optional-->
<property name="mandatory" value="true"/>
<property name="immediate" value="false"/>
<property name="timeoutMs" value="3000"/>
<property name="poolSize" value="10"/>
</bean>
</beans>
6.启动服务端代码Server.java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Server {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-server.xml");
}
}
7.客户端调用代码Client.java
[java] view
plain copy
print?
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
package cn.slimsmart.rabbitmq.spring.rabbitmq.demo.rpc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-rabbitmq-rpc-client.xml");
TestService testService = (TestService) context.getBean("testService");
System.out.println(testService.say(" Tom"));
}
}
先启动服务端,再运行客户端调用。
运行结果:hello Tom
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Gson.toJson()时内存溢出StackOverflowError
- Rabbitmq集群搭建笔记
- test
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 网络路由技术及运用2
- 路由器的关键技术
- IP网络路由技术
- 远程控制技术的应用
- 智能防火墙的技术特征
- 什么是多层交换技术
- 浅析多层交换技术
- 路由器技术的变革
- 网络路由技术及运用3
- CSS中的滑动门技术