--- title: "分片平衡权重配置" date: 2026-01-14 lastmod: 2026-01-14 description: "控制分片级别平衡权重的配置项说明" tags: ["集群配置", "负载均衡", "分片分配"] summary: "配置项作用 # cluster.routing.allocation.balance.shard 配置项定义分片级别的平衡权重。此配置与 balance.index 配合使用,共同决定集群在分配和重新平衡分片时的权重计算策略。 配置项类型 # 该配置项为动态配置,可以在运行时通过集群设置 API 进行修改。 默认值 # 0.45 是否必需 # 可选配置项(有默认值) 取值范围 # 0.0 ~ 正无穷 配置格式 # # 默认配置 cluster.routing.allocation.balance.shard: 0.45 # 提高分片平衡权重 cluster.routing.allocation.balance.shard: 0.7 # 降低分片平衡权重 cluster.routing.allocation.balance.shard: 0.2 # 完全禁用分片平衡 cluster.routing.allocation.balance.shard: 0.0 相关配置项 # 配置项 默认值 说明 cluster.routing.allocation.balance.shard 0.45 分片平衡权重 cluster.routing.allocation.balance.index 0.55 索引平衡权重 cluster.routing.allocation.balance.threshold 1." --- ## 配置项作用 `cluster.routing.allocation.balance.shard` 配置项定义分片级别的平衡权重。此配置与 `balance.index` 配合使用,共同决定集群在分配和重新平衡分片时的权重计算策略。 ## 配置项类型 该配置项为**动态配置**,可以在运行时通过集群设置 API 进行修改。 ## 默认值 ``` 0.45 ``` ## 是否必需 **可选配置项**(有默认值) ## 取值范围 ``` 0.0 ~ 正无穷 ``` ## 配置格式 ```yaml # 默认配置 cluster.routing.allocation.balance.shard: 0.45 # 提高分片平衡权重 cluster.routing.allocation.balance.shard: 0.7 # 降低分片平衡权重 cluster.routing.allocation.balance.shard: 0.2 # 完全禁用分片平衡 cluster.routing.allocation.balance.shard: 0.0 ``` ## 相关配置项 | 配置项 | 默认值 | 说明 | |-------|-------|------| | `cluster.routing.allocation.balance.shard` | 0.45 | 分片平衡权重 | | `cluster.routing.allocation.balance.index` | 0.55 | 索引平衡权重 | | `cluster.routing.allocation.balance.threshold` | 1.0 | 平衡操作阈值 | ## 权重计算公式 ``` 总权重 = 分片权重 + 索引权重 其中: - 分片权重 = balance.shard × (节点分片数 - 平均分片数) - 索引权重 = balance.index × (节点索引分片数 - 平均索引分片数) 归一化后: θ₀ = balance.shard / (balance.shard + balance.index) θ₁ = balance.index / (balance.shard + balance.index) ``` ## 配置值影响 ### 默认配置 (shard: 0.45, index: 0.55) ``` 平衡倾向: 分片平衡: 45% 索引平衡: 55% 效果: 综合考虑分片分布和索引分布 适用于大多数场景 ``` ### 提高分片权重 (shard: 0.7, index: 0.3) ``` 平衡倾向: 分片平衡: 70% 索引平衡: 30% 效果: 更重视节点总分片数的均匀分布 可能导致单个索引的集中 ``` ### 降低分片权重 (shard: 0.2, index: 0.8) ``` 平衡倾向: 分片平衡: 20% 索引平衡: 80% 效果: 更重视索引级别的分布均匀 可能导致总分片分布不均 ``` ## 使用场景 ### 1. 默认配置(推荐) ```yaml cluster.routing.allocation.balance.shard: 0.45 cluster.routing.allocation.balance.index: 0.55 ``` ### 2. 单索引为主 ```yaml cluster.routing.allocation.balance.shard: 0.3 cluster.routing.allocation.balance.index: 0.7 ``` ### 3. 多索引环境 ```yaml cluster.routing.allocation.shard: 0.5 cluster.routing.allocation.balance.index: 0.5 ``` ## 推荐设置建议 | 场景 | shard | index | 说明 | |-----|-------|-------|------| | 默认/通用 | 0.45 | 0.55 | 标准配置 | | 单索引 | 0.3-0.4 | 0.6-0.7 | 侧重索引均匀 | | 多索引 | 0.5-0.6 | 0.4-0.5 | 侧重总分片均匀 | | 大量小索引 | 0.6 | 0.4 | 防止单索引过小 | ## 注意事项 1. **动态更新**:此配置为动态配置,可在线修改 2. **与 index 配合**:两个权重和应大于 0 3. **渐进调整**:大幅调整可能导致大量分片迁移 4. **监控效果**:修改后观察分片分布情况