sycommon-python-lib 0.1.10__py3-none-any.whl → 0.1.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sycommon-python-lib might be problematic. Click here for more details.
- command/cli.py +167 -0
- sycommon/health/health_check.py +1 -1
- sycommon/models/base_http.py +2 -2
- sycommon/rabbitmq/rabbitmq_client.py +545 -401
- sycommon/rabbitmq/rabbitmq_service.py +442 -280
- sycommon/services.py +11 -8
- sycommon/synacos/feign.py +338 -126
- sycommon/synacos/nacos_service.py +62 -5
- {sycommon_python_lib-0.1.10.dist-info → sycommon_python_lib-0.1.12.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.1.10.dist-info → sycommon_python_lib-0.1.12.dist-info}/RECORD +13 -11
- sycommon_python_lib-0.1.12.dist-info/entry_points.txt +2 -0
- {sycommon_python_lib-0.1.10.dist-info → sycommon_python_lib-0.1.12.dist-info}/top_level.txt +1 -0
- {sycommon_python_lib-0.1.10.dist-info → sycommon_python_lib-0.1.12.dist-info}/WHEEL +0 -0
|
@@ -1,21 +1,35 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import logging
|
|
3
|
-
import aio_pika
|
|
4
3
|
import json
|
|
5
|
-
from
|
|
6
|
-
from
|
|
4
|
+
from typing import Callable, Coroutine, Optional, Dict, Any, Union, Set, List
|
|
5
|
+
from aio_pika import connect_robust, Message, DeliveryMode, ExchangeType
|
|
6
|
+
from aio_pika.abc import (
|
|
7
|
+
AbstractConnection,
|
|
8
|
+
AbstractChannel,
|
|
9
|
+
AbstractExchange,
|
|
10
|
+
AbstractQueue,
|
|
11
|
+
AbstractIncomingMessage,
|
|
12
|
+
ConsumerTag
|
|
13
|
+
)
|
|
14
|
+
from aiormq.exceptions import ChannelInvalidStateError, ConnectionClosed
|
|
7
15
|
|
|
8
16
|
from sycommon.models.mqmsg_model import MQMsgModel
|
|
9
|
-
from aiormq.exceptions import ChannelInvalidStateError, ConnectionClosed
|
|
10
17
|
|
|
11
18
|
# 最大重试次数限制
|
|
12
19
|
MAX_RETRY_COUNT = 3
|
|
13
20
|
|
|
21
|
+
logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
14
23
|
|
|
15
24
|
class RabbitMQClient:
|
|
25
|
+
"""
|
|
26
|
+
RabbitMQ客户端,支持集群多节点配置,基于aio-pika实现
|
|
27
|
+
提供自动故障转移、连接恢复和消息可靠性保障
|
|
28
|
+
"""
|
|
29
|
+
|
|
16
30
|
def __init__(
|
|
17
31
|
self,
|
|
18
|
-
|
|
32
|
+
hosts: List[str],
|
|
19
33
|
port: int,
|
|
20
34
|
username: str,
|
|
21
35
|
password: str,
|
|
@@ -31,61 +45,102 @@ class RabbitMQClient:
|
|
|
31
45
|
connection_timeout: int = 10,
|
|
32
46
|
rpc_timeout: int = 10,
|
|
33
47
|
app_name: str = "",
|
|
34
|
-
reconnection_delay: int =
|
|
48
|
+
reconnection_delay: int = 1,
|
|
35
49
|
max_reconnection_attempts: int = 5,
|
|
36
|
-
heartbeat: int =
|
|
37
|
-
|
|
50
|
+
heartbeat: int = 10,
|
|
51
|
+
prefetch_count: int = 2,
|
|
52
|
+
consumption_stall_threshold: int = 10
|
|
38
53
|
):
|
|
39
|
-
"""
|
|
40
|
-
|
|
54
|
+
"""
|
|
55
|
+
初始化RabbitMQ客户端,支持集群多节点配置
|
|
56
|
+
|
|
57
|
+
:param hosts: RabbitMQ主机地址列表(集群节点)
|
|
58
|
+
:param port: RabbitMQ端口
|
|
59
|
+
:param username: 用户名
|
|
60
|
+
:param password: 密码
|
|
61
|
+
:param virtualhost: 虚拟主机
|
|
62
|
+
:param exchange_name: 交换机名称
|
|
63
|
+
:param exchange_type: 交换机类型
|
|
64
|
+
:param queue_name: 队列名称
|
|
65
|
+
:param routing_key: 路由键
|
|
66
|
+
:param durable: 是否持久化
|
|
67
|
+
:param auto_delete: 是否自动删除
|
|
68
|
+
:param auto_parse_json: 是否自动解析JSON消息
|
|
69
|
+
:param create_if_not_exists: 如果资源不存在是否创建
|
|
70
|
+
:param connection_timeout: 连接超时时间(秒)
|
|
71
|
+
:param rpc_timeout: RPC操作超时时间(秒)
|
|
72
|
+
:param app_name: 应用名称,用于标识连接
|
|
73
|
+
:param reconnection_delay: 重连延迟(秒)
|
|
74
|
+
:param max_reconnection_attempts: 最大重连尝试次数
|
|
75
|
+
:param heartbeat: 心跳间隔(秒)
|
|
76
|
+
:param prefetch_count: 预取消息数量
|
|
77
|
+
:param consumption_stall_threshold: 消费停滞检测阈值(秒)
|
|
78
|
+
"""
|
|
79
|
+
# 连接参数 - 支持多主机
|
|
80
|
+
self.hosts = [host.strip() for host in hosts if host.strip()]
|
|
81
|
+
if not self.hosts:
|
|
82
|
+
raise ValueError("至少需要提供一个RabbitMQ主机地址")
|
|
41
83
|
self.port = port
|
|
42
84
|
self.username = username
|
|
43
85
|
self.password = password
|
|
44
86
|
self.virtualhost = virtualhost
|
|
87
|
+
self.app_name = app_name or "rabbitmq-client"
|
|
88
|
+
|
|
89
|
+
# 交换器和队列参数
|
|
45
90
|
self.exchange_name = exchange_name
|
|
46
91
|
self.exchange_type = ExchangeType(exchange_type)
|
|
47
92
|
self.queue_name = queue_name
|
|
48
93
|
self.routing_key = routing_key
|
|
49
94
|
self.durable = durable
|
|
50
95
|
self.auto_delete = auto_delete
|
|
96
|
+
|
|
97
|
+
# 行为控制参数
|
|
51
98
|
self.auto_parse_json = auto_parse_json
|
|
52
99
|
self.create_if_not_exists = create_if_not_exists
|
|
53
100
|
self.connection_timeout = connection_timeout
|
|
54
101
|
self.rpc_timeout = rpc_timeout
|
|
55
|
-
self.
|
|
56
|
-
|
|
57
|
-
# 连接保活相关配置
|
|
58
|
-
self.heartbeat = heartbeat
|
|
59
|
-
self.keepalive_interval = keepalive_interval
|
|
60
|
-
self.last_activity_timestamp = asyncio.get_event_loop().time()
|
|
102
|
+
self.prefetch_count = prefetch_count
|
|
61
103
|
|
|
62
|
-
#
|
|
104
|
+
# 重连和保活参数
|
|
63
105
|
self.reconnection_delay = reconnection_delay
|
|
64
106
|
self.max_reconnection_attempts = max_reconnection_attempts
|
|
107
|
+
self.heartbeat = heartbeat
|
|
65
108
|
|
|
66
|
-
#
|
|
67
|
-
self.
|
|
68
|
-
self.channel: Optional[aio_pika.RobustChannel] = None
|
|
69
|
-
self.exchange: Optional[aio_pika.Exchange] = None
|
|
70
|
-
self.queue: Optional[aio_pika.Queue] = None
|
|
109
|
+
# 消息处理参数
|
|
110
|
+
self.consumption_stall_threshold = consumption_stall_threshold
|
|
71
111
|
|
|
112
|
+
# 连接和通道对象
|
|
113
|
+
self.connection: Optional[AbstractConnection] = None
|
|
114
|
+
self.channel: Optional[AbstractChannel] = None
|
|
115
|
+
self.exchange: Optional[AbstractExchange] = None
|
|
116
|
+
self.queue: Optional[AbstractQueue] = None
|
|
117
|
+
|
|
118
|
+
# 当前活跃连接的主机
|
|
119
|
+
self._active_host: Optional[str] = None
|
|
120
|
+
|
|
121
|
+
# 状态跟踪
|
|
122
|
+
self.actual_queue_name: Optional[str] = None
|
|
72
123
|
self._exchange_exists = False
|
|
73
124
|
self._queue_exists = False
|
|
74
125
|
self._queue_bound = False
|
|
126
|
+
self._is_consuming = False
|
|
127
|
+
self._closed = False
|
|
128
|
+
self._consumer_tag: Optional[ConsumerTag] = None
|
|
129
|
+
self._last_activity_timestamp = asyncio.get_event_loop().time()
|
|
130
|
+
self._last_message_processed = asyncio.get_event_loop().time()
|
|
75
131
|
|
|
76
|
-
#
|
|
132
|
+
# 任务和处理器
|
|
77
133
|
self.message_handler: Optional[Callable[
|
|
78
|
-
[Union[
|
|
79
|
-
Coroutine
|
|
134
|
+
[Union[Dict[str, Any], str], AbstractIncomingMessage],
|
|
135
|
+
Coroutine[Any, Any, None]
|
|
80
136
|
]] = None
|
|
81
|
-
|
|
82
|
-
# 消费相关
|
|
83
|
-
self._consumer_tag: Optional[str] = None
|
|
84
137
|
self._consuming_task: Optional[asyncio.Task] = None
|
|
85
|
-
self._is_consuming: bool = False
|
|
86
138
|
self._reconnect_task: Optional[asyncio.Task] = None
|
|
87
|
-
self.
|
|
88
|
-
self.
|
|
139
|
+
self._keepalive_task: Optional[asyncio.Task] = None
|
|
140
|
+
self._monitor_task: Optional[asyncio.Task] = None
|
|
141
|
+
|
|
142
|
+
# 消息处理跟踪
|
|
143
|
+
self._processing_message_ids: Set[str] = set()
|
|
89
144
|
|
|
90
145
|
@property
|
|
91
146
|
def is_connected(self) -> bool:
|
|
@@ -94,18 +149,24 @@ class RabbitMQClient:
|
|
|
94
149
|
self.connection is not None and
|
|
95
150
|
not self.connection.is_closed and
|
|
96
151
|
self.channel is not None and
|
|
97
|
-
not self.channel.is_closed
|
|
152
|
+
not self.channel.is_closed and
|
|
153
|
+
self.exchange is not None)
|
|
98
154
|
|
|
99
|
-
def _update_activity_timestamp(self):
|
|
155
|
+
def _update_activity_timestamp(self) -> None:
|
|
100
156
|
"""更新最后活动时间戳"""
|
|
101
|
-
self.
|
|
157
|
+
self._last_activity_timestamp = asyncio.get_event_loop().time()
|
|
158
|
+
|
|
159
|
+
def _update_message_processed_timestamp(self) -> None:
|
|
160
|
+
"""更新最后消息处理时间戳"""
|
|
161
|
+
self._last_message_processed = asyncio.get_event_loop().time()
|
|
102
162
|
|
|
103
163
|
async def _check_exchange_exists(self) -> bool:
|
|
104
|
-
"""
|
|
164
|
+
"""检查交换机是否存在"""
|
|
105
165
|
if not self.channel:
|
|
106
166
|
return False
|
|
107
167
|
|
|
108
168
|
try:
|
|
169
|
+
# 使用被动模式检查交换机是否存在
|
|
109
170
|
await asyncio.wait_for(
|
|
110
171
|
self.channel.declare_exchange(
|
|
111
172
|
name=self.exchange_name,
|
|
@@ -118,18 +179,21 @@ class RabbitMQClient:
|
|
|
118
179
|
self._update_activity_timestamp()
|
|
119
180
|
return True
|
|
120
181
|
except asyncio.TimeoutError:
|
|
121
|
-
|
|
182
|
+
logger.error(
|
|
183
|
+
f"检查交换机 '{self.exchange_name}' 超时 (主机: {self._active_host})")
|
|
122
184
|
return False
|
|
123
185
|
except Exception as e:
|
|
124
|
-
|
|
186
|
+
logger.debug(
|
|
187
|
+
f"交换机 '{self.exchange_name}' 不存在: {str(e)} (主机: {self._active_host})")
|
|
125
188
|
return False
|
|
126
189
|
|
|
127
190
|
async def _check_queue_exists(self) -> bool:
|
|
128
|
-
"""
|
|
191
|
+
"""检查队列是否存在"""
|
|
129
192
|
if not self.channel or not self.queue_name:
|
|
130
193
|
return False
|
|
131
194
|
|
|
132
195
|
try:
|
|
196
|
+
# 使用被动模式检查队列是否存在
|
|
133
197
|
await asyncio.wait_for(
|
|
134
198
|
self.channel.declare_queue(
|
|
135
199
|
name=self.queue_name,
|
|
@@ -141,21 +205,24 @@ class RabbitMQClient:
|
|
|
141
205
|
self._update_activity_timestamp()
|
|
142
206
|
return True
|
|
143
207
|
except asyncio.TimeoutError:
|
|
144
|
-
|
|
208
|
+
logger.error(
|
|
209
|
+
f"检查队列 '{self.queue_name}' 超时 (主机: {self._active_host})")
|
|
145
210
|
return False
|
|
146
211
|
except Exception as e:
|
|
147
|
-
|
|
212
|
+
logger.debug(
|
|
213
|
+
f"队列 '{self.queue_name}' 不存在: {str(e)} (主机: {self._active_host})")
|
|
148
214
|
return False
|
|
149
215
|
|
|
150
216
|
async def _bind_queue(self) -> bool:
|
|
151
|
-
"""
|
|
217
|
+
"""将队列绑定到交换机"""
|
|
152
218
|
if not self.channel or not self.queue or not self.exchange:
|
|
153
219
|
return False
|
|
154
220
|
|
|
155
|
-
retries = 2
|
|
221
|
+
retries = 2
|
|
222
|
+
bind_routing_key = self.routing_key if self.routing_key else '#'
|
|
223
|
+
|
|
156
224
|
for attempt in range(retries + 1):
|
|
157
225
|
try:
|
|
158
|
-
bind_routing_key = self.routing_key if self.routing_key else '#'
|
|
159
226
|
await asyncio.wait_for(
|
|
160
227
|
self.queue.bind(
|
|
161
228
|
self.exchange,
|
|
@@ -165,319 +232,380 @@ class RabbitMQClient:
|
|
|
165
232
|
)
|
|
166
233
|
self._queue_bound = True
|
|
167
234
|
self._update_activity_timestamp()
|
|
168
|
-
|
|
169
|
-
f"队列 '{self.queue_name}' 已绑定到交换机 '{self.exchange_name}',路由键: {bind_routing_key}")
|
|
235
|
+
logger.info(
|
|
236
|
+
f"队列 '{self.queue_name}' 已绑定到交换机 '{self.exchange_name}',路由键: {bind_routing_key} (主机: {self._active_host})")
|
|
170
237
|
return True
|
|
171
238
|
except asyncio.TimeoutError:
|
|
172
|
-
|
|
173
|
-
f"队列 '{self.queue_name}' 绑定超时(第{attempt+1}次尝试)")
|
|
174
|
-
if attempt >= retries:
|
|
175
|
-
self._queue_bound = False
|
|
176
|
-
return False
|
|
177
|
-
await asyncio.sleep(1)
|
|
239
|
+
logger.warning(
|
|
240
|
+
f"队列 '{self.queue_name}' 绑定超时(第{attempt+1}次尝试)(主机: {self._active_host})")
|
|
178
241
|
except Exception as e:
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
242
|
+
logger.error(
|
|
243
|
+
f"队列绑定失败(第{attempt+1}次尝试): {str(e)} (主机: {self._active_host})")
|
|
244
|
+
|
|
245
|
+
if attempt < retries:
|
|
183
246
|
await asyncio.sleep(1)
|
|
247
|
+
|
|
248
|
+
self._queue_bound = False
|
|
184
249
|
return False
|
|
185
250
|
|
|
251
|
+
async def _try_connect_host(self, host: str) -> AbstractConnection:
|
|
252
|
+
"""尝试连接单个主机"""
|
|
253
|
+
try:
|
|
254
|
+
logger.debug(f"尝试连接主机: {host}:{self.port}")
|
|
255
|
+
return await asyncio.wait_for(
|
|
256
|
+
connect_robust(
|
|
257
|
+
host=host,
|
|
258
|
+
port=self.port,
|
|
259
|
+
login=self.username,
|
|
260
|
+
password=self.password,
|
|
261
|
+
virtualhost=self.virtualhost,
|
|
262
|
+
heartbeat=self.heartbeat,
|
|
263
|
+
loop=asyncio.get_event_loop(),
|
|
264
|
+
client_properties={
|
|
265
|
+
"connection_name": f"{self.app_name}@{host}"
|
|
266
|
+
}
|
|
267
|
+
),
|
|
268
|
+
timeout=self.connection_timeout
|
|
269
|
+
)
|
|
270
|
+
except Exception as e:
|
|
271
|
+
logger.warning(f"连接主机 {host}:{self.port} 失败: {str(e)}")
|
|
272
|
+
raise
|
|
273
|
+
|
|
186
274
|
async def connect(self, force_reconnect: bool = False, declare_queue: bool = True) -> None:
|
|
187
|
-
"""
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
275
|
+
"""
|
|
276
|
+
建立与RabbitMQ集群的连接(支持多节点故障转移)并初始化所需资源
|
|
277
|
+
|
|
278
|
+
:param force_reconnect: 是否强制重新连接
|
|
279
|
+
:param declare_queue: 是否声明队列
|
|
280
|
+
"""
|
|
281
|
+
logger.debug(
|
|
282
|
+
f"连接参数 - force_reconnect={force_reconnect}, "
|
|
283
|
+
f"declare_queue={declare_queue}, create_if_not_exists={self.create_if_not_exists}, "
|
|
284
|
+
f"主机列表: {self.hosts}"
|
|
192
285
|
)
|
|
193
286
|
|
|
287
|
+
# 如果已连接且不强制重连,则直接返回
|
|
194
288
|
if self.is_connected and not force_reconnect:
|
|
195
289
|
return
|
|
196
290
|
|
|
197
|
-
#
|
|
291
|
+
# 取消正在进行的重连任务
|
|
198
292
|
if self._reconnect_task and not self._reconnect_task.done():
|
|
199
293
|
self._reconnect_task.cancel()
|
|
200
294
|
|
|
201
|
-
|
|
202
|
-
f"尝试连接RabbitMQ -
|
|
203
|
-
f"虚拟主机: {self.virtualhost}, "
|
|
204
|
-
f"队列: {self.queue_name}, "
|
|
205
|
-
f"声明队列: {declare_queue}, "
|
|
206
|
-
f"允许创建: {self.create_if_not_exists}"
|
|
295
|
+
logger.debug(
|
|
296
|
+
f"尝试连接RabbitMQ集群 - 主机数量: {len(self.hosts)}, "
|
|
297
|
+
f"虚拟主机: {self.virtualhost}, 队列: {self.queue_name}"
|
|
207
298
|
)
|
|
208
299
|
|
|
209
300
|
# 重置状态
|
|
210
301
|
self._exchange_exists = False
|
|
211
302
|
self._queue_exists = False
|
|
212
303
|
self._queue_bound = False
|
|
304
|
+
self._active_host = None
|
|
213
305
|
|
|
214
306
|
retries = 0
|
|
215
307
|
last_exception = None
|
|
216
308
|
|
|
217
|
-
while retries <
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
self.connection = await asyncio.wait_for(
|
|
225
|
-
aio_pika.connect_robust(
|
|
226
|
-
host=self.host,
|
|
227
|
-
port=self.port,
|
|
228
|
-
login=self.username,
|
|
229
|
-
password=self.password,
|
|
230
|
-
virtualhost=self.virtualhost,
|
|
231
|
-
heartbeat=self.heartbeat,
|
|
232
|
-
client_properties={
|
|
233
|
-
"connection_name": self.app_name or "rabbitmq-client"}
|
|
234
|
-
),
|
|
235
|
-
timeout=self.connection_timeout
|
|
236
|
-
)
|
|
309
|
+
while retries < self.max_reconnection_attempts:
|
|
310
|
+
# 遍历所有主机尝试连接(故障转移)
|
|
311
|
+
for host in self.hosts:
|
|
312
|
+
try:
|
|
313
|
+
# 关闭现有连接
|
|
314
|
+
if self.connection and not self.connection.is_closed:
|
|
315
|
+
await self.connection.close()
|
|
237
316
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
self.
|
|
241
|
-
timeout=self.rpc_timeout
|
|
242
|
-
)
|
|
243
|
-
await self.channel.set_qos(prefetch_count=2)
|
|
317
|
+
# 尝试连接当前主机
|
|
318
|
+
self.connection = await self._try_connect_host(host)
|
|
319
|
+
self._active_host = host
|
|
244
320
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
if self.create_if_not_exists:
|
|
249
|
-
# 创建交换机
|
|
250
|
-
self.exchange = await asyncio.wait_for(
|
|
251
|
-
self.channel.declare_exchange(
|
|
252
|
-
name=self.exchange_name,
|
|
253
|
-
type=self.exchange_type,
|
|
254
|
-
durable=self.durable,
|
|
255
|
-
auto_delete=self.auto_delete
|
|
256
|
-
),
|
|
257
|
-
timeout=self.rpc_timeout
|
|
258
|
-
)
|
|
259
|
-
self._exchange_exists = True
|
|
260
|
-
logging.info(f"已创建交换机 '{self.exchange_name}'")
|
|
261
|
-
else:
|
|
262
|
-
raise Exception(
|
|
263
|
-
f"交换机 '{self.exchange_name}' 不存在且不允许自动创建")
|
|
264
|
-
else:
|
|
265
|
-
# 获取已有交换机
|
|
266
|
-
self.exchange = await asyncio.wait_for(
|
|
267
|
-
self.channel.get_exchange(self.exchange_name),
|
|
321
|
+
# 创建通道
|
|
322
|
+
self.channel = await asyncio.wait_for(
|
|
323
|
+
self.connection.channel(),
|
|
268
324
|
timeout=self.rpc_timeout
|
|
269
325
|
)
|
|
270
|
-
logging.info(f"使用已存在的交换机 '{self.exchange_name}'")
|
|
271
326
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if
|
|
327
|
+
# 设置预取计数,控制消息公平分发
|
|
328
|
+
await self.channel.set_qos(prefetch_count=self.prefetch_count)
|
|
329
|
+
|
|
330
|
+
# 处理交换机
|
|
331
|
+
exchange_exists = await self._check_exchange_exists()
|
|
332
|
+
if not exchange_exists:
|
|
333
|
+
if self.create_if_not_exists:
|
|
334
|
+
# 创建交换机
|
|
335
|
+
self.exchange = await asyncio.wait_for(
|
|
336
|
+
self.channel.declare_exchange(
|
|
337
|
+
name=self.exchange_name,
|
|
338
|
+
type=self.exchange_type,
|
|
339
|
+
durable=self.durable,
|
|
340
|
+
auto_delete=self.auto_delete
|
|
341
|
+
),
|
|
342
|
+
timeout=self.rpc_timeout
|
|
343
|
+
)
|
|
344
|
+
self._exchange_exists = True
|
|
345
|
+
logger.info(
|
|
346
|
+
f"已创建交换机 '{self.exchange_name}' (主机: {self._active_host})")
|
|
347
|
+
else:
|
|
279
348
|
raise Exception(
|
|
280
|
-
f"
|
|
281
|
-
|
|
282
|
-
# 创建队列
|
|
283
|
-
self.queue = await asyncio.wait_for(
|
|
284
|
-
self.channel.declare_queue(
|
|
285
|
-
name=self.queue_name,
|
|
286
|
-
durable=self.durable,
|
|
287
|
-
auto_delete=self.auto_delete,
|
|
288
|
-
exclusive=False,
|
|
289
|
-
passive=False
|
|
290
|
-
),
|
|
291
|
-
timeout=self.rpc_timeout
|
|
292
|
-
)
|
|
293
|
-
self._queue_exists = True
|
|
294
|
-
logging.info(f"已创建队列 '{self.queue_name}'")
|
|
349
|
+
f"交换机 '{self.exchange_name}' 不存在且不允许自动创建 (主机: {self._active_host})")
|
|
295
350
|
else:
|
|
296
|
-
#
|
|
297
|
-
self.
|
|
298
|
-
self.channel.
|
|
351
|
+
# 获取已有交换机
|
|
352
|
+
self.exchange = await asyncio.wait_for(
|
|
353
|
+
self.channel.get_exchange(self.exchange_name),
|
|
299
354
|
timeout=self.rpc_timeout
|
|
300
355
|
)
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
356
|
+
logger.info(
|
|
357
|
+
f"使用已存在的交换机 '{self.exchange_name}' (主机: {self._active_host})")
|
|
358
|
+
|
|
359
|
+
# 处理队列
|
|
360
|
+
if declare_queue and self.queue_name:
|
|
361
|
+
queue_exists = await self._check_queue_exists()
|
|
362
|
+
|
|
363
|
+
if not queue_exists:
|
|
364
|
+
if not self.create_if_not_exists:
|
|
365
|
+
raise Exception(
|
|
366
|
+
f"队列 '{self.queue_name}' 不存在且不允许自动创建 (主机: {self._active_host})")
|
|
367
|
+
|
|
368
|
+
# 创建队列
|
|
369
|
+
self.queue = await asyncio.wait_for(
|
|
370
|
+
self.channel.declare_queue(
|
|
371
|
+
name=self.queue_name,
|
|
372
|
+
durable=self.durable,
|
|
373
|
+
auto_delete=self.auto_delete,
|
|
374
|
+
exclusive=False
|
|
375
|
+
),
|
|
376
|
+
timeout=self.rpc_timeout
|
|
377
|
+
)
|
|
378
|
+
self._queue_exists = True
|
|
379
|
+
self.actual_queue_name = self.queue_name
|
|
380
|
+
logger.info(
|
|
381
|
+
f"已创建队列 '{self.queue_name}' (主机: {self._active_host})")
|
|
382
|
+
else:
|
|
383
|
+
# 获取已有队列
|
|
384
|
+
self.queue = await asyncio.wait_for(
|
|
385
|
+
self.channel.get_queue(self.queue_name),
|
|
386
|
+
timeout=self.rpc_timeout
|
|
387
|
+
)
|
|
388
|
+
self.actual_queue_name = self.queue_name
|
|
389
|
+
logger.info(
|
|
390
|
+
f"使用已存在的队列 '{self.queue_name}' (主机: {self._active_host})")
|
|
391
|
+
|
|
392
|
+
# 绑定队列到交换机
|
|
393
|
+
if self.queue and self.exchange:
|
|
394
|
+
bound = await self._bind_queue()
|
|
395
|
+
if not bound:
|
|
396
|
+
raise Exception(
|
|
397
|
+
f"队列 '{self.queue_name}' 绑定到交换机 '{self.exchange_name}' 失败 (主机: {self._active_host})")
|
|
398
|
+
else:
|
|
307
399
|
raise Exception(
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
400
|
+
"队列或交换机未正确初始化 (主机: {self._active_host})")
|
|
401
|
+
else:
|
|
402
|
+
# 不声明队列时的状态处理
|
|
403
|
+
self.queue = None
|
|
404
|
+
self.actual_queue_name = None
|
|
405
|
+
self._queue_exists = False
|
|
406
|
+
self._queue_bound = False
|
|
407
|
+
logger.debug(
|
|
408
|
+
f"跳过队列 '{self.queue_name}' 的声明和绑定 (主机: {self._active_host})")
|
|
409
|
+
|
|
410
|
+
# 验证连接状态
|
|
411
|
+
if not self.is_connected:
|
|
412
|
+
raise Exception(
|
|
413
|
+
f"连接验证失败,状态异常 (主机: {self._active_host})")
|
|
315
414
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
415
|
+
# 如果之前在消费,重新开始消费
|
|
416
|
+
if self._is_consuming and self.message_handler:
|
|
417
|
+
await self.start_consuming()
|
|
319
418
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
419
|
+
# 启动连接监控和保活任务
|
|
420
|
+
self._start_monitoring()
|
|
421
|
+
self._start_keepalive()
|
|
323
422
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
423
|
+
self._update_activity_timestamp()
|
|
424
|
+
logger.info(
|
|
425
|
+
f"RabbitMQ客户端连接成功 (主机: {self._active_host}, 队列: {self.actual_queue_name})")
|
|
426
|
+
return
|
|
328
427
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
428
|
+
except Exception as e:
|
|
429
|
+
last_exception = e
|
|
430
|
+
logger.warning(
|
|
431
|
+
f"主机 {host} 连接处理失败: {str(e)},尝试下一个主机...")
|
|
432
|
+
# 清理当前失败的连接资源
|
|
433
|
+
if self.connection and not self.connection.is_closed:
|
|
434
|
+
await self.connection.close()
|
|
435
|
+
self.connection = None
|
|
436
|
+
self.channel = None
|
|
437
|
+
self.exchange = None
|
|
438
|
+
self.queue = None
|
|
439
|
+
|
|
440
|
+
# 所有主机都尝试失败,进行重试
|
|
441
|
+
retries += 1
|
|
442
|
+
logger.warning(
|
|
443
|
+
f"集群连接失败({retries}/{self.max_reconnection_attempts}),所有主机均无法连接,重试中...")
|
|
334
444
|
|
|
335
|
-
if retries <
|
|
445
|
+
if retries < self.max_reconnection_attempts:
|
|
336
446
|
await asyncio.sleep(self.reconnection_delay)
|
|
337
447
|
|
|
338
|
-
|
|
448
|
+
logger.error(f"最终连接失败: {str(last_exception)}")
|
|
339
449
|
raise Exception(
|
|
340
|
-
f"经过
|
|
450
|
+
f"经过{self.max_reconnection_attempts}次重试后仍无法连接到RabbitMQ集群。最后错误: {str(last_exception)}")
|
|
341
451
|
|
|
342
|
-
def
|
|
343
|
-
"""
|
|
344
|
-
if self._closed:
|
|
452
|
+
def _start_monitoring(self) -> None:
|
|
453
|
+
"""启动连接和消费监控任务,支持集群节点故障检测"""
|
|
454
|
+
if self._closed or (self._monitor_task and not self._monitor_task.done()):
|
|
345
455
|
return
|
|
346
456
|
|
|
347
|
-
async def
|
|
457
|
+
async def monitor():
|
|
348
458
|
while not self._closed and self.connection:
|
|
349
459
|
try:
|
|
350
460
|
# 检查连接状态
|
|
351
461
|
if self.connection.is_closed:
|
|
352
|
-
|
|
462
|
+
logger.warning(
|
|
463
|
+
f"检测到RabbitMQ连接已关闭 (主机: {self._active_host}),将尝试重连到集群其他节点")
|
|
353
464
|
await self._schedule_reconnect()
|
|
354
465
|
return
|
|
355
466
|
|
|
356
467
|
# 检查通道状态
|
|
357
468
|
if self.channel and self.channel.is_closed:
|
|
358
|
-
|
|
469
|
+
logger.warning(
|
|
470
|
+
f"检测到RabbitMQ通道已关闭 (主机: {self._active_host}),将尝试重建")
|
|
359
471
|
await self._recreate_channel()
|
|
360
472
|
continue
|
|
473
|
+
|
|
474
|
+
# 检查消费停滞
|
|
475
|
+
if self._is_consuming:
|
|
476
|
+
current_time = asyncio.get_event_loop().time()
|
|
477
|
+
if current_time - self._last_message_processed > self.consumption_stall_threshold:
|
|
478
|
+
# logger.warning(
|
|
479
|
+
# f"检测到消费停滞超过 {self.consumption_stall_threshold} 秒 (主机: {self._active_host}),将重启消费者")
|
|
480
|
+
if self._is_consuming and self.message_handler:
|
|
481
|
+
await self.stop_consuming()
|
|
482
|
+
await asyncio.sleep(1)
|
|
483
|
+
await self.start_consuming()
|
|
484
|
+
logger.info("消费者已重启以恢复消费")
|
|
361
485
|
except Exception as e:
|
|
362
|
-
|
|
486
|
+
logger.error(f"监控任务出错: {str(e)}")
|
|
363
487
|
await asyncio.sleep(1)
|
|
364
488
|
|
|
365
|
-
await asyncio.sleep(5)
|
|
489
|
+
await asyncio.sleep(5) # 每5秒检查一次
|
|
366
490
|
|
|
367
|
-
|
|
368
|
-
asyncio.create_task(monitor_task())
|
|
491
|
+
self._monitor_task = asyncio.create_task(monitor())
|
|
369
492
|
|
|
370
|
-
async def _recreate_channel(self):
|
|
371
|
-
"""
|
|
493
|
+
async def _recreate_channel(self) -> None:
|
|
494
|
+
"""重建通道并恢复绑定和消费,支持当前节点故障时的快速恢复"""
|
|
372
495
|
try:
|
|
496
|
+
# 连接已关闭时触发完整重连(尝试其他节点)
|
|
373
497
|
if not self.connection or self.connection.is_closed:
|
|
498
|
+
logger.warning("连接已关闭,触发集群重连")
|
|
499
|
+
await self._schedule_reconnect()
|
|
374
500
|
return
|
|
375
501
|
|
|
376
502
|
# 重新创建通道
|
|
377
503
|
self.channel = await self.connection.channel()
|
|
378
|
-
await self.channel.set_qos(prefetch_count=
|
|
504
|
+
await self.channel.set_qos(prefetch_count=self.prefetch_count)
|
|
505
|
+
|
|
506
|
+
# 重新获取交换机
|
|
507
|
+
self.exchange = await self.channel.get_exchange(self.exchange_name)
|
|
379
508
|
|
|
380
509
|
# 重新绑定队列和交换机
|
|
381
|
-
if self.
|
|
382
|
-
await self.
|
|
510
|
+
if self.queue_name:
|
|
511
|
+
self.queue = await self.channel.get_queue(self.queue_name)
|
|
512
|
+
if self.queue and self.exchange:
|
|
513
|
+
await self._bind_queue()
|
|
383
514
|
|
|
384
515
|
# 重新开始消费
|
|
385
516
|
if self._is_consuming and self.message_handler:
|
|
386
517
|
await self.start_consuming()
|
|
387
518
|
|
|
388
|
-
|
|
519
|
+
logger.info(f"通道已重新创建并恢复服务 (主机: {self._active_host})")
|
|
389
520
|
self._update_activity_timestamp()
|
|
390
521
|
except Exception as e:
|
|
391
|
-
|
|
522
|
+
logger.error(f"通道重建失败,触发集群重连: {str(e)} (主机: {self._active_host})")
|
|
392
523
|
await self._schedule_reconnect()
|
|
393
524
|
|
|
394
|
-
def
|
|
395
|
-
"""
|
|
525
|
+
def _start_keepalive(self) -> None:
|
|
526
|
+
"""启动连接保活任务,维护集群连接心跳"""
|
|
396
527
|
if self._closed or (self._keepalive_task and not self._keepalive_task.done()):
|
|
397
528
|
return
|
|
398
529
|
|
|
399
|
-
async def
|
|
530
|
+
async def keepalive():
|
|
400
531
|
while not self._closed and self.is_connected:
|
|
401
532
|
current_time = asyncio.get_event_loop().time()
|
|
402
533
|
# 检查是否超过指定时间无活动
|
|
403
|
-
if current_time - self.
|
|
404
|
-
|
|
534
|
+
if current_time - self._last_activity_timestamp > self.heartbeat * 1.5:
|
|
535
|
+
logger.debug(
|
|
536
|
+
f"连接 {self.heartbeat*1.5}s 无活动,执行保活检查 (主机: {self._active_host})")
|
|
405
537
|
try:
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
timeout=5
|
|
424
|
-
)
|
|
425
|
-
|
|
426
|
-
self._update_activity_timestamp()
|
|
538
|
+
if self.connection and self.connection.is_closed:
|
|
539
|
+
logger.warning("连接已关闭,触发集群重连")
|
|
540
|
+
await self._schedule_reconnect()
|
|
541
|
+
return
|
|
542
|
+
|
|
543
|
+
# 执行轻量级操作保持连接活跃
|
|
544
|
+
if self.channel:
|
|
545
|
+
await asyncio.wait_for(
|
|
546
|
+
self.channel.declare_exchange(
|
|
547
|
+
name=self.exchange_name,
|
|
548
|
+
type=self.exchange_type,
|
|
549
|
+
passive=True # 仅检查存在性
|
|
550
|
+
),
|
|
551
|
+
timeout=5
|
|
552
|
+
)
|
|
553
|
+
|
|
554
|
+
self._update_activity_timestamp()
|
|
427
555
|
except asyncio.TimeoutError:
|
|
428
|
-
|
|
556
|
+
logger.warning(
|
|
557
|
+
f"保活检查超时,触发集群重连 (主机: {self._active_host})")
|
|
429
558
|
await self._schedule_reconnect()
|
|
430
559
|
except Exception as e:
|
|
431
|
-
|
|
560
|
+
logger.warning(
|
|
561
|
+
f"保活检查失败: {str(e)},触发集群重连 (主机: {self._active_host})")
|
|
432
562
|
await self._schedule_reconnect()
|
|
433
563
|
|
|
434
|
-
await asyncio.sleep(self.
|
|
564
|
+
await asyncio.sleep(self.heartbeat / 2) # 每心跳间隔的一半检查一次
|
|
435
565
|
|
|
436
|
-
self._keepalive_task = asyncio.create_task(
|
|
566
|
+
self._keepalive_task = asyncio.create_task(keepalive())
|
|
437
567
|
|
|
438
|
-
async def _schedule_reconnect(self):
|
|
439
|
-
"""
|
|
568
|
+
async def _schedule_reconnect(self) -> None:
|
|
569
|
+
"""安排重新连接(尝试集群中的所有节点)"""
|
|
440
570
|
if self._reconnect_task and not self._reconnect_task.done():
|
|
441
571
|
return
|
|
442
572
|
|
|
443
|
-
|
|
573
|
+
logger.info(f"将在 {self.reconnection_delay} 秒后尝试重新连接到RabbitMQ集群...")
|
|
444
574
|
|
|
445
|
-
async def
|
|
575
|
+
async def reconnect():
|
|
446
576
|
try:
|
|
447
577
|
await asyncio.sleep(self.reconnection_delay)
|
|
448
578
|
if not self._closed:
|
|
449
|
-
|
|
579
|
+
# 重连时尝试所有节点
|
|
580
|
+
await self.connect(force_reconnect=True)
|
|
450
581
|
except Exception as e:
|
|
451
|
-
|
|
452
|
-
# 如果重连失败,再次安排重连
|
|
582
|
+
logger.error(f"重连任务失败: {str(e)}")
|
|
453
583
|
if not self._closed:
|
|
454
584
|
await self._schedule_reconnect()
|
|
455
585
|
|
|
456
|
-
self._reconnect_task = asyncio.create_task(
|
|
586
|
+
self._reconnect_task = asyncio.create_task(reconnect())
|
|
457
587
|
|
|
458
588
|
async def close(self) -> None:
|
|
459
|
-
"""
|
|
589
|
+
"""关闭连接并清理资源"""
|
|
460
590
|
self._closed = True
|
|
461
591
|
self._is_consuming = False
|
|
462
592
|
|
|
463
|
-
#
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
if self._consuming_task and not self._consuming_task.done():
|
|
473
|
-
self._consuming_task.cancel()
|
|
593
|
+
# 取消所有任务
|
|
594
|
+
for task in [self._keepalive_task, self._reconnect_task,
|
|
595
|
+
self._consuming_task, self._monitor_task]:
|
|
596
|
+
if task and not task.done():
|
|
597
|
+
task.cancel()
|
|
598
|
+
try:
|
|
599
|
+
await task
|
|
600
|
+
except asyncio.CancelledError:
|
|
601
|
+
pass
|
|
474
602
|
|
|
475
603
|
# 关闭连接
|
|
476
604
|
if self.connection and not self.connection.is_closed:
|
|
477
605
|
try:
|
|
478
606
|
await asyncio.wait_for(self.connection.close(), timeout=5)
|
|
479
607
|
except Exception as e:
|
|
480
|
-
|
|
608
|
+
logger.warning(f"关闭连接时出错 (主机: {self._active_host}): {str(e)}")
|
|
481
609
|
|
|
482
610
|
# 重置状态
|
|
483
611
|
self.connection = None
|
|
@@ -488,241 +616,252 @@ class RabbitMQClient:
|
|
|
488
616
|
self._queue_exists = False
|
|
489
617
|
self._queue_bound = False
|
|
490
618
|
self._consumer_tag = None
|
|
491
|
-
self.
|
|
492
|
-
self.
|
|
619
|
+
self._processing_message_ids.clear()
|
|
620
|
+
self._active_host = None
|
|
493
621
|
|
|
494
|
-
|
|
622
|
+
logger.info("RabbitMQ客户端已关闭")
|
|
623
|
+
|
|
624
|
+
async def publish(
|
|
495
625
|
self,
|
|
496
626
|
message_body: Union[str, Dict[str, Any]],
|
|
627
|
+
routing_key: Optional[str] = None,
|
|
497
628
|
content_type: str = "application/json",
|
|
498
|
-
headers: Optional[Dict[str, Any]] = None
|
|
629
|
+
headers: Optional[Dict[str, Any]] = None,
|
|
630
|
+
delivery_mode: DeliveryMode = DeliveryMode.PERSISTENT
|
|
499
631
|
) -> None:
|
|
500
|
-
"""
|
|
632
|
+
"""
|
|
633
|
+
发布消息到交换机(自动处理连接故障并重试)
|
|
634
|
+
|
|
635
|
+
:param message_body: 消息体,可以是字符串或字典
|
|
636
|
+
:param routing_key: 路由键,如未指定则使用实例的routing_key
|
|
637
|
+
:param content_type: 内容类型
|
|
638
|
+
:param headers: 消息头
|
|
639
|
+
:param delivery_mode: 投递模式,持久化或非持久化
|
|
640
|
+
"""
|
|
501
641
|
if not self.is_connected:
|
|
502
|
-
|
|
642
|
+
logger.warning("连接已关闭,尝试重连后发布消息")
|
|
503
643
|
await self.connect(force_reconnect=True)
|
|
504
644
|
|
|
505
645
|
if not self.channel or not self.exchange:
|
|
506
646
|
raise Exception("RabbitMQ连接未初始化")
|
|
507
647
|
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
648
|
+
# 处理消息体
|
|
649
|
+
if isinstance(message_body, dict):
|
|
650
|
+
message_body_str = json.dumps(message_body, ensure_ascii=False)
|
|
651
|
+
if content_type == "text/plain":
|
|
652
|
+
content_type = "application/json"
|
|
653
|
+
else:
|
|
654
|
+
message_body_str = str(message_body)
|
|
655
|
+
|
|
656
|
+
# 创建消息对象
|
|
657
|
+
message = Message(
|
|
658
|
+
body=message_body_str.encode(),
|
|
659
|
+
content_type=content_type,
|
|
660
|
+
headers=headers or {},
|
|
661
|
+
delivery_mode=delivery_mode
|
|
662
|
+
)
|
|
522
663
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
664
|
+
# 发布消息(带重试机制)
|
|
665
|
+
retry_count = 0
|
|
666
|
+
while retry_count < 2: # 最多重试2次
|
|
667
|
+
try:
|
|
668
|
+
await self.exchange.publish(
|
|
669
|
+
message,
|
|
670
|
+
routing_key=routing_key or self.routing_key or '#'
|
|
671
|
+
)
|
|
672
|
+
self._update_activity_timestamp()
|
|
673
|
+
logger.debug(
|
|
674
|
+
f"消息已发布到交换机 '{self.exchange_name}' (主机: {self._active_host})")
|
|
675
|
+
return
|
|
676
|
+
except (ConnectionClosed, ChannelInvalidStateError):
|
|
677
|
+
retry_count += 1
|
|
678
|
+
logger.warning(f"连接已关闭,尝试重连后重新发布 (重试次数: {retry_count})")
|
|
679
|
+
await self.connect(force_reconnect=True)
|
|
680
|
+
except Exception as e:
|
|
681
|
+
retry_count += 1
|
|
682
|
+
logger.error(f"消息发布失败 (重试次数: {retry_count}): {str(e)}")
|
|
683
|
+
if retry_count < 2:
|
|
684
|
+
await asyncio.sleep(1)
|
|
685
|
+
|
|
686
|
+
raise Exception(f"消息发布失败,经过{retry_count}次重试仍未成功")
|
|
538
687
|
|
|
539
688
|
def set_message_handler(
|
|
540
689
|
self,
|
|
541
690
|
handler: Callable[
|
|
542
|
-
[Union[
|
|
543
|
-
Coroutine
|
|
691
|
+
[Union[Dict[str, Any], str], AbstractIncomingMessage],
|
|
692
|
+
Coroutine[Any, Any, None]
|
|
544
693
|
]
|
|
545
694
|
) -> None:
|
|
546
|
-
"""
|
|
547
|
-
|
|
695
|
+
"""
|
|
696
|
+
设置消息处理函数
|
|
548
697
|
|
|
549
|
-
|
|
550
|
-
"""
|
|
551
|
-
|
|
552
|
-
logging.debug("已经在消费中,返回现有consumer_tag")
|
|
553
|
-
return self._consumer_tag
|
|
698
|
+
:param handler: 消息处理函数,接收解析后的消息和原始消息对象
|
|
699
|
+
"""
|
|
700
|
+
self.message_handler = handler
|
|
554
701
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
while attempt < max_attempts:
|
|
559
|
-
if not self.is_connected:
|
|
560
|
-
await self.connect()
|
|
702
|
+
async def start_consuming(self) -> ConsumerTag:
|
|
703
|
+
"""
|
|
704
|
+
开始消费消息
|
|
561
705
|
|
|
562
|
-
|
|
563
|
-
|
|
706
|
+
:return: 消费者标签
|
|
707
|
+
"""
|
|
708
|
+
if self._is_consuming:
|
|
709
|
+
logger.debug("已经在消费中,返回现有consumer_tag")
|
|
710
|
+
if self._consumer_tag:
|
|
711
|
+
return self._consumer_tag
|
|
712
|
+
raise Exception("消费已启动但未获取到consumer_tag")
|
|
564
713
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
await
|
|
714
|
+
# 确保连接和队列已准备好
|
|
715
|
+
if not self.is_connected:
|
|
716
|
+
await self.connect()
|
|
568
717
|
|
|
569
718
|
if not self.queue:
|
|
570
|
-
|
|
571
|
-
logging.warning("最后尝试重新连接并声明队列")
|
|
572
|
-
await self.connect(force_reconnect=True, declare_queue=True)
|
|
573
|
-
if not self.queue:
|
|
574
|
-
raise Exception("队列未初始化,多次尝试后仍无法创建")
|
|
719
|
+
raise Exception("队列未初始化,无法开始消费")
|
|
575
720
|
|
|
576
721
|
if not self.message_handler:
|
|
577
722
|
raise Exception("未设置消息处理函数")
|
|
578
723
|
|
|
579
724
|
self._is_consuming = True
|
|
725
|
+
logger.info(
|
|
726
|
+
f"开始消费队列: {self.actual_queue_name} (主机: {self._active_host})")
|
|
580
727
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
logging.info(f"消费者已启动,tag: {self._consumer_tag}")
|
|
588
|
-
|
|
589
|
-
# 保持消费循环
|
|
590
|
-
while self._is_consuming and self.is_connected:
|
|
591
|
-
await asyncio.sleep(1)
|
|
592
|
-
|
|
593
|
-
# 如果退出循环,取消消费(增加重试逻辑)
|
|
594
|
-
if self._consumer_tag and self.queue and not self.queue.channel.is_closed:
|
|
595
|
-
await self._safe_cancel_consumer()
|
|
728
|
+
try:
|
|
729
|
+
# 开始消费,使用aio-pika的队列消费方法
|
|
730
|
+
self._consumer_tag = await self.queue.consume(
|
|
731
|
+
self._message_wrapper,
|
|
732
|
+
no_ack=False # 手动确认消息
|
|
733
|
+
)
|
|
596
734
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
735
|
+
logger.info(
|
|
736
|
+
f"消费者已启动,队列: {self.actual_queue_name}, tag: {self._consumer_tag}, 主机: {self._active_host}")
|
|
737
|
+
return self._consumer_tag
|
|
738
|
+
except Exception as e:
|
|
739
|
+
self._is_consuming = False
|
|
740
|
+
logger.error(
|
|
741
|
+
f"启动消费失败: {str(e)} (主机: {self._active_host})", exc_info=True)
|
|
742
|
+
raise
|
|
600
743
|
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
if self._closed or not self._is_consuming:
|
|
606
|
-
break
|
|
744
|
+
async def _safe_cancel_consumer(self) -> bool:
|
|
745
|
+
"""安全取消消费者"""
|
|
746
|
+
if not self._consumer_tag or not self.queue or not self.channel:
|
|
747
|
+
return True
|
|
607
748
|
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
await asyncio.sleep(1)
|
|
616
|
-
except asyncio.CancelledError:
|
|
617
|
-
logging.info("消费任务已取消")
|
|
618
|
-
except Exception as e:
|
|
619
|
-
logging.error(f"消费任务出错: {str(e)}", exc_info=True)
|
|
620
|
-
finally:
|
|
621
|
-
self._is_consuming = False
|
|
622
|
-
self._consumer_tag = None
|
|
623
|
-
logging.info("消费任务已结束")
|
|
624
|
-
|
|
625
|
-
# 保存消费任务引用
|
|
626
|
-
self._consuming_task = asyncio.create_task(consume_task())
|
|
627
|
-
return self._consumer_tag
|
|
628
|
-
|
|
629
|
-
async def _safe_cancel_consumer(self, max_retries: int = 3) -> bool:
|
|
630
|
-
"""安全取消消费者,增加重试机制"""
|
|
631
|
-
if not self._consumer_tag or not self.queue:
|
|
749
|
+
try:
|
|
750
|
+
await asyncio.wait_for(
|
|
751
|
+
self.queue.cancel(self._consumer_tag),
|
|
752
|
+
timeout=self.rpc_timeout
|
|
753
|
+
)
|
|
754
|
+
logger.info(
|
|
755
|
+
f"消费者 {self._consumer_tag} 已取消 (主机: {self._active_host})")
|
|
632
756
|
return True
|
|
757
|
+
except (ChannelInvalidStateError, ConnectionClosed):
|
|
758
|
+
logger.warning(f"取消消费者失败:通道或连接已关闭 (主机: {self._active_host})")
|
|
759
|
+
return False
|
|
760
|
+
except asyncio.TimeoutError:
|
|
761
|
+
logger.warning(f"取消消费者超时 (主机: {self._active_host})")
|
|
762
|
+
return False
|
|
763
|
+
except Exception as e:
|
|
764
|
+
logger.error(f"取消消费者异常: {str(e)} (主机: {self._active_host})")
|
|
765
|
+
return False
|
|
633
766
|
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
timeout=self.rpc_timeout
|
|
639
|
-
)
|
|
640
|
-
logging.info(f"消费者 {self._consumer_tag} 已取消")
|
|
641
|
-
return True
|
|
642
|
-
except ChannelInvalidStateError:
|
|
643
|
-
if attempt >= max_retries - 1:
|
|
644
|
-
logging.error(f"取消消费者 {self._consumer_tag} 失败:通道已关闭")
|
|
645
|
-
return False
|
|
646
|
-
logging.warning(f"取消消费者尝试 {attempt+1} 失败,通道状态异常,重试中...")
|
|
647
|
-
await asyncio.sleep(1)
|
|
648
|
-
except asyncio.TimeoutError:
|
|
649
|
-
if attempt >= max_retries - 1:
|
|
650
|
-
logging.error(f"取消消费者 {self._consumer_tag} 超时")
|
|
651
|
-
return False
|
|
652
|
-
logging.warning(f"取消消费者尝试 {attempt+1} 超时,重试中...")
|
|
653
|
-
await asyncio.sleep(1)
|
|
654
|
-
except Exception as e:
|
|
655
|
-
logging.error(f"取消消费者异常: {str(e)}")
|
|
656
|
-
return False
|
|
657
|
-
return False
|
|
767
|
+
async def stop_consuming(self) -> None:
|
|
768
|
+
"""停止消费消息,等待正在处理的消息完成"""
|
|
769
|
+
if not self._is_consuming:
|
|
770
|
+
return
|
|
658
771
|
|
|
659
|
-
async def stop_consuming(self, timeout: float = 5.0) -> None:
|
|
660
|
-
"""停止消费消息,延长超时时间并增加重试"""
|
|
661
772
|
self._is_consuming = False
|
|
662
773
|
|
|
663
|
-
|
|
774
|
+
# 取消消费者,停止接收新消息
|
|
775
|
+
if self._consumer_tag and self.queue:
|
|
664
776
|
await self._safe_cancel_consumer()
|
|
665
777
|
|
|
666
|
-
#
|
|
667
|
-
if self.
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
778
|
+
# 等待所有正在处理的消息完成
|
|
779
|
+
if self._processing_message_ids:
|
|
780
|
+
logger.info(
|
|
781
|
+
f"等待 {len(self._processing_message_ids)} 个正在处理的消息完成... (主机: {self._active_host})"
|
|
782
|
+
)
|
|
783
|
+
# 循环等待直到所有消息处理完成
|
|
784
|
+
while self._processing_message_ids and not self._closed:
|
|
785
|
+
await asyncio.sleep(0.1)
|
|
786
|
+
|
|
787
|
+
# 清理状态
|
|
788
|
+
self._consumer_tag = None
|
|
789
|
+
self._processing_message_ids.clear()
|
|
790
|
+
|
|
791
|
+
logger.info(
|
|
792
|
+
f"已停止消费队列: {self.actual_queue_name} (主机: {self._active_host})")
|
|
675
793
|
|
|
676
794
|
async def _parse_message(self, message: AbstractIncomingMessage) -> Union[Dict[str, Any], str]:
|
|
677
|
-
"""
|
|
795
|
+
"""解析消息体"""
|
|
678
796
|
try:
|
|
679
797
|
body_str = message.body.decode('utf-8')
|
|
680
|
-
self._update_activity_timestamp()
|
|
798
|
+
self._update_activity_timestamp()
|
|
681
799
|
|
|
682
800
|
if self.auto_parse_json:
|
|
683
801
|
return json.loads(body_str)
|
|
684
802
|
return body_str
|
|
685
803
|
except json.JSONDecodeError:
|
|
686
|
-
|
|
804
|
+
logger.warning(f"消息解析JSON失败,返回原始字符串 (主机: {self._active_host})")
|
|
687
805
|
return body_str
|
|
688
806
|
except Exception as e:
|
|
689
|
-
|
|
807
|
+
logger.error(f"消息解析出错: {str(e)} (主机: {self._active_host})")
|
|
690
808
|
return message.body.decode('utf-8')
|
|
691
809
|
|
|
692
810
|
async def _message_wrapper(self, message: AbstractIncomingMessage) -> None:
|
|
811
|
+
"""消息处理包装器,处理消息接收、解析、分发和确认"""
|
|
693
812
|
if not self.message_handler or not self._is_consuming:
|
|
694
|
-
|
|
695
|
-
|
|
813
|
+
logger.warning("未设置消息处理器或已停止消费,确认消息")
|
|
814
|
+
await message.ack()
|
|
696
815
|
return
|
|
697
816
|
|
|
817
|
+
# 跟踪消息ID,防止重复处理
|
|
818
|
+
message_id = message.message_id or str(id(message))
|
|
819
|
+
if message_id in self._processing_message_ids:
|
|
820
|
+
logger.warning(
|
|
821
|
+
f"检测到重复处理的消息ID: {message_id},直接确认 (主机: {self._active_host})")
|
|
822
|
+
await message.ack()
|
|
823
|
+
return
|
|
824
|
+
|
|
825
|
+
self._processing_message_ids.add(message_id)
|
|
826
|
+
|
|
698
827
|
try:
|
|
828
|
+
logger.debug(
|
|
829
|
+
f"收到队列 {self.actual_queue_name} 的消息: {message_id} (主机: {self._active_host})")
|
|
830
|
+
|
|
831
|
+
# 解析消息
|
|
699
832
|
parsed_data = await self._parse_message(message)
|
|
700
|
-
|
|
833
|
+
|
|
834
|
+
await self.message_handler(MQMsgModel(**parsed_data), message)
|
|
835
|
+
|
|
836
|
+
# 处理成功,确认消息
|
|
701
837
|
await message.ack()
|
|
702
838
|
self._update_activity_timestamp()
|
|
839
|
+
self._update_message_processed_timestamp()
|
|
840
|
+
logger.debug(f"消息 {message_id} 处理完成并确认 (主机: {self._active_host})")
|
|
841
|
+
|
|
703
842
|
except Exception as e:
|
|
843
|
+
# 处理失败,根据重试次数决定是否重新发布
|
|
704
844
|
current_headers = message.headers or {}
|
|
705
845
|
retry_count = current_headers.get('x-retry-count', 0)
|
|
706
846
|
retry_count += 1
|
|
707
847
|
|
|
708
|
-
|
|
709
|
-
f"
|
|
848
|
+
logger.error(
|
|
849
|
+
f"消息 {message_id} 处理出错(第{retry_count}次重试): {str(e)} (主机: {self._active_host})",
|
|
710
850
|
exc_info=True
|
|
711
851
|
)
|
|
712
852
|
|
|
713
|
-
# 判断是否超过最大重试次数
|
|
714
853
|
if retry_count >= MAX_RETRY_COUNT:
|
|
715
|
-
|
|
716
|
-
f"
|
|
854
|
+
logger.error(
|
|
855
|
+
f"消息 {message_id} 已达到最大重试次数({MAX_RETRY_COUNT}次),标记为失败 (主机: {self._active_host})")
|
|
717
856
|
await message.ack()
|
|
718
857
|
self._update_activity_timestamp()
|
|
719
858
|
return
|
|
720
859
|
|
|
721
|
-
#
|
|
860
|
+
# 准备重新发布的消息
|
|
722
861
|
new_headers = current_headers.copy()
|
|
723
862
|
new_headers['x-retry-count'] = retry_count
|
|
724
863
|
|
|
725
|
-
new_message =
|
|
864
|
+
new_message = Message(
|
|
726
865
|
body=message.body,
|
|
727
866
|
content_type=message.content_type,
|
|
728
867
|
headers=new_headers,
|
|
@@ -732,14 +871,19 @@ class RabbitMQClient:
|
|
|
732
871
|
# 拒绝原消息(不重新入队)
|
|
733
872
|
await message.reject(requeue=False)
|
|
734
873
|
|
|
735
|
-
#
|
|
874
|
+
# 重新发布消息
|
|
736
875
|
if self.exchange:
|
|
737
876
|
await self.exchange.publish(
|
|
738
877
|
new_message,
|
|
739
878
|
routing_key=self.routing_key or '#'
|
|
740
879
|
)
|
|
741
880
|
self._update_activity_timestamp()
|
|
742
|
-
|
|
881
|
+
logger.info(
|
|
882
|
+
f"消息 {message_id} 已重新发布,当前重试次数: {retry_count} (主机: {self._active_host})")
|
|
883
|
+
finally:
|
|
884
|
+
# 移除消息ID跟踪
|
|
885
|
+
if message_id in self._processing_message_ids:
|
|
886
|
+
self._processing_message_ids.remove(message_id)
|
|
743
887
|
|
|
744
888
|
async def __aenter__(self):
|
|
745
889
|
await self.connect()
|