SolrCloud集群 #

一、SolrCloud概述 #

1.1 什么是SolrCloud #

SolrCloud是Solr的分布式搜索模式,提供:

  • 分布式索引和搜索
  • 自动分片
  • 副本管理
  • 自动故障转移
  • 集中式配置管理

1.2 架构图 #

text
┌─────────────────────────────────────────────┐
│              ZooKeeper Ensemble             │
│         (集群配置、Leader选举)               │
└─────────────────────────────────────────────┘
                      │
        ┌─────────────┼─────────────┐
        ↓             ↓             ↓
┌───────────┐  ┌───────────┐  ┌───────────┐
│  Node 1   │  │  Node 2   │  │  Node 3   │
│ ┌───────┐ │  │ ┌───────┐ │  │ ┌───────┐ │
│ │Shard1 │ │  │ │Shard1 │ │  │ │Shard2 │ │
│ │Leader │ │  │ │Replica│ │  │ │Leader │ │
│ └───────┘ │  │ └───────┘ │  │ └───────┘ │
│ ┌───────┐ │  │ ┌───────┐ │  │ ┌───────┐ │
│ │Shard2 │ │  │ │Shard3 │ │  │ │Shard3 │ │
│ │Replica│ │  │ │Leader │ │  │ │Replica│ │
│ └───────┘ │  │ └───────┘ │  │ └───────┘ │
└───────────┘  └───────────┘  └───────────┘

1.3 核心概念 #

概念 说明
Collection 逻辑索引,由多个Shard组成
Shard 分片,Collection的一部分
Replica 副本,Shard的实例
Leader 主副本,负责写入
Node Solr节点

二、ZooKeeper #

2.1 ZooKeeper作用 #

  • 集群配置存储
  • Leader选举
  • 状态监控
  • 分布式锁

2.2 启动ZooKeeper #

内嵌ZooKeeper

bash
# 启动SolrCloud模式(内嵌ZK)
bin/solr start -c -p 8983

# 指定ZK端口
bin/solr start -c -p 8983 -z localhost:9983

外部ZooKeeper

bash
# 启动外部ZK
bin/zkServer.sh start

# Solr连接外部ZK
bin/solr start -c -p 8983 -z zk1:2181,zk2:2181,zk3:2181

2.3 上传配置到ZK #

bash
# 上传配置集
bin/solr zk upconfig -n myconfig -d server/solr/configsets/_default/conf -z localhost:9983

# 查看配置集
bin/solr zk ls /configs -z localhost:9983

三、集群部署 #

3.1 单节点开发环境 #

bash
# 启动单节点SolrCloud
bin/solr start -c -p 8983

3.2 多节点集群 #

启动节点

bash
# 节点1
bin/solr start -c -p 8983 -z localhost:9983

# 节点2
bin/solr start -c -p 8984 -z localhost:9983 -s example/cloud/node2/solr

# 节点3
bin/solr start -c -p 8985 -z localhost:9983 -s example/cloud/node3/solr

3.3 Docker部署 #

docker-compose.yml

yaml
version: '3'

services:
  zookeeper:
    image: zookeeper:3.8
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      - ZOO_MY_ID=1

  solr1:
    image: solr:9.4
    container_name: solr1
    ports:
      - "8983:8983"
    environment:
      - ZK_HOST=zookeeper:2181
    depends_on:
      - zookeeper

  solr2:
    image: solr:9.4
    container_name: solr2
    ports:
      - "8984:8983"
    environment:
      - ZK_HOST=zookeeper:2181
    depends_on:
      - zookeeper

  solr3:
    image: solr:9.4
    container_name: solr3
    ports:
      - "8985:8983"
    environment:
      - ZK_HOST=zookeeper:2181
    depends_on:
      - zookeeper
bash
docker-compose up -d

四、Collection管理 #

4.1 创建Collection #

bash
# 创建Collection
bin/solr create_collection -c mycollection -shards 3 -replicationFactor 2

# 指定配置集
bin/solr create_collection -c mycollection -shards 3 -replicationFactor 2 -n myconfig

# API方式
curl "http://localhost:8983/solr/admin/collections?action=CREATE&name=mycollection&numShards=3&replicationFactor=2"

4.2 Collection参数 #

参数 说明
name Collection名称
numShards 分片数量
replicationFactor 副本数量
collection.configName 配置集名称
maxShardsPerNode 每节点最大分片数

4.3 删除Collection #

bash
bin/solr delete -c mycollection

# API方式
curl "http://localhost:8983/solr/admin/collections?action=DELETE&name=mycollection"

4.4 查看Collection状态 #

bash
curl "http://localhost:8983/solr/admin/collections?action=CLUSTERSTATUS"

五、分片管理 #

5.1 添加分片 #

bash
curl "http://localhost:8983/solr/admin/collections?action=CREATESHARD&collection=mycollection&shard=shard4"

5.2 删除分片 #

bash
curl "http://localhost:8983/solr/admin/collections?action=DELETESHARD&collection=mycollection&shard=shard4"

5.3 分裂分片 #

bash
curl "http://localhost:8983/solr/admin/collections?action=SPLITSHARD&collection=mycollection&shard=shard1"

六、副本管理 #

6.1 添加副本 #

bash
curl "http://localhost:8983/solr/admin/collections?action=ADDREPLICA&collection=mycollection&shard=shard1&node=node1:8983"

6.2 删除副本 #

bash
curl "http://localhost:8983/solr/admin/collections?action=DELETEREPLICA&collection=mycollection&shard=shard1&replica=core_node1"

6.3 副本类型 #

类型 说明
NRT 近实时,可写可读
TLOG 事务日志,可写可读
PULL 只读副本

七、集群路由 #

7.1 路由策略 #

策略 说明
compositeId 默认,根据ID哈希
implicit 显式指定分片
routerField 根据字段路由

7.2 compositeId路由 #

bash
# 创建时指定
curl "http://localhost:8983/solr/admin/collections?action=CREATE&name=mycollection&numShards=3&router.name=compositeId"

# 索引时指定路由
curl -X POST "http://localhost:8983/solr/mycollection/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{"id": "user1!doc1", "title": "文档1"}'

7.3 implicit路由 #

bash
# 创建时指定
curl "http://localhost:8983/solr/admin/collections?action=CREATE&name=mycollection&router.name=implicit&shards=shard1,shard2,shard3"

# 索引时指定分片
curl -X POST "http://localhost:8983/solr/mycollection/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{"id": "doc1", "title": "文档1", "_route_": "shard1"}'

八、配置管理 #

8.1 上传配置 #

bash
bin/solr zk upconfig -n myconfig -d /path/to/config -z localhost:9983

8.2 下载配置 #

bash
bin/solr zk downconfig -n myconfig -d /path/to/config -z localhost:9983

8.3 更新配置 #

bash
# 修改配置后重新上传
bin/solr zk upconfig -n myconfig -d /path/to/config -z localhost:9983

# 重载Collection
curl "http://localhost:8983/solr/admin/collections?action=RELOAD&name=mycollection"

九、集群监控 #

9.1 管理界面 #

访问:http://localhost:8983/solr/#/~cloud

9.2 集群状态API #

bash
curl "http://localhost:8983/solr/admin/collections?action=CLUSTERSTATUS"

9.3 健康检查 #

bash
curl "http://localhost:8983/solr/admin/collections?action=CLUSTERHEALTH"

十、故障恢复 #

10.1 Leader选举 #

当Leader宕机时,自动从Replica中选举新Leader。

10.2 节点恢复 #

bash
# 重启节点
bin/solr restart -c -p 8983 -z localhost:9983

10.3 数据恢复 #

Replica会自动从Leader同步数据。

十一、总结 #

SolrCloud要点:

功能 说明
Collection 逻辑索引
Shard 分片
Replica 副本
Leader 主副本
ZooKeeper 协调服务

最佳实践:

  • 合理规划分片数量
  • 配置足够的副本
  • 使用外部ZooKeeper集群
  • 监控集群状态
  • 定期备份配置

下一步,让我们学习性能优化!

最后更新:2026-03-27