|
| 1 | +using System; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | + |
| 4 | +namespace Cyaim.WebSocketServer.Cluster.Hybrid |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Helper class for cluster configuration retrieval |
| 8 | + /// 集群配置获取辅助类 |
| 9 | + /// Supports multiple configuration sources with priority: config file -> environment variables -> defaults |
| 10 | + /// 支持多种配置来源,优先级:配置文件 -> 环境变量 -> 默认值 |
| 11 | + /// </summary> |
| 12 | + public static class ClusterConfigurationHelper |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Cluster configuration result / 集群配置结果 |
| 16 | + /// </summary> |
| 17 | + public class ClusterConfig |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Node ID / 节点 ID |
| 21 | + /// </summary> |
| 22 | + public string NodeId { get; set; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Node address / 节点地址 |
| 26 | + /// </summary> |
| 27 | + public string NodeAddress { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Node port / 节点端口 |
| 31 | + /// </summary> |
| 32 | + public int NodePort { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// WebSocket endpoint path / WebSocket 端点路径 |
| 36 | + /// </summary> |
| 37 | + public string Endpoint { get; set; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Maximum connections / 最大连接数 |
| 41 | + /// </summary> |
| 42 | + public int MaxConnections { get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Load balancing strategy / 负载均衡策略 |
| 46 | + /// </summary> |
| 47 | + public LoadBalancingStrategy LoadBalancingStrategy { get; set; } |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Get cluster configuration from multiple sources with priority |
| 52 | + /// 从多个来源获取集群配置(带优先级) |
| 53 | + /// Priority: config file -> environment variables -> defaults |
| 54 | + /// 优先级:配置文件 -> 环境变量 -> 默认值 |
| 55 | + /// </summary> |
| 56 | + /// <param name="configuration">Configuration instance / 配置实例</param> |
| 57 | + /// <param name="clusterSectionName">Cluster configuration section name, default is "Cluster" / 集群配置节名称,默认为 "Cluster"</param> |
| 58 | + /// <returns>Cluster configuration / 集群配置</returns> |
| 59 | + public static ClusterConfig GetClusterConfig(IConfiguration configuration, string clusterSectionName = "Cluster") |
| 60 | + { |
| 61 | + if (configuration == null) |
| 62 | + throw new ArgumentNullException(nameof(configuration)); |
| 63 | + |
| 64 | + var clusterConfig = configuration.GetSection(clusterSectionName); |
| 65 | + |
| 66 | + // 节点ID:配置文件 -> 环境变量 -> 机器名 |
| 67 | + // Node ID: config file -> environment variable -> machine name |
| 68 | + var nodeId = clusterConfig["NodeId"] |
| 69 | + ?? Environment.GetEnvironmentVariable("NODE_ID") |
| 70 | + ?? Environment.MachineName; |
| 71 | + |
| 72 | + // 节点地址:配置文件 -> 环境变量 -> localhost |
| 73 | + // Node address: config file -> environment variable -> localhost |
| 74 | + var nodeAddress = clusterConfig["NodeAddress"] |
| 75 | + ?? Environment.GetEnvironmentVariable("NODE_ADDRESS") |
| 76 | + ?? "localhost"; |
| 77 | + |
| 78 | + // 节点端口:配置文件 -> 环境变量 -> Kestrel配置 -> 默认5000 |
| 79 | + // Node port: config file -> environment variable -> Kestrel config -> default 5000 |
| 80 | + int nodePort; |
| 81 | + if (clusterConfig.GetValue<int?>("NodePort") is { } configPort && configPort > 0) |
| 82 | + { |
| 83 | + nodePort = configPort; |
| 84 | + } |
| 85 | + else if (Environment.GetEnvironmentVariable("NODE_PORT") != null |
| 86 | + && int.TryParse(Environment.GetEnvironmentVariable("NODE_PORT"), out var envPort) |
| 87 | + && envPort > 0) |
| 88 | + { |
| 89 | + nodePort = envPort; |
| 90 | + } |
| 91 | + else if (configuration.GetSection("Kestrel:Endpoints:Http:Url").Value != null |
| 92 | + && Uri.TryCreate(configuration.GetSection("Kestrel:Endpoints:Http:Url").Value, UriKind.Absolute, out var kestrelUri)) |
| 93 | + { |
| 94 | + nodePort = kestrelUri.Port; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + nodePort = 5000; // 默认端口 / Default port |
| 99 | + } |
| 100 | + |
| 101 | + // 端点路径:配置文件 -> 默认 /im |
| 102 | + // Endpoint path: config file -> default /im |
| 103 | + var endpoint = clusterConfig["Endpoint"] ?? "/im"; |
| 104 | + |
| 105 | + // 最大连接数:配置文件 -> 默认 10000 |
| 106 | + // Max connections: config file -> default 10000 |
| 107 | + var maxConnections = clusterConfig.GetValue<int?>("MaxConnections") ?? 10000; |
| 108 | + |
| 109 | + // 负载均衡策略:配置文件 -> 默认 LeastConnections |
| 110 | + // Load balancing strategy: config file -> default LeastConnections |
| 111 | + var loadBalancingStrategyStr = clusterConfig["LoadBalancingStrategy"] ?? "LeastConnections"; |
| 112 | + var loadBalancingStrategy = Enum.TryParse<LoadBalancingStrategy>(loadBalancingStrategyStr, out var strategy) |
| 113 | + ? strategy |
| 114 | + : LoadBalancingStrategy.LeastConnections; |
| 115 | + |
| 116 | + return new ClusterConfig |
| 117 | + { |
| 118 | + NodeId = nodeId, |
| 119 | + NodeAddress = nodeAddress, |
| 120 | + NodePort = nodePort, |
| 121 | + Endpoint = endpoint, |
| 122 | + MaxConnections = maxConnections, |
| 123 | + LoadBalancingStrategy = loadBalancingStrategy |
| 124 | + }; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
0 commit comments