- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章springboot集成rabbitMQ之对象传输的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
rabbitmq的安装方法网上有很多教程,这里就不重复了.
在springboot上使用rabbitmq传输字符串和对象,本文所给出的例子是在两个不同的项目之间进行对象和和字符串的传输.
rabbitmq的依赖(在两个项目中一样的配置):
1
2
3
4
|
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-amqp</artifactid>
</dependency>
|
pom配置文件(在两个项目中一样的配置):
1
2
3
4
5
6
7
8
9
|
spring.application.name: demo1
//项目名
spring.rabbitmq.host:
192.168
.
1.111
//写自己的ip
spring.rabbitmq.port:
5672
spring.rabbitmq.username: guest
spring.rabbitmq.password: guest
spring.rabbitmq.virtual-host: /
spring.rabbitmq.publisher-confirms:
true
spring.rabbitmq.publisher-returns:
true
spring.rabbitmq.template.mandatory:
true
|
字符转的相互传输(本例使用的topic类型) 。
1>. 首先,在生产者(项目a)中写配置文件,其中生成队列queue,交换机exchange并且进行绑定binding 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import
org.springframework.amqp.core.binding;
import
org.springframework.amqp.core.bindingbuilder;
import
org.springframework.amqp.core.queue;
import
org.springframework.amqp.core.topicexchange;
import
org.springframework.beans.factory.annotation.qualifier;
import
org.springframework.context.annotation.bean;
import
org.springframework.context.annotation.configuration;
/**
* @author:fdh
* @description:
* @date: create in 16:13 2017/12/22
*/
@configuration
public
class
senderconfigration {
/**
*@description: 新建队列 topic.messages
*@data:16:14 2017/12/22
*/
@bean
(name =
"messages"
)
public
queue queuemessages(){
return
new
queue(
"topic.messages"
);
}
/**
*@description: 定义交换器
*@data:16:15 2017/12/22
*/
@bean
public
topicexchange exchange(){
return
new
topicexchange(
"exchange"
);
}
/**
*@description: 交换机与消息队列进行绑定 队列messages绑定交换机with topic.messages
*@data:16:18 2017/12/22
*/
@bean
binding bindingexchangemessages(
@qualifier
(
"messages"
) queue queuemessages,topicexchange exchange){
return
bindingbuilder.bind(queuemessages).to(exchange).with(
"topic.messages"
);
}
}
|
2>. 第二步(项目a),生产者把消息发送到消息队列, 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* @author:fdh
* @description:
* @date: create in 14:15 2017/12/22
*/
@controller
public
class
rabbitcontroller {
@autowired
private
amqptemplate amqptemplate;
@requestmapping
(
"/sendss"
)
public
void
send1(){
amqptemplate.convertandsend(
"exchange"
,
"topic.messages"
,
"hello topic.messages rabbitmq"
);
}
}
|
3>. 接下来,在消费者(项目b)端写一个监听器,交换器会根据绑定的routing key(topic.messages)把生产者生产的消息放到匹配的消息队列中,监听器会监听相应的消息队列来获取路由到该消息队列上的消息.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import
org.springframework.beans.factory.annotation.autowired;
import
org.springframework.stereotype.component;
import
org.springframework.amqp.rabbit.annotation.rabbitlistener;
/**
* @ author:fdh
* @ description: 消息队列监听器
* @ date: create in 14:19 2017/12/22
*/
@component
public
class
receiver {
@rabbitlistener
(queues =
"topic.messages"
)
public
void
process2(string str1)
throws
classnotfoundexception{
system.out.println(
"messages :"
+str1);
system.out.println(thread.currentthread().getname()+
"接收到来自topic.message队列的消息: "
+str1);
}
|
这样,一个简单的字符串的传输便写好了,下面打开刚才定义的mapping: 192.168.1.111:8080/sendss 。
在消费者端的console窗口便会看到打印的消息 。
以上就是一个简单的传输字符串的例子了.
2. 下面重点介绍一下消费者和生产者之间对象的传输.
对象的传输,要现在生产者(a)中进行序列化,即把对象转化为字节数组进行传输,在消费者中,再把转化的字节数组反序列化为对象。序列化和反序列化的方法很多,这里采用的是java的serializable 接口 。
1>. 在生产者(项目a)和消费者(项目b)的项目中创建实体类.
!注意!:新建实体类boy.java 该实体类在项目a、b中的位置,必须一致,即包名必须一致,在本项目中,boy.java 在项目a、b中都是: import com.fengdonghao.shiro.bean.boy,
实体类也要一致.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package
com.fengdonghao.shiro.bean;
import
javax.persistence.*;
import
java.io.serializable;
/**
* @author:fdh
* @description:
* @date:create in11:14 2017/12/16
*/
@entity
public
class
boy
implements
serializable{
private
static
final
long
serialversionuid=1l;
@id
@generatedvalue
private
int
id;
private
string name;
private
int
age;
@override
public
string tostring() {
return
"boy{"
+
"age="
+ age +
", id="
+ id +
", name='"
+ name + '\
''
+
'}'
;
}
//此处省略getter 和setter 方法
}
|
2>. 在生产者(a)中配置 消息队列,交换器,并进行绑定binding,和在 例子1中的第一步是一样的 。
3>. 在生产者(a)中的rabbitcontroller.java 中另写一个mapping,如下 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@requestmapping
(
"/send"
)
public
void
sendmessage() {
boy boy=
new
boy();
boy.setname(
"tim"
);
boy.setage(
11
);
system.out.println(boy);
//以下是序列化操作
//write obj to file
objectoutputstream oos =
null
;
try
{
oos =
new
objectoutputstream(
new
fileoutputstream(
new
file(
"e:\\webpackage\\a.txt"
)));
//把序列化之后的字节数组暂时存放在该目录下
oos.writeobject(boy);
}
catch
(ioexception e) {
e.printstacktrace();
}
finally
{
ioutils.closequietly(oos);
}
rabbitmqservice.send(
"对象已序列化"
);
|
4>. 在消费者(b)中对字节数组进行反序列化.
在receiver中,重新编写例1重点的监听器 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@rabbitlistener
(queues =
"topic.messages"
)
public
void
process2(string str1) {
system.out.println(thread.currentthread().getname()+
"接收到来自topic.message队列的消息: "
+str1+
" 并进行反序列化"
);
file file =
new
file(
"e:\\webpackage\\a.txt"
);
//消费者和生产者中路径要保持一致,才能读取文件,进行解析
objectinputstream ois =
null
;
try
{
ois =
new
objectinputstream(
new
fileinputstream(file));
boy newuser = (boy) ois.readobject();
system.out.println(
"反序列之后:"
+newuser);
system.out.println(
"反序列之后getname:"
+newuser.getname());
system.out.println(
"反序列之后getage"
+newuser.getage());
}
catch
(ioexception e) {
e.printstacktrace();
}
catch
(classnotfoundexception e) {
e.printstacktrace();
}
finally
{
ioutils.closequietly(ois);
try
{
fileutils.forcedelete(file);
}
catch
(ioexception e) {
e.printstacktrace();
}
}
system.out.println(
"messages :"
+str1);
}
|
验证mapping: ip:8080/send 。
结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/east123321/article/details/78900791 。
最后此篇关于springboot集成rabbitMQ之对象传输的方法的文章就讲到这里了,如果你想了解更多关于springboot集成rabbitMQ之对象传输的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想读取 RabbitMQ 队列中未确认消息的负载或 messageId。这可能吗? 我想这样做的原因是我尝试使用 RabbitMQ 死信功能来构建一个循环以定期自动生成消息。简而言之,创建两个队列
除了 vFabric 由 VMWare 提供商业支持之外,vFabric RabbitMQ 和 RabbitMQ 之间的主要区别是什么? 最佳答案 来自 source : We also produc
RabbitMQ 集群中有如下三个节点。 在 RabbitMQ 中,有两个队列,q1 和 q2。 q1 和q2 的主副本分布在不同的节点上。两个队列都被其他节点镜像。 三个节点前面有一个负载均衡器。
我希望在谷歌计算引擎上实现 rabbitmq 来处理我的 android 和 ios 消息传递应用程序上的消息。我听说 rabbitmq 可能非常耗电,所以我想知道解决这个问题的最佳解决方案是什么?我
是否可以在 RabbitMQ 服务器(管理插件 View )中查看连接单元的主机名,而不仅仅是 IP/端口?我们使用动态连接位置,这样更容易识别客户...... 最佳答案 不,没有这样的内置功能。 作
我正在阅读 RabbitMQ in Action 书,仍在第 2 章中,但作者说的一件事让我感到困惑。您设置了一个交换并发送了一条消息,两个订阅者正在监听队列。当第一条消息进来时,第一个订阅者得到它,
我正在使用 RabbitMQ 将所有消息排队,并将消息作为 SMS 发送给各个消费者。我正在使用直接交换,并且我已经正确地创建了一个到带有路由键的队列的绑定(bind)。问题是,当我尝试发布消息时,我
我们正在使用微服务架构在 nodejs 中实现 Web-API。每个服务都会公开 HTTP 端点,以便应用程序/网站可以与其交互。为了同步不同的数据库,我们目前使用 RabbitMQ。微服务可以在扇出
我计划在 RabbitMQ 消息头中存储堆栈跟踪。消息 header 是否有大小限制? 最佳答案 RabbitMQ 默认使用 AMQP 版本 0.9.1。根据AMQP protocol specifi
无法理解 exclusive queue 和 exclusive consumer 之间的区别,想知道我是否理解正确。 假设我有一个 queue、consumer1 和 consumer2。 我的理解
发布到 RabbitMQ 队列(发布/订阅模型)时消息的最大大小是多少? 我在文档中看不到任何明确的限制,但我认为有一些指导方针。 提前致谢。 最佳答案 我在做比较亚马逊队列服务和 RabbitMQ
我可以使用 Publish/Subscribe 创建扇出交换RabbitMQ Java 教程,任何连接的消费者都会收到一条消息的副本。我不想以动态/编程方式声明交换和绑定(bind),而是想在连接任何
java的 native rabbitmq客户端允许在连接设置上设置心跳,例如: import com.rabbitmq.client.ConnectionFactory; ... Connectio
我开始着手一个新项目,我们被要求将系统构建为一系列微服务,使用 RabbitMQ 作为它们之间的通信层。 在开发 REST API 时,我倾向于使用接受 HTTP header 来控制版本控制,我看到
在 Rabbit MQ 中使用集群时,我计划使用竞争订阅者模式。 Producer : 1 Exchange : 1 direct Queue : 1 Consumers : n (multiple)
是否可以实现 aggregator pattern在 RabbitMQ 中? 我有 A … N在发送到另一个队列之前我需要等待/聚合的消息 X . 所以我想我会有一些 唯一 ID 确保消息被路由 独家
我正在使用RabbitMQ向用户发送通知。用户可以随时读取其队列。 我面临的问题是,队列在夜间充满了很多通知,而当用户在早上返回时,他必须顺序处理这些消息。这些通知中有很多甚至是重复的。 我认为在发布
是否可以延迟通过 RabbitMQ 发送消息? 例如,我想在 30 分钟后使客户端 session 过期,并且我发送了一条将在 30 分钟后处理的消息。 最佳答案 您可以尝试两种方法: 旧方法:在每个
RabbitMQ 在单个服务器上可以处理的最大队列数是多少? 这取决于内存吗?它取决于 erlang 进程吗? 最佳答案 RabbitMQ 代理内部没有任何硬编码限制。代理将利用所有可用资源(除非您对
RabbitMQ Server 使用或需要在防火墙上为节点集群打开哪些端口? 我的 /usr/lib/rabbitmq/bin/rabbitmq-env 设置如下,我假设需要 (35197)。 SER
我是一名优秀的程序员,十分优秀!