sycommon-python-lib 0.1.56b9__py3-none-any.whl → 0.1.56b11__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.
- sycommon/logging/kafka_log.py +2 -1
- sycommon/rabbitmq/rabbitmq_service_client_manager.py +47 -53
- sycommon/rabbitmq/rabbitmq_service_consumer_manager.py +3 -1
- sycommon/rabbitmq/rabbitmq_service_producer_manager.py +8 -5
- sycommon/sentry/sy_sentry.py +20 -19
- sycommon/synacos/nacos_config_manager.py +2 -1
- sycommon/synacos/nacos_heartbeat_manager.py +3 -1
- sycommon/synacos/nacos_service.py +4 -3
- sycommon/synacos/nacos_service_discovery.py +4 -2
- sycommon/synacos/nacos_service_registration.py +28 -10
- sycommon/tools/env.py +62 -0
- {sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/METADATA +1 -1
- {sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/RECORD +16 -15
- {sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/WHEEL +0 -0
- {sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/entry_points.txt +0 -0
- {sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/top_level.txt +0 -0
sycommon/logging/kafka_log.py
CHANGED
|
@@ -12,6 +12,7 @@ from loguru import logger
|
|
|
12
12
|
|
|
13
13
|
from sycommon.config.Config import Config, SingletonMeta
|
|
14
14
|
from sycommon.middleware.context import current_trace_id, current_headers
|
|
15
|
+
from sycommon.tools.env import check_env_flag
|
|
15
16
|
from sycommon.tools.snowflake import Snowflake
|
|
16
17
|
|
|
17
18
|
# 配置Loguru的颜色方案
|
|
@@ -285,7 +286,7 @@ class SYLogger:
|
|
|
285
286
|
else:
|
|
286
287
|
logger.info(log_json)
|
|
287
288
|
|
|
288
|
-
if
|
|
289
|
+
if check_env_flag(['DEV-LOG']):
|
|
289
290
|
print(log_json)
|
|
290
291
|
|
|
291
292
|
@staticmethod
|
|
@@ -72,7 +72,7 @@ class RabbitMQClientManager(RabbitMQCoreService):
|
|
|
72
72
|
return False
|
|
73
73
|
|
|
74
74
|
@classmethod
|
|
75
|
-
async def _create_client(cls,
|
|
75
|
+
async def _create_client(cls, **kwargs) -> RabbitMQClient:
|
|
76
76
|
"""创建客户端实例"""
|
|
77
77
|
if cls._is_shutdown:
|
|
78
78
|
raise RuntimeError("RabbitMQService已关闭,无法创建客户端")
|
|
@@ -82,15 +82,12 @@ class RabbitMQClientManager(RabbitMQCoreService):
|
|
|
82
82
|
|
|
83
83
|
app_name = kwargs.get('app_name', cls._config.get(
|
|
84
84
|
"APP_NAME", "")) if cls._config else ""
|
|
85
|
-
|
|
86
|
-
is_sender_only = not cls._has_listeners and cls._has_senders
|
|
85
|
+
queue_name = kwargs.get('queue_name', '')
|
|
87
86
|
|
|
88
|
-
|
|
89
|
-
create_if_not_exists = False if is_sender_only else cls._has_listeners
|
|
87
|
+
create_if_not_exists = kwargs.get('create_if_not_exists', True)
|
|
90
88
|
|
|
91
89
|
processed_queue_name = queue_name
|
|
92
|
-
|
|
93
|
-
if not is_sender_only and create_if_not_exists and processed_queue_name and app_name:
|
|
90
|
+
if create_if_not_exists and processed_queue_name and app_name:
|
|
94
91
|
if not processed_queue_name.endswith(f".{app_name}"):
|
|
95
92
|
processed_queue_name = f"{processed_queue_name}.{app_name}"
|
|
96
93
|
logger.info(f"监听器队列名称自动拼接app-name: {processed_queue_name}")
|
|
@@ -98,9 +95,9 @@ class RabbitMQClientManager(RabbitMQCoreService):
|
|
|
98
95
|
logger.info(f"监听器队列已包含app-name: {processed_queue_name}")
|
|
99
96
|
|
|
100
97
|
logger.info(
|
|
101
|
-
f"创建客户端 -
|
|
102
|
-
f"
|
|
103
|
-
f"
|
|
98
|
+
f"创建客户端 - 原始队列名: {queue_name}, "
|
|
99
|
+
f"处理后队列名: {processed_queue_name}, "
|
|
100
|
+
f"是否创建队列: {create_if_not_exists}"
|
|
104
101
|
)
|
|
105
102
|
|
|
106
103
|
# 创建客户端实例
|
|
@@ -109,11 +106,10 @@ class RabbitMQClientManager(RabbitMQCoreService):
|
|
|
109
106
|
exchange_name=cls._config.get(
|
|
110
107
|
'exchange_name', "system.topic.exchange"),
|
|
111
108
|
exchange_type=kwargs.get('exchange_type', "topic"),
|
|
112
|
-
|
|
113
|
-
queue_name=None if is_sender_only else processed_queue_name,
|
|
109
|
+
queue_name=None if not create_if_not_exists else processed_queue_name,
|
|
114
110
|
routing_key=kwargs.get(
|
|
115
111
|
'routing_key',
|
|
116
|
-
f"{queue_name.split('.')[0]}.#"
|
|
112
|
+
f"{queue_name.split('.')[0]}.#"
|
|
117
113
|
),
|
|
118
114
|
durable=kwargs.get('durable', True),
|
|
119
115
|
auto_delete=kwargs.get('auto_delete', False),
|
|
@@ -130,12 +126,25 @@ class RabbitMQClientManager(RabbitMQCoreService):
|
|
|
130
126
|
@classmethod
|
|
131
127
|
async def get_client(
|
|
132
128
|
cls,
|
|
133
|
-
client_name: str = "default",
|
|
129
|
+
client_name: str = "default",
|
|
130
|
+
client_type: str = "sender", # sender(发送器)/listener(监听器),默认sender
|
|
131
|
+
**kwargs
|
|
134
132
|
) -> RabbitMQClient:
|
|
135
|
-
"""
|
|
133
|
+
"""
|
|
134
|
+
获取或创建RabbitMQ客户端
|
|
135
|
+
:param client_name: 客户端名称
|
|
136
|
+
:param client_type: 客户端类型 - sender(发送器)/listener(监听器)
|
|
137
|
+
:param kwargs: 其他参数
|
|
138
|
+
:return: RabbitMQClient实例
|
|
139
|
+
"""
|
|
136
140
|
if cls._is_shutdown:
|
|
137
141
|
raise RuntimeError("RabbitMQService已关闭,无法获取客户端")
|
|
138
142
|
|
|
143
|
+
# 校验client_type合法性
|
|
144
|
+
if client_type not in ["sender", "listener"]:
|
|
145
|
+
raise ValueError(
|
|
146
|
+
f"client_type只能是sender/listener,当前值:{client_type}")
|
|
147
|
+
|
|
139
148
|
# 等待连接池就绪
|
|
140
149
|
await cls.wait_for_pool_ready()
|
|
141
150
|
|
|
@@ -144,56 +153,41 @@ class RabbitMQClientManager(RabbitMQCoreService):
|
|
|
144
153
|
cls._init_locks[client_name] = asyncio.Lock()
|
|
145
154
|
|
|
146
155
|
async with cls._init_locks[client_name]:
|
|
147
|
-
#
|
|
156
|
+
# ===== 原有“客户端已存在”的逻辑保留 =====
|
|
148
157
|
if client_name in cls._clients:
|
|
149
158
|
client = cls._clients[client_name]
|
|
150
|
-
|
|
159
|
+
# 核心:根据client_type重置客户端的队列创建配置
|
|
160
|
+
if client_type == "sender":
|
|
161
|
+
client.create_if_not_exists = False
|
|
162
|
+
else: # listener
|
|
163
|
+
client.create_if_not_exists = True
|
|
164
|
+
# 监听器必须有队列名,从kwargs补全
|
|
165
|
+
if not client.queue_name and kwargs.get("queue_name"):
|
|
166
|
+
client.queue_name = kwargs.get("queue_name")
|
|
151
167
|
|
|
152
168
|
if await client.is_connected:
|
|
153
|
-
# 只有非纯发送器场景才检查队列初始化状态
|
|
154
|
-
if not is_sender_only and not client._queue and cls._has_listeners:
|
|
155
|
-
logger.info(f"客户端 '{client_name}' 队列未初始化,重新连接")
|
|
156
|
-
client.create_if_not_exists = True
|
|
157
|
-
await client.connect()
|
|
158
169
|
return client
|
|
159
170
|
else:
|
|
160
|
-
logger.info(f"客户端 '{client_name}'
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
171
|
+
logger.info(f"客户端 '{client_name}' 连接已断开,重新创建")
|
|
172
|
+
await cls._clean_client_resources(client)
|
|
173
|
+
|
|
174
|
+
# ===== 核心逻辑:根据client_type统一控制队列创建 =====
|
|
175
|
+
if client_type == "sender":
|
|
176
|
+
kwargs["create_if_not_exists"] = False # 禁用创建队列
|
|
177
|
+
else:
|
|
178
|
+
if not kwargs.get("queue_name"):
|
|
179
|
+
raise ValueError("监听器类型必须指定queue_name参数")
|
|
180
|
+
kwargs["create_if_not_exists"] = True
|
|
165
181
|
|
|
166
|
-
# 创建新客户端
|
|
167
|
-
initial_queue_name = kwargs.pop('queue_name', '')
|
|
168
|
-
is_sender_only = not cls._has_listeners and cls._has_senders
|
|
169
|
-
|
|
170
|
-
# 发送器特殊处理
|
|
171
|
-
if is_sender_only:
|
|
172
|
-
kwargs['create_if_not_exists'] = False
|
|
173
|
-
client = await cls._create_client(
|
|
174
|
-
"", # 空队列名
|
|
175
|
-
app_name=cls._config.get("APP_NAME", ""),
|
|
176
|
-
**kwargs
|
|
177
|
-
)
|
|
178
|
-
cls._clients[client_name] = client
|
|
179
|
-
return client
|
|
180
|
-
|
|
181
|
-
# 监听器逻辑
|
|
182
|
-
kwargs['create_if_not_exists'] = True
|
|
183
182
|
client = await cls._create_client(
|
|
184
|
-
initial_queue_name,
|
|
185
|
-
app_name=cls._config.get("APP_NAME", ""),
|
|
186
183
|
**kwargs
|
|
187
184
|
)
|
|
188
185
|
|
|
189
|
-
#
|
|
190
|
-
if
|
|
191
|
-
|
|
192
|
-
client.create_if_not_exists = True
|
|
193
|
-
await client.connect()
|
|
194
|
-
if not client._queue:
|
|
195
|
-
raise Exception(f"无法创建队列 '{initial_queue_name}'")
|
|
186
|
+
# 监听器额外验证队列创建结果
|
|
187
|
+
if client_type == "listener" and not client._queue:
|
|
188
|
+
raise RuntimeError(f"监听器队列 '{kwargs['queue_name']}' 创建失败")
|
|
196
189
|
|
|
190
|
+
# 存储客户端
|
|
197
191
|
cls._clients[client_name] = client
|
|
198
192
|
return client
|
|
199
193
|
|
|
@@ -24,7 +24,7 @@ class RabbitMQProducerManager(RabbitMQClientManager):
|
|
|
24
24
|
|
|
25
25
|
@classmethod
|
|
26
26
|
async def setup_senders(cls, senders: List[RabbitMQSendConfig], has_listeners: bool = False, **kwargs) -> None:
|
|
27
|
-
"""
|
|
27
|
+
"""设置消息发送器(适配client_type参数,确保发送器不创建队列)"""
|
|
28
28
|
if cls._is_shutdown:
|
|
29
29
|
logger.warning("服务已关闭,无法设置发送器")
|
|
30
30
|
return
|
|
@@ -42,33 +42,36 @@ class RabbitMQProducerManager(RabbitMQClientManager):
|
|
|
42
42
|
app_name = cls._config.get(
|
|
43
43
|
"APP_NAME", "") if cls._config else ""
|
|
44
44
|
|
|
45
|
-
#
|
|
45
|
+
# 处理发送器客户端名称(非队列名)
|
|
46
46
|
normalized_name = queue_name
|
|
47
47
|
if app_name and normalized_name.endswith(f".{app_name}"):
|
|
48
48
|
normalized_name = normalized_name[:-len(f".{app_name}")]
|
|
49
|
-
logger.info(f"
|
|
49
|
+
logger.info(f"发送器客户端名称移除app-name后缀: {normalized_name}")
|
|
50
50
|
|
|
51
51
|
# 检查是否已初始化
|
|
52
52
|
if normalized_name in cls._sender_client_names:
|
|
53
53
|
logger.info(f"发送客户端 '{normalized_name}' 已存在,跳过")
|
|
54
54
|
continue
|
|
55
55
|
|
|
56
|
-
#
|
|
56
|
+
# ===== 处理已有客户端重连 =====
|
|
57
57
|
if normalized_name in cls._clients:
|
|
58
58
|
client = cls._clients[normalized_name]
|
|
59
59
|
if not await client.is_connected:
|
|
60
|
+
client.queue_name = normalized_name
|
|
60
61
|
client.create_if_not_exists = False
|
|
61
62
|
await client.connect()
|
|
62
63
|
else:
|
|
63
64
|
client = await cls.get_client(
|
|
64
65
|
client_name=normalized_name,
|
|
66
|
+
client_type="sender",
|
|
65
67
|
exchange_type=sender_config.exchange_type,
|
|
66
68
|
durable=sender_config.durable,
|
|
67
69
|
auto_delete=sender_config.auto_delete,
|
|
68
70
|
auto_parse_json=sender_config.auto_parse_json,
|
|
69
71
|
queue_name=queue_name,
|
|
70
72
|
create_if_not_exists=False,
|
|
71
|
-
prefetch_count=prefetch_count,
|
|
73
|
+
prefetch_count=prefetch_count,
|
|
74
|
+
**kwargs
|
|
72
75
|
)
|
|
73
76
|
|
|
74
77
|
# 记录客户端
|
sycommon/sentry/sy_sentry.py
CHANGED
|
@@ -13,22 +13,23 @@ def sy_sentry_init():
|
|
|
13
13
|
sentry_configs = config.get('SentryConfig', [])
|
|
14
14
|
target_config = next(
|
|
15
15
|
(item for item in sentry_configs if item.get('name') == server_name), None)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
16
|
+
if target_config:
|
|
17
|
+
target_dsn = target_config.get('dsn')
|
|
18
|
+
target_enable = target_config.get('enable')
|
|
19
|
+
current_version = datetime.now().strftime("%Y-%m-%d %H:%M:%S-version")
|
|
20
|
+
if target_config and target_dsn and target_enable:
|
|
21
|
+
try:
|
|
22
|
+
sentry_sdk.init(
|
|
23
|
+
dsn=target_dsn,
|
|
24
|
+
traces_sample_rate=1.0,
|
|
25
|
+
server_name=server_name,
|
|
26
|
+
environment=environment,
|
|
27
|
+
release=current_version,
|
|
28
|
+
integrations=[
|
|
29
|
+
FastApiIntegration(),
|
|
30
|
+
# LoggingIntegration(level=logging.INFO,
|
|
31
|
+
# event_level=logging.ERROR)
|
|
32
|
+
],
|
|
33
|
+
)
|
|
34
|
+
except Exception as e:
|
|
35
|
+
SYLogger.error(f"Sentry初始化失败: {str(e)}")
|
|
@@ -2,6 +2,7 @@ import json
|
|
|
2
2
|
import threading
|
|
3
3
|
import time
|
|
4
4
|
from typing import Callable, Optional, Dict, List
|
|
5
|
+
from sycommon.synacos.nacos_client_base import NacosClientBase
|
|
5
6
|
import yaml
|
|
6
7
|
from sycommon.logging.kafka_log import SYLogger
|
|
7
8
|
|
|
@@ -9,7 +10,7 @@ from sycommon.logging.kafka_log import SYLogger
|
|
|
9
10
|
class NacosConfigManager:
|
|
10
11
|
"""Nacos配置管理类 - 负责配置读取、监听和更新"""
|
|
11
12
|
|
|
12
|
-
def __init__(self, client_base):
|
|
13
|
+
def __init__(self, client_base: NacosClientBase):
|
|
13
14
|
self.client_base = client_base
|
|
14
15
|
|
|
15
16
|
# 配置
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import threading
|
|
2
2
|
import time
|
|
3
3
|
from sycommon.logging.kafka_log import SYLogger
|
|
4
|
+
from sycommon.synacos.nacos_client_base import NacosClientBase
|
|
5
|
+
from sycommon.synacos.nacos_service_registration import NacosServiceRegistration
|
|
4
6
|
|
|
5
7
|
|
|
6
8
|
class NacosHeartbeatManager:
|
|
7
9
|
"""Nacos心跳管理类 - 负责心跳发送和监控"""
|
|
8
10
|
|
|
9
|
-
def __init__(self, client_base, registration, heartbeat_interval: int = 15):
|
|
11
|
+
def __init__(self, client_base: NacosClientBase, registration: NacosServiceRegistration, heartbeat_interval: int = 15):
|
|
10
12
|
self.client_base = client_base
|
|
11
13
|
self.registration = registration
|
|
12
14
|
|
|
@@ -13,6 +13,7 @@ from sycommon.synacos.nacos_service_registration import NacosServiceRegistration
|
|
|
13
13
|
from sycommon.synacos.nacos_heartbeat_manager import NacosHeartbeatManager
|
|
14
14
|
from sycommon.synacos.nacos_config_manager import NacosConfigManager
|
|
15
15
|
from sycommon.synacos.nacos_service_discovery import NacosServiceDiscovery
|
|
16
|
+
from sycommon.tools.env import check_env_flag, get_env_var
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
class NacosService(metaclass=SingletonMeta):
|
|
@@ -23,9 +24,9 @@ class NacosService(metaclass=SingletonMeta):
|
|
|
23
24
|
self.service_name = config['Name']
|
|
24
25
|
self.host = config['Host']
|
|
25
26
|
self.port = config['Port']
|
|
26
|
-
self.version =
|
|
27
|
-
self.enable_register_nacos =
|
|
28
|
-
'REGISTER-NACOS', 'true')
|
|
27
|
+
self.version = get_env_var('VERSION')
|
|
28
|
+
self.enable_register_nacos = check_env_flag(
|
|
29
|
+
['REGISTER-NACOS'], 'true')
|
|
29
30
|
|
|
30
31
|
# 初始化基础模块
|
|
31
32
|
self.client_base = NacosClientBase(
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import threading
|
|
2
2
|
from typing import List, Dict
|
|
3
3
|
from sycommon.logging.kafka_log import SYLogger
|
|
4
|
+
from sycommon.synacos.nacos_client_base import NacosClientBase
|
|
5
|
+
from sycommon.synacos.nacos_service_registration import NacosServiceRegistration
|
|
4
6
|
|
|
5
7
|
|
|
6
8
|
class NacosServiceDiscovery:
|
|
7
9
|
"""Nacos服务发现类 - 负责服务实例发现和轮询"""
|
|
8
10
|
|
|
9
|
-
def __init__(self, client_base):
|
|
11
|
+
def __init__(self, client_base: NacosClientBase):
|
|
10
12
|
self.client_base = client_base
|
|
11
13
|
|
|
12
14
|
# 轮询管理
|
|
@@ -91,7 +93,7 @@ class NacosServiceDiscovery:
|
|
|
91
93
|
SYLogger.error(f"nacos:服务发现失败: {service_name}: {str(e)}")
|
|
92
94
|
return []
|
|
93
95
|
|
|
94
|
-
def monitor_connection(self, registration):
|
|
96
|
+
def monitor_connection(self, registration: NacosServiceRegistration):
|
|
95
97
|
"""连接监控线程"""
|
|
96
98
|
with self._monitor_thread_lock:
|
|
97
99
|
if self.client_base._shutdown_event.is_set() or self._monitor_thread_started:
|
|
@@ -2,12 +2,13 @@ import threading
|
|
|
2
2
|
import time
|
|
3
3
|
import atexit
|
|
4
4
|
from sycommon.logging.kafka_log import SYLogger
|
|
5
|
+
from sycommon.synacos.nacos_client_base import NacosClientBase
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class NacosServiceRegistration:
|
|
8
9
|
"""Nacos服务注册类 - 负责服务注册、注销和状态验证"""
|
|
9
10
|
|
|
10
|
-
def __init__(self, client_base, service_name: str, real_ip: str, port: int, version: str):
|
|
11
|
+
def __init__(self, client_base: NacosClientBase, service_name: str, real_ip: str, port: int, version: str):
|
|
11
12
|
self.client_base = client_base
|
|
12
13
|
self.service_name = service_name
|
|
13
14
|
self.real_ip = real_ip
|
|
@@ -182,6 +183,8 @@ class NacosServiceRegistration:
|
|
|
182
183
|
retry_count = 0
|
|
183
184
|
last_error = None
|
|
184
185
|
self.registered = False
|
|
186
|
+
# 首次注册尝试标记
|
|
187
|
+
first_attempt = True
|
|
185
188
|
|
|
186
189
|
while (not self.registered) and (self.max_long_term_retries < 0 or retry_count < self.max_long_term_retries):
|
|
187
190
|
if self.registered:
|
|
@@ -193,27 +196,42 @@ class NacosServiceRegistration:
|
|
|
193
196
|
raise RuntimeError("nacos:服务注册请求失败")
|
|
194
197
|
|
|
195
198
|
SYLogger.info(
|
|
196
|
-
f"nacos
|
|
197
|
-
time.sleep(self.registration_post_delay)
|
|
199
|
+
f"nacos:服务注册请求已发送,{'首次启动信任注册,跳过阻塞验证' if first_attempt else f'延迟 {self.registration_post_delay} 秒后开始验证'}")
|
|
198
200
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
201
|
+
# 核心逻辑:首次注册跳过阻塞验证,非首次按原逻辑
|
|
202
|
+
if first_attempt:
|
|
203
|
+
# 首次注册:直接标记成功,不阻塞
|
|
204
|
+
self.registered = True
|
|
203
205
|
self.client_base._client_initialized = True
|
|
204
206
|
self.client_base._shutdown_event.set()
|
|
205
207
|
self.client_base._shutdown_event.clear()
|
|
206
208
|
self._long_term_retry_count = 0
|
|
207
209
|
|
|
208
|
-
SYLogger.info(
|
|
209
|
-
|
|
210
|
+
SYLogger.info(f"nacos:首次启动信任注册成功: {self.service_name}")
|
|
211
|
+
first_attempt = False # 标记为非首次
|
|
210
212
|
return True
|
|
211
213
|
else:
|
|
212
|
-
|
|
214
|
+
# 非首次/重试:保留原有阻塞验证逻辑
|
|
215
|
+
time.sleep(self.registration_post_delay)
|
|
216
|
+
registered = self.verify_registration()
|
|
217
|
+
self.registered = registered
|
|
218
|
+
|
|
219
|
+
if self.registered:
|
|
220
|
+
self.client_base._client_initialized = True
|
|
221
|
+
self.client_base._shutdown_event.set()
|
|
222
|
+
self.client_base._shutdown_event.clear()
|
|
223
|
+
self._long_term_retry_count = 0
|
|
224
|
+
|
|
225
|
+
SYLogger.info(
|
|
226
|
+
f"nacos:服务注册成功并通过验证: {self.service_name}")
|
|
227
|
+
return True
|
|
228
|
+
else:
|
|
229
|
+
raise RuntimeError("nacos:服务注册验证失败")
|
|
213
230
|
|
|
214
231
|
except Exception as e:
|
|
215
232
|
last_error = str(e)
|
|
216
233
|
retry_count += 1
|
|
234
|
+
first_attempt = False # 失败后标记为非首次
|
|
217
235
|
delay = min(self.register_retry_interval,
|
|
218
236
|
self.client_base.max_retry_delay)
|
|
219
237
|
|
sycommon/tools/env.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _normalize_env_key(key: str) -> str:
|
|
5
|
+
"""
|
|
6
|
+
环境变量名标准化:
|
|
7
|
+
1. 转小写
|
|
8
|
+
2. 中划线(-)和下划线(_)统一替换为下划线(_)
|
|
9
|
+
:param key: 原始环境变量名
|
|
10
|
+
:return: 标准化后的key
|
|
11
|
+
"""
|
|
12
|
+
return key.lower().replace('-', '_')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def check_env_flag(target_keys: list, default: str = 'false') -> bool:
|
|
16
|
+
"""
|
|
17
|
+
检查环境变量是否为"true"(自动兼容:大小写、中划线/下划线)
|
|
18
|
+
:param target_keys: 目标变量名列表(如 ['REGISTER-NACOS'],无需传双key)
|
|
19
|
+
:param default: 默认值(未找到变量时使用)
|
|
20
|
+
:return: 布尔值
|
|
21
|
+
"""
|
|
22
|
+
# 1. 标准化目标key(小写+统一下划线)
|
|
23
|
+
target_keys_normalized = [_normalize_env_key(k) for k in target_keys]
|
|
24
|
+
|
|
25
|
+
# 2. 遍历所有环境变量,标准化后匹配
|
|
26
|
+
for env_key, env_val in os.environ.items():
|
|
27
|
+
env_key_normalized = _normalize_env_key(env_key)
|
|
28
|
+
if env_key_normalized in target_keys_normalized:
|
|
29
|
+
# 3. 值去空格 + 转小写 判断
|
|
30
|
+
return env_val.strip().lower() == 'true'
|
|
31
|
+
|
|
32
|
+
# 4. 未找到变量时,判断默认值
|
|
33
|
+
return default.strip().lower() == 'true'
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def get_env_var(key: str, default='', case_insensitive: bool = True) -> str:
|
|
37
|
+
"""
|
|
38
|
+
获取环境变量值(自动兼容:大小写、中划线/下划线)
|
|
39
|
+
:param key: 目标环境变量名(如 'REGISTER-NACOS'/'version')
|
|
40
|
+
:param default: 无匹配时的默认值,默认空字符串
|
|
41
|
+
:param case_insensitive: 是否忽略变量名大小写(默认True,建议保持)
|
|
42
|
+
:return: 匹配到的环境变量值 / 默认值
|
|
43
|
+
"""
|
|
44
|
+
if case_insensitive:
|
|
45
|
+
# 标准化目标key(小写+统一下划线)
|
|
46
|
+
target_key_normalized = _normalize_env_key(key)
|
|
47
|
+
|
|
48
|
+
# 遍历环境变量,标准化后匹配
|
|
49
|
+
for env_key, env_val in os.environ.items():
|
|
50
|
+
env_key_normalized = _normalize_env_key(env_key)
|
|
51
|
+
if env_key_normalized == target_key_normalized:
|
|
52
|
+
return env_val
|
|
53
|
+
return default
|
|
54
|
+
else:
|
|
55
|
+
# 不忽略大小写时,仅自动兼容中划线/下划线
|
|
56
|
+
target_key_1 = key # 原始key
|
|
57
|
+
target_key_2 = key.replace(
|
|
58
|
+
'-', '_') if '-' in key else key.replace('_', '-') # 替换格式的key
|
|
59
|
+
val = os.getenv(target_key_1)
|
|
60
|
+
if val is not None:
|
|
61
|
+
return val
|
|
62
|
+
return os.getenv(target_key_2, default)
|
|
@@ -24,7 +24,7 @@ sycommon/llm/llm_logger.py,sha256=n4UeNy_-g4oHQOsw-VUzF4uo3JVRLtxaMp1FcI8FiEo,54
|
|
|
24
24
|
sycommon/llm/llm_tokens.py,sha256=-udDyFcmyzx6UAwIi6_d_wwI5kMd5w0-WcS2soVPQxg,4309
|
|
25
25
|
sycommon/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
26
26
|
sycommon/logging/async_sql_logger.py,sha256=_OY36XkUm__U3NhMgiecy-qd-nptZ_0gpE3J8lGAr58,2619
|
|
27
|
-
sycommon/logging/kafka_log.py,sha256=
|
|
27
|
+
sycommon/logging/kafka_log.py,sha256=gfOqdZe0HJ3PkIFfnNWG4DZVadxsCKJ6AmelR7_Z1Xs,9960
|
|
28
28
|
sycommon/logging/logger_levels.py,sha256=_-uQ_T1N8NkNgcAmLrMmJ83nHTDw5ZNvXFPvdk89XGY,1144
|
|
29
29
|
sycommon/logging/logger_wrapper.py,sha256=TiHsrIIHiQMzXgXK12-0KIpU9GhwQJOoHslakzmq2zc,357
|
|
30
30
|
sycommon/logging/sql_logger.py,sha256=aEU3OGnI_51Tjyuuf4FpUi9KPTceFRuKAOyQbPzGhzM,2021
|
|
@@ -50,13 +50,13 @@ sycommon/notice/uvicorn_monitor.py,sha256=VryQYcAtjijJuGDBimbVurgwxlsLaLtkNnABPD
|
|
|
50
50
|
sycommon/rabbitmq/rabbitmq_client.py,sha256=jClUm9Ao9vZ2IZg7IXIEaDbYYnV8qtmtm77vdmwpKE4,22599
|
|
51
51
|
sycommon/rabbitmq/rabbitmq_pool.py,sha256=5ruFVViSCKA7ToZmR2dZIkP0zT0ZcfsnkSGbrdkg0Tk,16141
|
|
52
52
|
sycommon/rabbitmq/rabbitmq_service.py,sha256=XSHo9HuIJ_lq-vizRh4xJVdZr_2zLqeLhot09qb0euA,2025
|
|
53
|
-
sycommon/rabbitmq/rabbitmq_service_client_manager.py,sha256=
|
|
53
|
+
sycommon/rabbitmq/rabbitmq_service_client_manager.py,sha256=MM4r8Pa2rjAmzy_NpHFb4thGznr6AYk6m__IC8IIxEM,7852
|
|
54
54
|
sycommon/rabbitmq/rabbitmq_service_connection_monitor.py,sha256=uvoMuJDzJ9i63uVRq1NKFV10CvkbGnTMyEoq2rgjQx8,3013
|
|
55
|
-
sycommon/rabbitmq/rabbitmq_service_consumer_manager.py,sha256=
|
|
55
|
+
sycommon/rabbitmq/rabbitmq_service_consumer_manager.py,sha256=489r1RKd5WrTNMAcWCxUZpt9yWGrNunZlLCCp-M_rzM,11497
|
|
56
56
|
sycommon/rabbitmq/rabbitmq_service_core.py,sha256=_9gqc7BOhrzfeYS557yIOfyCAraAKh-3ClMpKHn9ufY,4753
|
|
57
|
-
sycommon/rabbitmq/rabbitmq_service_producer_manager.py,sha256=
|
|
57
|
+
sycommon/rabbitmq/rabbitmq_service_producer_manager.py,sha256=TJrLbvsjF55P9lwr7aCm9uRIRuC3z4EZNx74KEVKBtU,10190
|
|
58
58
|
sycommon/sentry/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
59
|
-
sycommon/sentry/sy_sentry.py,sha256=
|
|
59
|
+
sycommon/sentry/sy_sentry.py,sha256=e5Fbt9Gi2gIb048z0nuKbuhp3uCAEqYH2xXbF6qrZq4,1471
|
|
60
60
|
sycommon/sse/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
61
61
|
sycommon/sse/event.py,sha256=k_rBJy23R7crtzQeetT0Q73D8o5-5p-eESGSs_BPOj0,2797
|
|
62
62
|
sycommon/sse/sse.py,sha256=__CfWEcYxOxQ-HpLor4LTZ5hLWqw9-2X7CngqbVHsfw,10128
|
|
@@ -66,19 +66,20 @@ sycommon/synacos/example2.py,sha256=adUaru3Hy482KrOA17DfaC4nwvLj8etIDS_KrWLWmCU,
|
|
|
66
66
|
sycommon/synacos/feign.py,sha256=frB3D5LeFDtT3pJLFOwFzEOrNAJKeQNGk-BzUg9T3WM,8295
|
|
67
67
|
sycommon/synacos/feign_client.py,sha256=ExO7Pd5B3eFKDjXqBRc260K1jkI49IYguLwJJaD2R-o,16166
|
|
68
68
|
sycommon/synacos/nacos_client_base.py,sha256=l5jpall6nEt0Hy07Wk-PVU0VN0BmD_Mmtldmtyvvksg,4526
|
|
69
|
-
sycommon/synacos/nacos_config_manager.py,sha256=
|
|
70
|
-
sycommon/synacos/nacos_heartbeat_manager.py,sha256=
|
|
71
|
-
sycommon/synacos/nacos_service.py,sha256=
|
|
72
|
-
sycommon/synacos/nacos_service_discovery.py,sha256=
|
|
73
|
-
sycommon/synacos/nacos_service_registration.py,sha256=
|
|
69
|
+
sycommon/synacos/nacos_config_manager.py,sha256=Cff-4gpp0aD7sQVi-nEvDO4BWqK9abEDDDJ9qXKFQgs,4399
|
|
70
|
+
sycommon/synacos/nacos_heartbeat_manager.py,sha256=G80_pOn37WdO_HpYUiAfpwMqAxW0ff0Bnw0NEuge9v0,5568
|
|
71
|
+
sycommon/synacos/nacos_service.py,sha256=BezQ1eDIYwBPE567Po_Qh1Ki_z9WmhZy1J1NiTPbdHY,6118
|
|
72
|
+
sycommon/synacos/nacos_service_discovery.py,sha256=vE72BCLBv4fQ1H9CQJ8efC6R2SPMQlAWPPTb8Ae7i2g,6024
|
|
73
|
+
sycommon/synacos/nacos_service_registration.py,sha256=plg2PmH8CWgmVnPtiIXBxtj-3BpyMdSzKr1wyWRdzh4,10968
|
|
74
74
|
sycommon/synacos/param.py,sha256=KcfSkxnXOa0TGmCjY8hdzU9pzUsA8-4PeyBKWI2-568,1765
|
|
75
75
|
sycommon/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
76
76
|
sycommon/tools/docs.py,sha256=OPj2ETheuWjXLyaXtaZPbwmJKfJaYXV5s4XMVAUNrms,1607
|
|
77
|
+
sycommon/tools/env.py,sha256=Ah-tBwG2C0_hwLGFebVQgKdWWXCjTzBuF23gCkLHYy4,2437
|
|
77
78
|
sycommon/tools/merge_headers.py,sha256=HV_i52Q-9se3SP8qh7ZGYl8bP7Fxtal4CGVkyMwEdM8,4373
|
|
78
79
|
sycommon/tools/snowflake.py,sha256=lVEe5mNCOgz5OqGQpf5_nXaGnRJlI2STX2s-ppTtanA,11947
|
|
79
80
|
sycommon/tools/timing.py,sha256=OiiE7P07lRoMzX9kzb8sZU9cDb0zNnqIlY5pWqHcnkY,2064
|
|
80
|
-
sycommon_python_lib-0.1.
|
|
81
|
-
sycommon_python_lib-0.1.
|
|
82
|
-
sycommon_python_lib-0.1.
|
|
83
|
-
sycommon_python_lib-0.1.
|
|
84
|
-
sycommon_python_lib-0.1.
|
|
81
|
+
sycommon_python_lib-0.1.56b11.dist-info/METADATA,sha256=uWU5EW2z6Tsw_iwPxxy5AkDK84Jq6UfPsNg23_j2GD8,7270
|
|
82
|
+
sycommon_python_lib-0.1.56b11.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
83
|
+
sycommon_python_lib-0.1.56b11.dist-info/entry_points.txt,sha256=q_h2nbvhhmdnsOUZEIwpuoDjaNfBF9XqppDEmQn9d_A,46
|
|
84
|
+
sycommon_python_lib-0.1.56b11.dist-info/top_level.txt,sha256=98CJ-cyM2WIKxLz-Pf0AitWLhJyrfXvyY8slwjTXNuc,17
|
|
85
|
+
sycommon_python_lib-0.1.56b11.dist-info/RECORD,,
|
|
File without changes
|
{sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{sycommon_python_lib-0.1.56b9.dist-info → sycommon_python_lib-0.1.56b11.dist-info}/top_level.txt
RENAMED
|
File without changes
|