From 22ade06114a316a656441b3d1bfff0b9b90ffd01 Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Mon, 10 Aug 2026 03:35:39 +0800 Subject: [PATCH] =?UTF-8?q?dnfs2:=20--no-confs=20=E6=A8=A1=E5=BC=8F=20+=20?= =?UTF-8?q?=E5=85=A8=E9=87=8F=20CLI=20=E5=8F=82=E6=95=B0=EF=BC=88dnfs.ini?= =?UTF-8?q?=20=E5=AD=97=E6=AE=B5=E5=85=A8=E9=83=A8=E5=8F=AF=20CLI=20?= =?UTF-8?q?=E8=A6=86=E7=9B=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 --db-host/--db-port/--db-timeout - 新增 --review-url/--review-timeout - 新增 --gateway-url/--gateway-timeout - 新增 --no-confs 开关:跳过 confs 拉取,完全靠 CLI + ENV + 默认值 - services.yaml: dnfs2 启用 --no-confs,所有配置写进 args --- core/services.yaml | 25 +++++++- dnfs2/src/dnfs2.cpp | 147 ++++++++++++++++++++++++++------------------ 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/core/services.yaml b/core/services.yaml index 68a9bb2..5a27311 100644 --- a/core/services.yaml +++ b/core/services.yaml @@ -28,7 +28,30 @@ services: # --- 文件服务 --- - name: dnfs2 cmd: bash /opt/micro_servers/dnfs2/_run.sh - args: ["--instance", "dnfs2", "--log-level", "info"] + args: + - "--no-confs" + - "--instance" + - "dnfs2" + - "--log-level" + - "info" + - "--host" + - "127.0.0.1" + - "--port" + - "8006" + - "--db-host" + - "127.0.0.1" + - "--db-port" + - "8005" + - "--db-timeout" + - "10" + - "--review-url" + - "http://127.0.0.1:8007/review" + - "--review-timeout" + - "10" + - "--gateway-url" + - "http://127.0.0.1:8443/whoami" + - "--gateway-timeout" + - "5" depends_on: [db2, ai_review] sleep_before: 0 diff --git a/dnfs2/src/dnfs2.cpp b/dnfs2/src/dnfs2.cpp index 0ef0e23..2cb5b9b 100644 --- a/dnfs2/src/dnfs2.cpp +++ b/dnfs2/src/dnfs2.cpp @@ -171,6 +171,7 @@ struct Config { std::string instance = "dnfs2"; std::string log_level = "info"; // debug|info|warn|error std::string confs_config_name = "dnfs.ini"; // --config 覆盖 + bool no_confs = false; // --no-confs: 跳过 confs 拉取 }; static Config load_config(int argc, char* argv[]) { @@ -184,17 +185,27 @@ static Config load_config(int argc, char* argv[]) { "dnfs2 — C++20 树状网络文件系统\n\n" "用法: dnfs2 [选项]\n\n" "选项:\n" - " --host 绑定地址(别名: --addr/-a),默认 127.0.0.1\n" - " --port 监听端口(别名: -p),默认 8006\n" - " --threads 工作线程数(别名: -t),默认 4\n" - " --config confs 拉取的配置名,默认 dnfs.ini\n" - " --log-level debug|info|warn|error,默认 info\n" - " --instance 实例名(注册中心用),默认 dnfs2\n" - " --help, -h 显示此帮助并退出\n\n" - "配置优先级: CLI > 环境变量 > confs > 默认值\n"); + " --host 绑定地址(别名: --addr/-a),默认 127.0.0.1\n" + " --port 监听端口(别名: -p),默认 8006\n" + " --threads 工作线程数(别名: -t),默认 4\n" + " --db-host 数据库地址,默认 127.0.0.1\n" + " --db-port 数据库端口,默认 8005\n" + " --db-timeout 数据库超时秒数,默认 10\n" + " --review-url AI 审核服务 URL,默认 http://127.0.0.1:8007/review\n" + " --review-timeout AI 审核超时秒数,默认 10\n" + " --gateway-url 网关验签 URL,默认 http://127.0.0.1:8443/whoami\n" + " --gateway-timeout 网关超时秒数,默认 5\n" + " --config confs 拉取的配置名,默认 dnfs.ini\n" + " --no-confs 跳过 confs 拉取,完全依赖 CLI + ENV + 默认值\n" + " --log-level debug|info|warn|error,默认 info\n" + " --instance 实例名(注册中心用),默认 dnfs2\n" + " --help, -h 显示此帮助并退出\n\n" + "配置优先级: CLI > 环境变量 > confs > 默认值\n" + "使用 --no-confs 时跳过 confs,优先级变为: CLI > 环境变量 > 默认值\n"); std::exit(0); } if (arg == "--config" && i + 1 < argc) cfg.confs_config_name = argv[++i]; + if (arg == "--no-confs") cfg.no_confs = true; } // 1. confs 引导地址 @@ -208,60 +219,63 @@ static Config load_config(int argc, char* argv[]) { } // 2. 从 confs 拉取配置文件(retry_on_refused=true,等待 confs 就绪) - std::string ini_text; - try { - RpcClient confs_client(cfg.confs_host, cfg.confs_port, "dnfs2", - /*timeout=*/5.0, /*retry_on_refused=*/true); - json::value data = confs_client.call("get", {{"name", cfg.confs_config_name}}); - if (data.is_object() && data.as_object().contains("content")) { - ini_text = std::string(data.as_object().at("content").as_string()); - } - } catch (const std::exception& e) { - std::fprintf(stderr, "[dnfs2] 从 confs 获取 %s 失败: %s\n", cfg.confs_config_name.c_str(), e.what()); - std::exit(1); - } - - // 3. 解析 INI - IniMap ini = parse_ini(ini_text); - - // 校验必需配置项 - static const std::pair required_keys[] = { - {"server", "host"}, - {"server", "port"}, - {"database", "host"}, - {"database", "port"}, - {"database", "timeout"}, - {"ai_review", "url"}, - {"ai_review", "timeout"}, - {"gateway", "url"}, - {"gateway", "timeout"}, - }; - for (auto& [sec, key] : required_keys) { - auto si = ini.find(sec); - if (si == ini.end() || si->second.find(key) == si->second.end()) { - std::fprintf(stderr, "[dnfs2] 缺少必需配置: [%s] %s\n", sec, key); + // --no-confs 模式下跳过,直接用 ENV + CLI + 默认值 + if (!cfg.no_confs) { + std::string ini_text; + try { + RpcClient confs_client(cfg.confs_host, cfg.confs_port, "dnfs2", + /*timeout=*/5.0, /*retry_on_refused=*/true); + json::value data = confs_client.call("get", {{"name", cfg.confs_config_name}}); + if (data.is_object() && data.as_object().contains("content")) { + ini_text = std::string(data.as_object().at("content").as_string()); + } + } catch (const std::exception& e) { + std::fprintf(stderr, "[dnfs2] 从 confs 获取 %s 失败: %s\n", cfg.confs_config_name.c_str(), e.what()); std::exit(1); } + + // 3. 解析 INI + IniMap ini = parse_ini(ini_text); + + // 校验必需配置项 + static const std::pair required_keys[] = { + {"server", "host"}, + {"server", "port"}, + {"database", "host"}, + {"database", "port"}, + {"database", "timeout"}, + {"ai_review", "url"}, + {"ai_review", "timeout"}, + {"gateway", "url"}, + {"gateway", "timeout"}, + }; + for (auto& [sec, key] : required_keys) { + auto si = ini.find(sec); + if (si == ini.end() || si->second.find(key) == si->second.end()) { + std::fprintf(stderr, "[dnfs2] 缺少必需配置: [%s] %s\n", sec, key); + std::exit(1); + } + } + + auto get = [&](const std::string& sec, const std::string& key, + const std::string& def = "") -> std::string { + auto si = ini.find(sec); + if (si == ini.end()) return def; + auto ki = si->second.find(key); + return (ki == si->second.end()) ? def : ki->second; + }; + + cfg.listen_host = get("server", "host", cfg.listen_host); + cfg.listen_port = safe_stoi(get("server", "port", std::to_string(cfg.listen_port)), "[server] port"); + cfg.db_host = get("database", "host", cfg.db_host); + cfg.db_port = safe_stoi(get("database", "port", std::to_string(cfg.db_port)), "[database] port"); + cfg.db_timeout = std::stod(get("database", "timeout", std::to_string(cfg.db_timeout))); + cfg.review_url = get("ai_review", "url", cfg.review_url); + cfg.review_timeout = std::stod(get("ai_review", "timeout", std::to_string(cfg.review_timeout))); + cfg.gateway_url = get("gateway", "url", cfg.gateway_url); + cfg.gateway_timeout = std::stod(get("gateway", "timeout", std::to_string(cfg.gateway_timeout))); } - auto get = [&](const std::string& sec, const std::string& key, - const std::string& def = "") -> std::string { - auto si = ini.find(sec); - if (si == ini.end()) return def; - auto ki = si->second.find(key); - return (ki == si->second.end()) ? def : ki->second; - }; - - cfg.listen_host = get("server", "host", cfg.listen_host); - cfg.listen_port = safe_stoi(get("server", "port", std::to_string(cfg.listen_port)), "[server] port"); - cfg.db_host = get("database", "host", cfg.db_host); - cfg.db_port = safe_stoi(get("database", "port", std::to_string(cfg.db_port)), "[database] port"); - cfg.db_timeout = std::stod(get("database", "timeout", std::to_string(cfg.db_timeout))); - cfg.review_url = get("ai_review", "url", cfg.review_url); - cfg.review_timeout = std::stod(get("ai_review", "timeout", std::to_string(cfg.review_timeout))); - cfg.gateway_url = get("gateway", "url", cfg.gateway_url); - cfg.gateway_timeout = std::stod(get("gateway", "timeout", std::to_string(cfg.gateway_timeout))); - // 4. 环境变量覆盖(Docker 兼容) if (const char* v = std::getenv("LISTEN_ADDR")) cfg.listen_host = v; if (const char* v = std::getenv("LISTEN_PORT")) cfg.listen_port = safe_stoi(v, "LISTEN_PORT"); @@ -276,6 +290,13 @@ static Config load_config(int argc, char* argv[]) { if ((arg == "--addr" || arg == "-a" || arg == "--host") && i + 1 < argc) cfg.listen_host = argv[++i]; else if ((arg == "--port" || arg == "-p") && i + 1 < argc) cfg.listen_port = safe_stoi(argv[++i], "--port"); else if ((arg == "--threads" || arg == "-t") && i + 1 < argc) cfg.threads = safe_stoi(argv[++i], "--threads"); + else if (arg == "--db-host" && i + 1 < argc) cfg.db_host = argv[++i]; + else if (arg == "--db-port" && i + 1 < argc) cfg.db_port = safe_stoi(argv[++i], "--db-port"); + else if (arg == "--db-timeout" && i + 1 < argc) cfg.db_timeout = std::stod(argv[++i]); + else if (arg == "--review-url" && i + 1 < argc) cfg.review_url = argv[++i]; + else if (arg == "--review-timeout" && i + 1 < argc) cfg.review_timeout = std::stod(argv[++i]); + else if (arg == "--gateway-url" && i + 1 < argc) cfg.gateway_url = argv[++i]; + else if (arg == "--gateway-timeout" && i + 1 < argc) cfg.gateway_timeout = std::stod(argv[++i]); else if (arg == "--log-level" && i + 1 < argc) cfg.log_level = argv[++i]; else if (arg == "--instance" && i + 1 < argc) cfg.instance = argv[++i]; } @@ -539,9 +560,13 @@ public: ); _log_queue->start(); - std::printf("[dnfs2] 配置来源: confs (%s @ %s:%d)\n", - _cfg.confs_config_name.c_str(), - _cfg.confs_host.c_str(), _cfg.confs_port); + if (cfg.no_confs) { + std::printf("[dnfs2] 配置来源: CLI + ENV + 默认值(--no-confs 模式)\n"); + } else { + std::printf("[dnfs2] 配置来源: confs (%s @ %s:%d)\n", + _cfg.confs_config_name.c_str(), + _cfg.confs_host.c_str(), _cfg.confs_port); + } std::printf("[dnfs2] instance=%s log-level=%s\n", _cfg.instance.c_str(), _cfg.log_level.c_str()); std::printf("[dnfs2] Token 验签: gateway %s\n", _cfg.gateway_url.c_str());