SolrCloud简介

SolrCloud是Solr的分布式集群模式,用于实现:

  • 水平扩展:数据拆分到多个节点
  • 高可用:节点故障自动切换
  • 高性能:多副本分担查询压力
  • 统一管理:集中管理配置和索引

核心概念

概念 说明
Node 一个 Solr 服务实例
Collection 一个逻辑索引
Shard 数据分片
Replica 分片副本
Leader Shard主节点,负责写入协调
ConfigSet schema和配置集合
ZooKeeper 集群协调和状态管理

关系:

Collection
 ├── Shard1
 │    ├── Replica(Leader)
 │    └── Replica
 │
 └── Shard2
      ├── Replica(Leader)
      └── Replica

工作流程

写入

客户端
  ↓
Shard Leader
  ↓
Replica同步
  ↓
完成

查询

客户端
  ↓
多个Shard查询(并行)
  ↓
结果合并
  ↓
返回

solrcloud日常操作

启动 SolrCloud

1
bin/solr start -c

创建 Collection

1
2
3
4
bin/solr create \
-c products \
-shards 2 \
-replicationFactor 2

含义:

  • products:索引名称
  • shards 2:2个分片
  • replicationFactor 2:每个分片2个副本

添加文档

1
2
3
4
5
6
7
8
curl \
http://localhost:8983/solr/products/update?commit=true \
-H 'Content-Type: application/json' \
-d '
[
{"id":"1","name":"iphone"},
{"id":"2","name":"macbook"}
]'

查询

1
2
curl \
"http://localhost:8983/solr/products/select?q=name:iphone"

SolrCloud生产规划

常见配置:

3个Solr节点
+
3个ZooKeeper节点
+
Collection:
  3 Shard
  每Shard 2 Replica

例如:

Node1
 ├─ Shard1 Leader
 └─ Shard2 Replica

Node2
 ├─ Shard2 Leader
 └─ Shard3 Replica

Node3
 ├─ Shard3 Leader
 └─ Shard1 Replica