- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
老话说的好:做人要懂得变通,善于思考,有时稍微转个弯,也许问题就解决了.
言归正传,之前我们聊了 RabbitMQ 3.9.7 镜像模式集群的搭建,今天我们来聊聊 RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合.
服务器A IP:192.168.1.22 。
服务器B IP:192.168.1.8 。
服务器C IP:192.168.1.144 。
此三台服务器上已搭建好了 RabbitMQ镜像模式集群,镜像模式集群的搭建,可参见我的上一篇文章.
1
2
3
4
5
6
|
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>
2.5
.
5
</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
|
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
spring:
rabbitmq:
addresses:
192.168
.
1.22
:
5672
,
192.168
.
1.8
:
5672
,
192.168
.
1.144
:
5672
username: guest
password: guest
virtual-host: /
connection-timeout:
16000
# 启用消息确认模式
publisher-confirm-type: correlated
# 启用
return
消息模式
publisher-returns:
true
template:
mandatory:
true
|
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import
org.springframework.amqp.AmqpException;
import
org.springframework.amqp.core.MessagePostProcessor;
import
org.springframework.amqp.rabbit.connection.CorrelationData;
import
org.springframework.amqp.rabbit.core.RabbitTemplate;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.messaging.Message;
import
org.springframework.messaging.MessageHeaders;
import
org.springframework.messaging.support.MessageBuilder;
import
org.springframework.stereotype.Component;
import
java.util.Map;
@Component
public
class
Producer {
@Autowired
private
RabbitTemplate rabbitTemplate;
/**
* 确认回调
*/
final
RabbitTemplate.ConfirmCallback confirmCallback =
new
RabbitTemplate.ConfirmCallback() {
@Override
public
void
confirm(CorrelationData correlationData,
boolean
ack, String cause) {
// correlationData 唯一标识
// ack mq是否收到消息
// cause 失败原因
System.out.println(
"correlationData:"
+ correlationData.getId());
System.out.println(
"ack:"
+ ack);
System.out.println(
"cause:"
+ cause);
}
};
/**
* 发送消息
* @param messageBody 消息体
* @param headers 附加属性
* @throws Exception
*/
public
void
sendMessage(String messageBody, Map<String, Object> headers, String id)
throws
Exception {
MessageHeaders messageHeaders =
new
MessageHeaders(headers);
Message<String> message = MessageBuilder.createMessage(messageBody, messageHeaders);
rabbitTemplate.setConfirmCallback(confirmCallback);
String exchangeName =
"exchange-hello"
;
String routingKey =
"test.123"
;
CorrelationData correlationData =
new
CorrelationData(id);
rabbitTemplate.convertAndSend(exchangeName, routingKey, message,
new
MessagePostProcessor() {
/**
* 发送消息后做的事情
* @param message
* @return
* @throws AmqpException
*/
@Override
public
org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message)
throws
AmqpException {
return
message;
}
}, correlationData);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
spring:
rabbitmq:
addresses:
192.168
.
1.22
:
5672
,
192.168
.
1.8
:
5672
,
192.168
.
1.144
:
5672
username: guest
password: guest
virtual-host: /
connection-timeout:
16000
listener:
simple:
# 设置为手工ACK
acknowledge-mode: manual
concurrency:
5
prefetch:
1
max-concurrency:
10
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
com.rabbitmq.client.Channel;
import
org.springframework.amqp.rabbit.annotation.*;
import
org.springframework.amqp.support.AmqpHeaders;
import
org.springframework.messaging.Message;
import
org.springframework.stereotype.Component;
@Component
public
class
Consumer {
@RabbitListener
(bindings =
@QueueBinding
(
value =
@Queue
(value =
"queue-hello"
, durable =
"true"
),
exchange =
@Exchange
(value =
"exchange-hello"
, durable =
"true"
, type =
"topic"
),
key =
"test.*"
))
@RabbitHandler
public
void
onMessage(Message message, Channel channel)
throws
Exception {
System.out.println(
"收到消息:"
+ message.getPayload());
Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
channel.basicAck(deliveryTag,
false
);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@RestController
@RequestMapping
(
"/mq"
)
public
class
RabbitmqController {
@Autowired
private
Producer producer;
@GetMapping
(
"/sendMessage"
)
public
String sendMessage(
@RequestParam
String messageBody,
@RequestParam
String id)
throws
Exception {
Map<String, Object> headers =
new
HashMap<>();
producer.sendMessage(messageBody, headers, id);
return
"success"
;
}
}
|
到此这篇关于RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合的文章就介绍到这了,更多相关RabbitMQ镜像模式集群内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://www.cnblogs.com/w84422/p/15361662.html 。
最后此篇关于RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合的文章就讲到这里了,如果你想了解更多关于RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
对此感到疯狂,真的缺少一些东西。 我有webpack 4.6.0,webpack-cli ^ 2.1.2,所以是最新的。 在文档(https://webpack.js.org/concepts/mod
object Host "os.google.com" { import "windows" address = "linux.google.com" groups = ["linux"] } obj
每当我安装我的应用程序时,我都可以将数据库从 Assets 文件夹复制到 /data/data/packagename/databases/ .到此为止,应用程序工作得很好。 但 10 或 15 秒后
我在 cc 模式缓冲区中使用 hideshow.el 来折叠我不查看的文件部分。 如果能够在 XML 文档中做到这一点就好了。我使用 emacs 22.2.1 和内置的 sgml-mode 进行 xm
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
根据java: public Scanner useDelimiter(String pattern) Sets this scanner's delimiting pattern to a patt
我读过一些关于 PRG 模式以及它如何防止用户重新提交表单的文章。比如this post有一张不错的图: 我能理解为什么在收到 2xx 后用户刷新页面时不会发生表单提交。但我仍然想知道: (1) 如果
看看下面的图片,您可能会清楚地看到这一点。 那么如何在带有其他一些 View 的简单屏幕中实现没有任何弹出/对话框/模式的微调器日期选择器? 我在整个网络上进行了谷歌搜索,但没有找到与之相关的任何合适
我不知道该怎么做,我一直遇到问题。 以下是代码: rows = int(input()) for i in range(1,rows): for j in range(1,i+1):
我想为重写创建一个正则表达式。 将所有请求重写为 index.php(不需要匹配),它不是以/api 开头,或者不是以('.html',或'.js'或'.css'或'.png'结束) 我的例子还是这样
MVC模式代表 Model-View-Controller(模型-视图-控制器) 模式 MVC模式用于应用程序的分层开发 Model(模型) - 模型代表一个存取数据的对象或 JAVA PO
我想为组织模式创建一个 RDF 模式世界。您可能知道,组织模式文档基于层次结构大纲,其中标题是主要的分组实体。 * March auxiliary :PROPERTIES: :HLEVEL: 1 :E
我正在编写一个可以从文件中读取 JSON 数据的软件。该文件包含“person”——一个值为对象数组的对象。我打算使用 JSON 模式验证库来验证内容,而不是自己编写代码。符合代表以下数据的 JSON
假设我有 4 张 table 人 公司 团体 和 账单 现在bills/persons和bills/companys和bills/groups之间是多对多的关系。 我看到了 4 种可能的 sql 模式
假设您有这样的文档: doc1: id:1 text: ... references: Journal1, 2013, pag 123 references: Journal2, 2014,
我有这个架构。它检查评论,目前工作正常。 var schema = { id: '', type: 'object', additionalProperties: false, pro
这可能很简单,但有人可以解释为什么以下模式匹配不明智吗?它说其他规则,例如1, 0, _ 永远不会匹配。 let matchTest(n : int) = let ran = new Rand
我有以下选择序列作为 XML 模式的一部分。理想情况下,我想要一个序列: 来自 my:namespace 的元素必须严格解析。 来自任何其他命名空间的元素,不包括 ##targetNamespace和
我希望编写一个 json 模式来涵盖这个(简化的)示例 { "errorMessage": "", "nbRunningQueries": 0, "isError": Fals
首先,我是 f# 的新手,所以也许答案很明显,但我没有看到。所以我有一些带有 id 和值的元组。我知道我正在寻找的 id,我想从我传入的三个元组中选择正确的元组。我打算用两个 match 语句来做到这
我是一名优秀的程序员,十分优秀!