From f9d7619891af90d30427da48302cc722f21df7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Wed, 14 Jan 2026 23:05:40 +0800 Subject: [PATCH] a --- test_valkey_tls.py | 122 ++++++++++++++++++++++++++++++++ trading_system/requirements.txt | 3 + 2 files changed, 125 insertions(+) create mode 100644 test_valkey_tls.py diff --git a/test_valkey_tls.py b/test_valkey_tls.py new file mode 100644 index 0000000..75e9c4c --- /dev/null +++ b/test_valkey_tls.py @@ -0,0 +1,122 @@ +""" +Valkey/Redis TLS 连接测试脚本 +基于: redis-cli --tls -h asredis-hadmun.serverless.ape1.cache.amazonaws.com -p 6379 +""" +import asyncio +import redis.asyncio as aioredis +import ssl +from typing import Optional + + +async def test_valkey_tls_connection(): + """测试 Valkey TLS 连接""" + + # 连接参数 + host = "asredis-hadmun.serverless.ape1.cache.amazonaws.com" + port = 6379 + use_tls = True + + print("=" * 60) + print("🧪 Valkey TLS 连接测试") + print("=" * 60) + print(f"主机: {host}") + print(f"端口: {port}") + print(f"TLS: {'启用' if use_tls else '禁用'}") + print() + + client: Optional[aioredis.Redis] = None + + try: + # 创建 SSL 上下文(如果需要证书验证) + ssl_context = None + if use_tls: + ssl_context = ssl.create_default_context() + # 对于 AWS ElastiCache,通常需要禁用证书验证 + # 或者使用 AWS 提供的 CA 证书 + ssl_context.check_hostname = False + ssl_context.verify_mode = ssl.CERT_NONE + + # 创建 Redis 客户端 + print("🔄 正在连接...") + client = await aioredis.from_url( + f"rediss://{host}:{port}", # rediss:// 表示使用 TLS + ssl=ssl_context if use_tls else None, + decode_responses=True, # 自动解码字符串 + socket_connect_timeout=10, + socket_timeout=10, + retry_on_timeout=True, + health_check_interval=30 + ) + + # 测试连接 + print("📡 测试 PING...") + ping_result = await client.ping() + print(f"✅ PING 成功: {ping_result}") + + # 测试 SET + print("\n📝 测试 SET 操作...") + set_result = await client.set("test:valkey:tls", "Hello Valkey with TLS!") + print(f"✅ SET 结果: {set_result}") + + # 测试 GET + print("\n📖 测试 GET 操作...") + get_result = await client.get("test:valkey:tls") + print(f"✅ GET 结果: {get_result}") + + # 测试 EXISTS + print("\n🔍 测试 EXISTS 操作...") + exists_result = await client.exists("test:valkey:tls") + print(f"✅ EXISTS 结果: {exists_result}") + + # 测试缓存数据结构(交易对信息) + print("\n📊 测试交易对信息缓存...") + symbol_info = { + "quantityPrecision": 3, + "pricePrecision": 2, + "minQty": 0.001, + "stepSize": 0.001, + "minNotional": 5.0 + } + import json + await client.set("symbol_info:BTCUSDT", json.dumps(symbol_info), ex=3600) + cached_info = await client.get("symbol_info:BTCUSDT") + if cached_info: + print(f"✅ 交易对信息缓存成功: {json.loads(cached_info)}") + + # 清理测试数据 + print("\n🧹 清理测试数据...") + await client.delete("test:valkey:tls", "symbol_info:BTCUSDT") + print("✅ 清理完成") + + print("\n" + "=" * 60) + print("🎉 Valkey TLS 连接测试通过!") + print("=" * 60) + + except aioredis.ConnectionError as e: + print(f"\n❌ 连接错误: {e}") + print(" 可能原因:") + print(" - Valkey 服务未启动或不可访问") + print(" - 网络连接问题") + print(" - 安全组/防火墙配置") + print(" - TLS 配置不正确") + except aioredis.TimeoutError as e: + print(f"\n❌ 连接超时: {e}") + print(" 可能原因:") + print(" - 网络延迟过高") + print(" - 服务响应慢") + except Exception as e: + print(f"\n❌ 未知错误: {e}") + import traceback + print("\n详细错误信息:") + traceback.print_exc() + finally: + if client: + try: + await client.close() + print("\n🔒 连接已关闭") + except Exception as e: + print(f"\n⚠️ 关闭连接时出错: {e}") + + +if __name__ == "__main__": + asyncio.run(test_valkey_tls_connection()) \ No newline at end of file diff --git a/trading_system/requirements.txt b/trading_system/requirements.txt index 34a35a6..f6704e7 100644 --- a/trading_system/requirements.txt +++ b/trading_system/requirements.txt @@ -6,3 +6,6 @@ aiohttp==3.9.1 # 数据库依赖(用于从数据库读取配置) pymysql==1.1.0 python-dotenv==1.0.0 + +# Redis/Valkey 缓存依赖(可选) +redis>=5.0.0