首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Elasticsearch 配置文件 elasticsearch.yml 核心參數深度解析

     編輯:Java 綜合教學

Elasticsearch 核心配置文件結構

Elasticsearch 的配置目錄 config/ 下包含兩個核心文件:

  • elasticsearch.yml:集群、節點、網絡、索引、線程池等核心參數
  • logging.yml:基於 Log4j 的日誌配置,遵循標準 Log4j 語法

本文聚焦 elasticsearch.yml 的生產環境關鍵參數調優。

集群與節點標識

cluster.name: "production-cluster"  # 集群唯一標識,防止誤加入其他集群
node.name: "es-master-01"           # 節點名稱,建議使用主機名+角色
node.master: true                   # 是否具備 Master 選舉資格
node.data: true                     # 是否存儲數據(純 Master 節點設 false)

索引分片與副本策略

index.number_of_shards: 5      # 默認主分片數,建議根據數據量規劃(如 1GB/分片)
index.number_of_replicas: 1    # 副本數,生產環境至少 1,高可用建議 2

注意:分片數創建後不可變更(除非 reindex),副本數可動態調整。

路徑配置:分離數據、日誌、插件

path.data: /data/elasticsearch/data1,/data/elasticsearch/data2  # 多數據目錄提升 I/O
path.logs: /var/log/elasticsearch
path.plugins: /usr/share/elasticsearch/plugins
path.repo: /mnt/backup/snapshots  # 快照倉庫路徑

內存鎖定與 JVM 設置

bootstrap.memory_lock: true  # 鎖定物理內存,防止 Swap 導致性能抖動

配合環境變量:

export ES_JAVA_OPTS="-Xms4g -Xmx4g"  # 堆內存設為物理內存 50% 以內,且 min=max
ulimit -l unlimited  # Linux 允許進程鎖內存

網絡與傳輸配置

network.host: 192.168.1.100  # 綁定具體 IP,生產禁用 0.0.0.0
http.port: 9200              # REST API 端口
transport.tcp.port: 9300     # 節點間通信端口
transport.tcp.compress: true # 啟用 TCP 壓縮,跨機房必開
http.max_content_length: 100mb

發現與集群組建

discovery.seed_hosts: ["192.168.1.100:9300", "192.168.1.101:9300"]  # 種子節點
cluster.initial_master_nodes: ["es-master-01", "es-master-02"]      # 7.x+ 必填
discovery.zen.minimum_master_nodes: 2  # 6.x 防腦裂公式:(master資格節點/2)+1

線程池與隊列調優

thread_pool.write.queue_size: 10000   # 寫入隊列,防止突發流量拒絕
thread_pool.search.queue_size: 5000   # 搜索隊列
thread_pool.get.queue_size: 1000      # 獲取隊列

慢查詢日誌:性能排查利器

index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms

index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug: 500ms
index.search.slowlog.threshold.fetch.trace: 200ms

建議生產環境開啟 warn 級別,定期分析慢查詢優化 DSL。

熔斷與恢復控制

indices.breaker.total.limit: 70%        # 總熔斷閾值
indices.breaker.request.limit: 60%      # 請求熔斷
indices.recovery.max_bytes_per_sec: 40mb # 恢復限速,避免佔滿帶寬
cluster.routing.allocation.node_concurrent_recoveries: 2

生產環境檢查清單

  1. ✅ cluster.name 唯一且有業務含義
  2. ✅ bootstrap.memory_lock: true + JVM 堆內存固定
  3. ✅ 關鍵路徑掛載獨立磁盤(data/logs 分離)
  4. ✅ network.host 綁定內網 IP,http.port 防火牆限制訪問
  5. ✅ 配置 discovery.seed_hosts 與 initial_master_nodes
  6. ✅ 開啟慢查詢日誌與熔斷機制
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved