sycommon-python-lib 0.1.55__tar.gz → 0.1.55a0__tar.gz
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_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/PKG-INFO +9 -9
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/pyproject.toml +9 -9
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/logging/kafka_log.py +9 -21
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/context.py +0 -2
- sycommon_python_lib-0.1.55a0/src/sycommon/middleware/traceid.py +166 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/services.py +51 -54
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/feign.py +4 -9
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/feign_client.py +10 -24
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/nacos_service.py +2 -0
- sycommon_python_lib-0.1.55a0/src/sycommon/tools/snowflake.py +221 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon_python_lib.egg-info/PKG-INFO +9 -9
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon_python_lib.egg-info/SOURCES.txt +0 -5
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon_python_lib.egg-info/requires.txt +8 -8
- sycommon_python_lib-0.1.55/src/sycommon/database/async_base_db_service.py +0 -36
- sycommon_python_lib-0.1.55/src/sycommon/database/async_database_service.py +0 -96
- sycommon_python_lib-0.1.55/src/sycommon/logging/async_sql_logger.py +0 -65
- sycommon_python_lib-0.1.55/src/sycommon/logging/logger_levels.py +0 -23
- sycommon_python_lib-0.1.55/src/sycommon/middleware/traceid.py +0 -289
- sycommon_python_lib-0.1.55/src/sycommon/tools/merge_headers.py +0 -97
- sycommon_python_lib-0.1.55/src/sycommon/tools/snowflake.py +0 -300
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/README.md +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/setup.cfg +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/command/cli.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/Config.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/DatabaseConfig.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/EmbeddingConfig.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/LLMConfig.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/MQConfig.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/RerankerConfig.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/config/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/database/base_db_service.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/database/database_service.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/health/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/health/health_check.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/health/metrics.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/health/ping.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/logging/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/logging/logger_wrapper.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/logging/sql_logger.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/cors.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/docs.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/exception.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/middleware.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/monitor_memory.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/mq.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/middleware/timeout.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/base_http.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/log.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/mqlistener_config.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/mqmsg_model.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/mqsend_config.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/models/sso_user.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/rabbitmq/rabbitmq_client.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/rabbitmq/rabbitmq_pool.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/rabbitmq/rabbitmq_service.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/sse/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/sse/event.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/sse/sse.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/example.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/example2.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/synacos/param.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/tools/__init__.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/tools/docs.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/tools/timing.py +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon_python_lib.egg-info/dependency_links.txt +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon_python_lib.egg-info/entry_points.txt +0 -0
- {sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon_python_lib.egg-info/top_level.txt +0 -0
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sycommon-python-lib
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.55a0
|
|
4
4
|
Summary: Add your description here
|
|
5
5
|
Requires-Python: >=3.10
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
7
7
|
Requires-Dist: aio-pika>=9.5.8
|
|
8
8
|
Requires-Dist: aiohttp>=3.13.2
|
|
9
|
-
Requires-Dist: aiomysql>=0.3.2
|
|
10
9
|
Requires-Dist: decorator>=5.2.1
|
|
11
|
-
Requires-Dist: fastapi>=0.
|
|
12
|
-
Requires-Dist: kafka-python>=2.
|
|
10
|
+
Requires-Dist: fastapi>=0.121.2
|
|
11
|
+
Requires-Dist: kafka-python>=2.2.16
|
|
13
12
|
Requires-Dist: loguru>=0.7.3
|
|
14
13
|
Requires-Dist: mysql-connector-python>=9.5.0
|
|
15
14
|
Requires-Dist: nacos-sdk-python<3.0,>=2.0.9
|
|
16
|
-
Requires-Dist:
|
|
17
|
-
Requires-Dist: pydantic>=2.12.
|
|
15
|
+
Requires-Dist: netifaces>=0.11.0
|
|
16
|
+
Requires-Dist: pydantic>=2.12.4
|
|
18
17
|
Requires-Dist: python-dotenv>=1.2.1
|
|
19
18
|
Requires-Dist: pyyaml>=6.0.3
|
|
20
|
-
Requires-Dist: sqlalchemy
|
|
21
|
-
Requires-Dist: starlette>=0.
|
|
22
|
-
Requires-Dist:
|
|
19
|
+
Requires-Dist: sqlalchemy>=2.0.44
|
|
20
|
+
Requires-Dist: starlette>=0.49.3
|
|
21
|
+
Requires-Dist: uuid>=1.30
|
|
22
|
+
Requires-Dist: uvicorn>=0.38.0
|
|
23
23
|
|
|
24
24
|
# sycommon-python-lib
|
|
25
25
|
|
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "sycommon-python-lib"
|
|
3
|
-
version = "0.1.55"
|
|
3
|
+
version = "0.1.55-alpha"
|
|
4
4
|
description = "Add your description here"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.10"
|
|
7
7
|
dependencies = [
|
|
8
8
|
"aio-pika>=9.5.8",
|
|
9
9
|
"aiohttp>=3.13.2",
|
|
10
|
-
"aiomysql>=0.3.2",
|
|
11
10
|
"decorator>=5.2.1",
|
|
12
|
-
"fastapi>=0.
|
|
13
|
-
"kafka-python>=2.
|
|
11
|
+
"fastapi>=0.121.2",
|
|
12
|
+
"kafka-python>=2.2.16",
|
|
14
13
|
"loguru>=0.7.3",
|
|
15
14
|
"mysql-connector-python>=9.5.0",
|
|
16
15
|
"nacos-sdk-python>=2.0.9,<3.0",
|
|
17
|
-
"
|
|
18
|
-
"pydantic>=2.12.
|
|
16
|
+
"netifaces>=0.11.0",
|
|
17
|
+
"pydantic>=2.12.4",
|
|
19
18
|
"python-dotenv>=1.2.1",
|
|
20
19
|
"pyyaml>=6.0.3",
|
|
21
|
-
"sqlalchemy
|
|
22
|
-
"starlette>=0.
|
|
23
|
-
"
|
|
20
|
+
"sqlalchemy>=2.0.44",
|
|
21
|
+
"starlette>=0.49.3",
|
|
22
|
+
"uuid>=1.30",
|
|
23
|
+
"uvicorn>=0.38.0",
|
|
24
24
|
]
|
|
25
25
|
|
|
26
26
|
[tool.setuptools]
|
{sycommon_python_lib-0.1.55 → sycommon_python_lib-0.1.55a0}/src/sycommon/logging/kafka_log.py
RENAMED
|
@@ -15,7 +15,7 @@ from kafka import KafkaProducer
|
|
|
15
15
|
from loguru import logger
|
|
16
16
|
import loguru
|
|
17
17
|
from sycommon.config.Config import Config, SingletonMeta
|
|
18
|
-
from sycommon.middleware.context import current_trace_id
|
|
18
|
+
from sycommon.middleware.context import current_trace_id
|
|
19
19
|
from sycommon.tools.snowflake import Snowflake
|
|
20
20
|
|
|
21
21
|
# 配置Loguru的颜色方案
|
|
@@ -114,7 +114,7 @@ class KafkaLogger(metaclass=SingletonMeta):
|
|
|
114
114
|
trace_id = None
|
|
115
115
|
|
|
116
116
|
if not trace_id:
|
|
117
|
-
trace_id = SYLogger.get_trace_id() or Snowflake.
|
|
117
|
+
trace_id = SYLogger.get_trace_id() or Snowflake.next_id()
|
|
118
118
|
|
|
119
119
|
# 获取线程/协程信息
|
|
120
120
|
thread_info = SYLogger._get_execution_context()
|
|
@@ -173,7 +173,7 @@ class KafkaLogger(metaclass=SingletonMeta):
|
|
|
173
173
|
"className": "",
|
|
174
174
|
"sqlCost": 0,
|
|
175
175
|
"size": len(str(message)),
|
|
176
|
-
"uid": int(Snowflake.
|
|
176
|
+
"uid": int(Snowflake.next_id()) # 独立新的id
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
# 智能队列管理
|
|
@@ -212,7 +212,7 @@ class KafkaLogger(metaclass=SingletonMeta):
|
|
|
212
212
|
return
|
|
213
213
|
|
|
214
214
|
# 获取当前的trace_id
|
|
215
|
-
trace_id = SYLogger.get_trace_id() or Snowflake.
|
|
215
|
+
trace_id = SYLogger.get_trace_id() or Snowflake.next_id()
|
|
216
216
|
|
|
217
217
|
# 构建错误日志
|
|
218
218
|
error_log = {
|
|
@@ -441,18 +441,6 @@ class SYLogger:
|
|
|
441
441
|
"""重置当前的 trace_id"""
|
|
442
442
|
current_trace_id.reset(token)
|
|
443
443
|
|
|
444
|
-
@staticmethod
|
|
445
|
-
def get_headers():
|
|
446
|
-
return current_headers.get()
|
|
447
|
-
|
|
448
|
-
@staticmethod
|
|
449
|
-
def set_headers(headers: list[tuple[str, str]]):
|
|
450
|
-
return current_headers.set(headers)
|
|
451
|
-
|
|
452
|
-
@staticmethod
|
|
453
|
-
def reset_headers(token):
|
|
454
|
-
current_headers.reset(token)
|
|
455
|
-
|
|
456
444
|
@staticmethod
|
|
457
445
|
def _get_execution_context() -> str:
|
|
458
446
|
"""获取当前执行上下文的线程或协程信息,返回格式化字符串"""
|
|
@@ -471,7 +459,7 @@ class SYLogger:
|
|
|
471
459
|
|
|
472
460
|
@staticmethod
|
|
473
461
|
def _log(msg: any, level: str = "INFO"):
|
|
474
|
-
trace_id = SYLogger.get_trace_id() or Snowflake.
|
|
462
|
+
trace_id = SYLogger.get_trace_id() or Snowflake.next_id()
|
|
475
463
|
|
|
476
464
|
if isinstance(msg, dict) or isinstance(msg, list):
|
|
477
465
|
msg_str = json.dumps(msg, ensure_ascii=False)
|
|
@@ -485,7 +473,7 @@ class SYLogger:
|
|
|
485
473
|
request_log = {}
|
|
486
474
|
if level == "ERROR":
|
|
487
475
|
request_log = {
|
|
488
|
-
"trace_id": str(trace_id) if trace_id else Snowflake.
|
|
476
|
+
"trace_id": str(trace_id) if trace_id else Snowflake.next_id(),
|
|
489
477
|
"message": msg_str,
|
|
490
478
|
"traceback": traceback.format_exc(),
|
|
491
479
|
"level": level,
|
|
@@ -493,7 +481,7 @@ class SYLogger:
|
|
|
493
481
|
}
|
|
494
482
|
else:
|
|
495
483
|
request_log = {
|
|
496
|
-
"trace_id": str(trace_id) if trace_id else Snowflake.
|
|
484
|
+
"trace_id": str(trace_id) if trace_id else Snowflake.next_id(),
|
|
497
485
|
"message": msg_str,
|
|
498
486
|
"level": level,
|
|
499
487
|
"threadName": thread_info
|
|
@@ -533,7 +521,7 @@ class SYLogger:
|
|
|
533
521
|
@staticmethod
|
|
534
522
|
def exception(msg: any, *args, **kwargs):
|
|
535
523
|
"""记录异常信息,包括完整堆栈"""
|
|
536
|
-
trace_id = SYLogger.get_trace_id() or Snowflake.
|
|
524
|
+
trace_id = SYLogger.get_trace_id() or Snowflake.next_id()
|
|
537
525
|
|
|
538
526
|
if isinstance(msg, dict) or isinstance(msg, list):
|
|
539
527
|
msg_str = json.dumps(msg, ensure_ascii=False)
|
|
@@ -545,7 +533,7 @@ class SYLogger:
|
|
|
545
533
|
|
|
546
534
|
# 构建包含异常堆栈的日志
|
|
547
535
|
request_log = {
|
|
548
|
-
"trace_id": str(trace_id) if trace_id else Snowflake.
|
|
536
|
+
"trace_id": str(trace_id) if trace_id else Snowflake.next_id(),
|
|
549
537
|
"message": msg_str,
|
|
550
538
|
"level": "ERROR",
|
|
551
539
|
"threadName": thread_info
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import re
|
|
3
|
+
from typing import Dict, Any
|
|
4
|
+
from fastapi import Request, Response
|
|
5
|
+
from sycommon.logging.kafka_log import SYLogger
|
|
6
|
+
from sycommon.tools.snowflake import Snowflake
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def setup_trace_id_handler(app):
|
|
10
|
+
@app.middleware("http")
|
|
11
|
+
async def trace_id_and_log_middleware(request: Request, call_next):
|
|
12
|
+
# 生成或获取 traceId
|
|
13
|
+
trace_id = request.headers.get("x-traceId-header")
|
|
14
|
+
if not trace_id:
|
|
15
|
+
trace_id = Snowflake.next_id()
|
|
16
|
+
|
|
17
|
+
# 设置 trace_id 上下文
|
|
18
|
+
token = SYLogger.set_trace_id(trace_id)
|
|
19
|
+
|
|
20
|
+
# 获取请求参数
|
|
21
|
+
query_params = dict(request.query_params)
|
|
22
|
+
request_body: Dict[str, Any] = {}
|
|
23
|
+
files_info: Dict[str, str] = {}
|
|
24
|
+
|
|
25
|
+
# 检测请求内容类型
|
|
26
|
+
content_type = request.headers.get("content-type", "").lower()
|
|
27
|
+
|
|
28
|
+
if "application/json" in content_type and request.method in ["POST", "PUT", "PATCH"]:
|
|
29
|
+
try:
|
|
30
|
+
request_body = await request.json()
|
|
31
|
+
except Exception as e:
|
|
32
|
+
request_body = {"error": f"Failed to parse JSON: {str(e)}"}
|
|
33
|
+
|
|
34
|
+
elif "multipart/form-data" in content_type and request.method in ["POST", "PUT"]:
|
|
35
|
+
try:
|
|
36
|
+
# 从请求头中提取boundary
|
|
37
|
+
boundary = None
|
|
38
|
+
if "boundary=" in content_type:
|
|
39
|
+
boundary = content_type.split("boundary=")[1].strip()
|
|
40
|
+
boundary = boundary.encode('ascii')
|
|
41
|
+
|
|
42
|
+
if boundary:
|
|
43
|
+
# 读取原始请求体
|
|
44
|
+
body = await request.body()
|
|
45
|
+
|
|
46
|
+
# 尝试从原始请求体中提取文件名
|
|
47
|
+
parts = body.split(boundary)
|
|
48
|
+
for part in parts:
|
|
49
|
+
part_str = part.decode('utf-8', errors='ignore')
|
|
50
|
+
|
|
51
|
+
# 使用正则表达式查找文件名
|
|
52
|
+
filename_match = re.search(
|
|
53
|
+
r'filename="([^"]+)"', part_str)
|
|
54
|
+
if filename_match:
|
|
55
|
+
field_name_match = re.search(
|
|
56
|
+
r'name="([^"]+)"', part_str)
|
|
57
|
+
field_name = field_name_match.group(
|
|
58
|
+
1) if field_name_match else "unknown"
|
|
59
|
+
filename = filename_match.group(1)
|
|
60
|
+
files_info[field_name] = filename
|
|
61
|
+
except Exception as e:
|
|
62
|
+
request_body = {
|
|
63
|
+
"error": f"Failed to process form data: {str(e)}"}
|
|
64
|
+
|
|
65
|
+
# 构建请求日志信息
|
|
66
|
+
request_message = {
|
|
67
|
+
"method": request.method,
|
|
68
|
+
"url": str(request.url),
|
|
69
|
+
"query_params": query_params,
|
|
70
|
+
"request_body": request_body,
|
|
71
|
+
"uploaded_files": files_info if files_info else None
|
|
72
|
+
}
|
|
73
|
+
request_message_str = json.dumps(request_message, ensure_ascii=False)
|
|
74
|
+
SYLogger.info(request_message_str)
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
# 处理请求
|
|
78
|
+
response = await call_next(request)
|
|
79
|
+
|
|
80
|
+
content_type = response.headers.get("Content-Type", "")
|
|
81
|
+
|
|
82
|
+
# 处理 SSE 响应
|
|
83
|
+
if "text/event-stream" in content_type:
|
|
84
|
+
# 流式响应不能有Content-Length,移除它
|
|
85
|
+
if "Content-Length" in response.headers:
|
|
86
|
+
del response.headers["Content-Length"]
|
|
87
|
+
response.headers["x-traceId-header"] = trace_id
|
|
88
|
+
return response
|
|
89
|
+
|
|
90
|
+
# 处理普通响应
|
|
91
|
+
response_body = b""
|
|
92
|
+
try:
|
|
93
|
+
# 收集所有响应块
|
|
94
|
+
async for chunk in response.body_iterator:
|
|
95
|
+
response_body += chunk
|
|
96
|
+
|
|
97
|
+
content_disposition = response.headers.get(
|
|
98
|
+
"Content-Disposition", "")
|
|
99
|
+
|
|
100
|
+
# 判断是否能添加 trace_id
|
|
101
|
+
if "application/json" in content_type and not content_disposition.startswith("attachment"):
|
|
102
|
+
try:
|
|
103
|
+
data = json.loads(response_body)
|
|
104
|
+
data["traceId"] = trace_id
|
|
105
|
+
new_body = json.dumps(
|
|
106
|
+
data, ensure_ascii=False).encode()
|
|
107
|
+
|
|
108
|
+
# 创建新响应,确保Content-Length正确
|
|
109
|
+
response = Response(
|
|
110
|
+
content=new_body,
|
|
111
|
+
status_code=response.status_code,
|
|
112
|
+
headers=dict(response.headers),
|
|
113
|
+
media_type=response.media_type
|
|
114
|
+
)
|
|
115
|
+
# 显式设置正确的Content-Length
|
|
116
|
+
response.headers["Content-Length"] = str(len(new_body))
|
|
117
|
+
except json.JSONDecodeError:
|
|
118
|
+
# 如果不是JSON,恢复原始响应体并更新长度
|
|
119
|
+
response = Response(
|
|
120
|
+
content=response_body,
|
|
121
|
+
status_code=response.status_code,
|
|
122
|
+
headers=dict(response.headers),
|
|
123
|
+
media_type=response.media_type
|
|
124
|
+
)
|
|
125
|
+
response.headers["Content-Length"] = str(
|
|
126
|
+
len(response_body))
|
|
127
|
+
else:
|
|
128
|
+
# 非JSON响应,恢复原始响应体
|
|
129
|
+
response = Response(
|
|
130
|
+
content=response_body,
|
|
131
|
+
status_code=response.status_code,
|
|
132
|
+
headers=dict(response.headers),
|
|
133
|
+
media_type=response.media_type
|
|
134
|
+
)
|
|
135
|
+
response.headers["Content-Length"] = str(
|
|
136
|
+
len(response_body))
|
|
137
|
+
except StopAsyncIteration:
|
|
138
|
+
pass
|
|
139
|
+
|
|
140
|
+
# 构建响应日志信息
|
|
141
|
+
response_message = {
|
|
142
|
+
"status_code": response.status_code,
|
|
143
|
+
"response_body": response_body.decode('utf-8', errors='ignore'),
|
|
144
|
+
}
|
|
145
|
+
response_message_str = json.dumps(
|
|
146
|
+
response_message, ensure_ascii=False)
|
|
147
|
+
SYLogger.info(response_message_str)
|
|
148
|
+
|
|
149
|
+
response.headers["x-traceId-header"] = trace_id
|
|
150
|
+
|
|
151
|
+
return response
|
|
152
|
+
except Exception as e:
|
|
153
|
+
error_message = {
|
|
154
|
+
"error": str(e),
|
|
155
|
+
"query_params": query_params,
|
|
156
|
+
"request_body": request_body,
|
|
157
|
+
"uploaded_files": files_info if files_info else None
|
|
158
|
+
}
|
|
159
|
+
error_message_str = json.dumps(error_message, ensure_ascii=False)
|
|
160
|
+
SYLogger.error(error_message_str)
|
|
161
|
+
raise
|
|
162
|
+
finally:
|
|
163
|
+
# 清理上下文变量,防止泄漏
|
|
164
|
+
SYLogger.reset_trace_id(token)
|
|
165
|
+
|
|
166
|
+
return app
|
|
@@ -7,7 +7,6 @@ from fastapi import FastAPI, applications
|
|
|
7
7
|
from pydantic import BaseModel
|
|
8
8
|
from typing import Any, Callable, Dict, List, Tuple, Union, Optional, AsyncGenerator
|
|
9
9
|
from sycommon.config.Config import SingletonMeta
|
|
10
|
-
from sycommon.logging.logger_levels import setup_logger_levels
|
|
11
10
|
from sycommon.models.mqlistener_config import RabbitMQListenerConfig
|
|
12
11
|
from sycommon.models.mqsend_config import RabbitMQSendConfig
|
|
13
12
|
from sycommon.rabbitmq.rabbitmq_service import RabbitMQService
|
|
@@ -24,9 +23,6 @@ class Services(metaclass=SingletonMeta):
|
|
|
24
23
|
_user_lifespan: Optional[Callable] = None
|
|
25
24
|
_shutdown_lock: asyncio.Lock = asyncio.Lock()
|
|
26
25
|
|
|
27
|
-
# 用于存储待执行的异步数据库初始化任务
|
|
28
|
-
_pending_async_db_setup: List[Tuple[Callable, str]] = []
|
|
29
|
-
|
|
30
26
|
def __init__(self, config: dict, app: FastAPI):
|
|
31
27
|
if not Services._config:
|
|
32
28
|
Services._config = config
|
|
@@ -52,25 +48,25 @@ class Services(metaclass=SingletonMeta):
|
|
|
52
48
|
nacos_service: Optional[Callable[[dict], None]] = None,
|
|
53
49
|
logging_service: Optional[Callable[[dict], None]] = None,
|
|
54
50
|
database_service: Optional[Union[
|
|
55
|
-
Tuple[Callable, str],
|
|
56
|
-
List[Tuple[Callable, str]]
|
|
51
|
+
Tuple[Callable[[dict, str], None], str],
|
|
52
|
+
List[Tuple[Callable[[dict, str], None], str]]
|
|
57
53
|
]] = None,
|
|
58
54
|
rabbitmq_listeners: Optional[List[RabbitMQListenerConfig]] = None,
|
|
59
55
|
rabbitmq_senders: Optional[List[RabbitMQSendConfig]] = None
|
|
60
56
|
) -> FastAPI:
|
|
61
57
|
load_dotenv()
|
|
62
|
-
|
|
58
|
+
# 保存应用实例和配置
|
|
63
59
|
cls._app = app
|
|
64
60
|
cls._config = config
|
|
65
61
|
cls._user_lifespan = app.router.lifespan_context
|
|
66
|
-
|
|
62
|
+
# 设置文档
|
|
67
63
|
applications.get_swagger_ui_html = custom_swagger_ui_html
|
|
68
64
|
applications.get_redoc_html = custom_redoc_html
|
|
69
|
-
|
|
65
|
+
# 设置app.state host, port
|
|
70
66
|
if not cls._config:
|
|
71
67
|
config = yaml.safe_load(open('app.yaml', 'r', encoding='utf-8'))
|
|
72
68
|
cls._config = config
|
|
73
|
-
|
|
69
|
+
# 使用config
|
|
74
70
|
app.state.config = {
|
|
75
71
|
"host": cls._config.get('Host', '0.0.0.0'),
|
|
76
72
|
"port": cls._config.get('Port', 8080),
|
|
@@ -78,6 +74,7 @@ class Services(metaclass=SingletonMeta):
|
|
|
78
74
|
"h11_max_incomplete_event_size": cls._config.get('H11MaxIncompleteEventSize', 1024 * 1024 * 10)
|
|
79
75
|
}
|
|
80
76
|
|
|
77
|
+
# 立即配置非异步服务(在应用启动前)
|
|
81
78
|
if middleware:
|
|
82
79
|
middleware(app, config)
|
|
83
80
|
|
|
@@ -87,29 +84,8 @@ class Services(metaclass=SingletonMeta):
|
|
|
87
84
|
if logging_service:
|
|
88
85
|
logging_service(config)
|
|
89
86
|
|
|
90
|
-
# ========== 处理数据库服务 ==========
|
|
91
|
-
# 清空之前的待执行列表(防止热重载时重复)
|
|
92
|
-
cls._pending_async_db_setup = []
|
|
93
|
-
|
|
94
87
|
if database_service:
|
|
95
|
-
|
|
96
|
-
items = [database_service] if isinstance(
|
|
97
|
-
database_service, tuple) else database_service
|
|
98
|
-
for item in items:
|
|
99
|
-
db_setup_func, db_name = item
|
|
100
|
-
if asyncio.iscoroutinefunction(db_setup_func):
|
|
101
|
-
# 如果是异步函数,加入待执行列表
|
|
102
|
-
logging.info(f"检测到异步数据库服务: {db_name},将在应用启动时初始化")
|
|
103
|
-
cls._pending_async_db_setup.append(item)
|
|
104
|
-
else:
|
|
105
|
-
# 如果是同步函数,立即执行
|
|
106
|
-
logging.info(f"执行同步数据库服务: {db_name}")
|
|
107
|
-
try:
|
|
108
|
-
db_setup_func(config, db_name)
|
|
109
|
-
except Exception as e:
|
|
110
|
-
logging.error(
|
|
111
|
-
f"同步数据库服务 {db_name} 初始化失败: {e}", exc_info=True)
|
|
112
|
-
raise
|
|
88
|
+
cls._setup_database_static(database_service, config)
|
|
113
89
|
|
|
114
90
|
# 创建组合生命周期管理器
|
|
115
91
|
@asynccontextmanager
|
|
@@ -117,25 +93,14 @@ class Services(metaclass=SingletonMeta):
|
|
|
117
93
|
# 1. 执行Services自身的初始化
|
|
118
94
|
instance = cls(config, app)
|
|
119
95
|
|
|
120
|
-
#
|
|
121
|
-
if cls._pending_async_db_setup:
|
|
122
|
-
logging.info("开始执行异步数据库初始化...")
|
|
123
|
-
for db_setup_func, db_name in cls._pending_async_db_setup:
|
|
124
|
-
try:
|
|
125
|
-
await db_setup_func(config, db_name)
|
|
126
|
-
logging.info(f"异步数据库服务 {db_name} 初始化成功")
|
|
127
|
-
except Exception as e:
|
|
128
|
-
logging.error(
|
|
129
|
-
f"异步数据库服务 {db_name} 初始化失败: {e}", exc_info=True)
|
|
130
|
-
raise
|
|
131
|
-
|
|
132
|
-
# ========== 初始化 MQ ==========
|
|
96
|
+
# 明确判断是否有有效的监听器/发送器配置
|
|
133
97
|
has_valid_listeners = bool(
|
|
134
98
|
rabbitmq_listeners and len(rabbitmq_listeners) > 0)
|
|
135
99
|
has_valid_senders = bool(
|
|
136
100
|
rabbitmq_senders and len(rabbitmq_senders) > 0)
|
|
137
101
|
|
|
138
102
|
try:
|
|
103
|
+
# 只有存在监听器或发送器时才初始化RabbitMQService
|
|
139
104
|
if has_valid_listeners or has_valid_senders:
|
|
140
105
|
await instance._setup_mq_async(
|
|
141
106
|
rabbitmq_listeners=rabbitmq_listeners if has_valid_listeners else None,
|
|
@@ -154,18 +119,28 @@ class Services(metaclass=SingletonMeta):
|
|
|
154
119
|
# 2. 执行用户定义的生命周期
|
|
155
120
|
if cls._user_lifespan:
|
|
156
121
|
async with cls._user_lifespan(app):
|
|
157
|
-
yield
|
|
122
|
+
yield # 应用运行阶段
|
|
158
123
|
else:
|
|
159
|
-
yield
|
|
124
|
+
yield # 没有用户生命周期时直接 yield
|
|
160
125
|
|
|
161
126
|
# 3. 执行Services的关闭逻辑
|
|
162
127
|
await cls.shutdown()
|
|
163
128
|
logging.info("Services已关闭")
|
|
164
129
|
|
|
130
|
+
# 设置组合生命周期
|
|
165
131
|
app.router.lifespan_context = combined_lifespan
|
|
132
|
+
|
|
166
133
|
return app
|
|
167
134
|
|
|
168
|
-
|
|
135
|
+
@staticmethod
|
|
136
|
+
def _setup_database_static(database_service, config):
|
|
137
|
+
"""静态方法:设置数据库服务"""
|
|
138
|
+
if isinstance(database_service, tuple):
|
|
139
|
+
db_setup, db_name = database_service
|
|
140
|
+
db_setup(config, db_name)
|
|
141
|
+
elif isinstance(database_service, list):
|
|
142
|
+
for db_setup, db_name in database_service:
|
|
143
|
+
db_setup(config, db_name)
|
|
169
144
|
|
|
170
145
|
async def _setup_mq_async(
|
|
171
146
|
self,
|
|
@@ -174,13 +149,16 @@ class Services(metaclass=SingletonMeta):
|
|
|
174
149
|
has_listeners: bool = False,
|
|
175
150
|
has_senders: bool = False,
|
|
176
151
|
):
|
|
177
|
-
"""异步设置MQ
|
|
152
|
+
"""异步设置MQ相关服务(适配单通道RabbitMQService)"""
|
|
153
|
+
# ========== 只有需要使用MQ时才初始化 ==========
|
|
178
154
|
if not (has_listeners or has_senders):
|
|
179
155
|
logging.info("无RabbitMQ监听器/发送器配置,跳过RabbitMQService初始化")
|
|
180
156
|
return
|
|
181
157
|
|
|
158
|
+
# 仅当有监听器或发送器时,才执行RabbitMQService初始化
|
|
182
159
|
RabbitMQService.init(self._config, has_listeners, has_senders)
|
|
183
160
|
|
|
161
|
+
# 优化:等待连接池“存在且初始化完成”(避免提前执行后续逻辑)
|
|
184
162
|
start_time = asyncio.get_event_loop().time()
|
|
185
163
|
while not (RabbitMQService._connection_pool and RabbitMQService._connection_pool._initialized) and not RabbitMQService._is_shutdown:
|
|
186
164
|
if asyncio.get_event_loop().time() - start_time > 30:
|
|
@@ -188,7 +166,10 @@ class Services(metaclass=SingletonMeta):
|
|
|
188
166
|
logging.info("等待RabbitMQ连接池初始化...")
|
|
189
167
|
await asyncio.sleep(0.5)
|
|
190
168
|
|
|
169
|
+
# ========== 保留原有严格的发送器/监听器初始化判断 ==========
|
|
170
|
+
# 只有配置了发送器才执行发送器初始化
|
|
191
171
|
if has_senders and rabbitmq_senders:
|
|
172
|
+
# 判断是否有监听器,如果有遍历监听器列表,队列名一样将prefetch_count属性设置到发送器对象中
|
|
192
173
|
if has_listeners and rabbitmq_listeners:
|
|
193
174
|
for sender in rabbitmq_senders:
|
|
194
175
|
for listener in rabbitmq_listeners:
|
|
@@ -196,25 +177,31 @@ class Services(metaclass=SingletonMeta):
|
|
|
196
177
|
sender.prefetch_count = listener.prefetch_count
|
|
197
178
|
await self._setup_senders_async(rabbitmq_senders, has_listeners)
|
|
198
179
|
|
|
180
|
+
# 只有配置了监听器才执行监听器初始化
|
|
199
181
|
if has_listeners and rabbitmq_listeners:
|
|
200
182
|
await self._setup_listeners_async(rabbitmq_listeners, has_senders)
|
|
201
183
|
|
|
184
|
+
# 验证初始化结果
|
|
202
185
|
if has_listeners:
|
|
186
|
+
# 异步获取客户端数量(适配新的RabbitMQService)
|
|
203
187
|
listener_count = len(RabbitMQService._consumer_tasks)
|
|
204
188
|
logging.info(f"监听器初始化完成,共启动 {listener_count} 个消费者")
|
|
205
189
|
if listener_count == 0:
|
|
206
190
|
logging.warning("未成功初始化任何监听器,请检查配置或MQ服务状态")
|
|
207
191
|
|
|
208
192
|
async def _setup_senders_async(self, rabbitmq_senders, has_listeners: bool):
|
|
209
|
-
"""
|
|
193
|
+
"""设置发送器(适配新的RabbitMQService异步方法)"""
|
|
210
194
|
Services._registered_senders = [
|
|
211
195
|
sender.queue_name for sender in rabbitmq_senders]
|
|
196
|
+
|
|
197
|
+
# 将是否有监听器的信息传递给RabbitMQService(异步调用)
|
|
212
198
|
await RabbitMQService.setup_senders(rabbitmq_senders, has_listeners)
|
|
199
|
+
# 更新已注册的发送器(从RabbitMQService获取实际注册的名称)
|
|
213
200
|
Services._registered_senders = RabbitMQService._sender_client_names
|
|
214
201
|
logging.info(f"已注册的RabbitMQ发送器: {Services._registered_senders}")
|
|
215
202
|
|
|
216
203
|
async def _setup_listeners_async(self, rabbitmq_listeners, has_senders: bool):
|
|
217
|
-
"""
|
|
204
|
+
"""设置监听器(适配新的RabbitMQService异步方法)"""
|
|
218
205
|
await RabbitMQService.setup_listeners(rabbitmq_listeners, has_senders)
|
|
219
206
|
|
|
220
207
|
@classmethod
|
|
@@ -225,7 +212,7 @@ class Services(metaclass=SingletonMeta):
|
|
|
225
212
|
max_retries: int = 3,
|
|
226
213
|
retry_delay: float = 1.0, **kwargs
|
|
227
214
|
) -> None:
|
|
228
|
-
"""
|
|
215
|
+
"""发送消息,添加重试机制(适配单通道RabbitMQService)"""
|
|
229
216
|
if not cls._initialized or not cls._loop:
|
|
230
217
|
logging.error("Services not properly initialized!")
|
|
231
218
|
raise ValueError("服务未正确初始化")
|
|
@@ -236,15 +223,18 @@ class Services(metaclass=SingletonMeta):
|
|
|
236
223
|
|
|
237
224
|
for attempt in range(max_retries):
|
|
238
225
|
try:
|
|
226
|
+
# 验证发送器是否注册
|
|
239
227
|
if queue_name not in cls._registered_senders:
|
|
240
228
|
cls._registered_senders = RabbitMQService._sender_client_names
|
|
241
229
|
if queue_name not in cls._registered_senders:
|
|
242
230
|
raise ValueError(f"发送器 {queue_name} 未注册")
|
|
243
231
|
|
|
232
|
+
# 获取发送器(适配新的异步get_sender方法)
|
|
244
233
|
sender = await RabbitMQService.get_sender(queue_name)
|
|
245
234
|
if not sender:
|
|
246
235
|
raise ValueError(f"发送器 '{queue_name}' 不存在或连接无效")
|
|
247
236
|
|
|
237
|
+
# 发送消息(调用RabbitMQService的异步send_message)
|
|
248
238
|
await RabbitMQService.send_message(data, queue_name, **kwargs)
|
|
249
239
|
logging.info(f"消息发送成功(尝试 {attempt+1}/{max_retries})")
|
|
250
240
|
return
|
|
@@ -254,18 +244,25 @@ class Services(metaclass=SingletonMeta):
|
|
|
254
244
|
logging.error(
|
|
255
245
|
f"消息发送失败(已尝试 {max_retries} 次): {str(e)}", exc_info=True)
|
|
256
246
|
raise
|
|
247
|
+
|
|
257
248
|
logging.warning(
|
|
258
|
-
f"消息发送失败(尝试 {attempt+1}/{max_retries}): {str(e)},
|
|
249
|
+
f"消息发送失败(尝试 {attempt+1}/{max_retries}): {str(e)},"
|
|
250
|
+
f"{retry_delay}秒后重试..."
|
|
251
|
+
)
|
|
259
252
|
await asyncio.sleep(retry_delay)
|
|
260
253
|
|
|
261
254
|
@classmethod
|
|
262
255
|
async def shutdown(cls):
|
|
263
|
-
"""
|
|
256
|
+
"""关闭所有服务(适配单通道RabbitMQService关闭逻辑)"""
|
|
264
257
|
async with cls._shutdown_lock:
|
|
265
258
|
if RabbitMQService._is_shutdown:
|
|
266
259
|
logging.info("RabbitMQService已关闭,无需重复操作")
|
|
267
260
|
return
|
|
261
|
+
|
|
262
|
+
# 关闭RabbitMQ服务(异步调用,内部会关闭所有客户端+消费任务)
|
|
268
263
|
await RabbitMQService.shutdown()
|
|
264
|
+
|
|
265
|
+
# 清理全局状态
|
|
269
266
|
cls._initialized = False
|
|
270
267
|
cls._registered_senders.clear()
|
|
271
268
|
logging.info("所有服务已关闭")
|
|
@@ -2,7 +2,6 @@ import io
|
|
|
2
2
|
import os
|
|
3
3
|
import time
|
|
4
4
|
|
|
5
|
-
from sycommon.tools.merge_headers import merge_headers
|
|
6
5
|
from sycommon.tools.snowflake import Snowflake
|
|
7
6
|
|
|
8
7
|
import aiohttp
|
|
@@ -25,18 +24,14 @@ async def feign(service_name, api_path, method='GET', params=None, headers=None,
|
|
|
25
24
|
try:
|
|
26
25
|
# 初始化headers,确保是可修改的字典
|
|
27
26
|
headers = headers.copy() if headers else {}
|
|
28
|
-
headers = merge_headers(SYLogger.get_headers(), headers)
|
|
29
27
|
if "x-traceId-header" not in headers:
|
|
30
|
-
headers["x-traceId-header"] = SYLogger.get_trace_id() or Snowflake.
|
|
28
|
+
headers["x-traceId-header"] = SYLogger.get_trace_id() or Snowflake.next_id()
|
|
31
29
|
|
|
32
30
|
# 处理JSON请求的Content-Type
|
|
33
31
|
is_json_request = method.upper() in ["POST", "PUT", "PATCH"] and not (
|
|
34
32
|
files or form_data or file_path)
|
|
35
|
-
if is_json_request:
|
|
36
|
-
|
|
37
|
-
headers_lower = {k.lower(): v for k, v in headers.items()}
|
|
38
|
-
if "content-type" not in headers_lower:
|
|
39
|
-
headers["Content-Type"] = "application/json"
|
|
33
|
+
if is_json_request and "Content-Type" not in headers:
|
|
34
|
+
headers["Content-Type"] = "application/json"
|
|
40
35
|
|
|
41
36
|
nacos_service = NacosService(None)
|
|
42
37
|
version = headers.get('s-y-version')
|
|
@@ -52,7 +47,7 @@ async def feign(service_name, api_path, method='GET', params=None, headers=None,
|
|
|
52
47
|
instance = instances[int(time.time()) % len(instances)]
|
|
53
48
|
|
|
54
49
|
SYLogger.info(f"nacos:开始调用服务: {service_name}")
|
|
55
|
-
|
|
50
|
+
SYLogger.info(f"nacos:请求头: {headers}")
|
|
56
51
|
|
|
57
52
|
ip = instance.get('ip')
|
|
58
53
|
port = instance.get('port')
|