一、前言
elasticsearch是一個免費、開源、分散式的搜索和分析引擎。elasticsearch是建立在Apache Lucene™軟體庫之上的,它提供了一個分散式多用戶能力的全文搜索引擎。elasticsearch是RESTful的(基於HTTP協議),它內部所有的操作都是JSON格式的。因此,它可以通過HTTP來訪問,從而可以在瀏覽器、命令行等多種環境下使用。
二、elasticsearch啟動流程
elasticsearch啟動過程中,其實會進行很多的初始化操作,如:讀取配置文件,創建必要的線程池,創建數據存儲目錄等,最後啟動各個組件,如:http服務,transport服務,jvm監控等。
我們可以通過以下步驟來了解elasticsearch啟動的整個過程:
Elasticsearch.java -> main | V Elasticsearch.java -> start | V Node.java -> start | V Node.java -> doStart | V InternalElasticsearchBuild.java -> start | V HttpServerTransport.java -> start | V TransportService.java -> start | V HttpServerTransport.java -> bindServer -> Jetty6HttpServerTransport.java -> startJetty | V Node.java -> onJoinClusterService -> LifecycleService.java -> start
三、啟動腳本及配置
在啟動elasticsearch時,我們需要使用以下命令:
./bin/elasticsearch
在bin目錄下,還有其他啟動腳本,如:
bin/elasticsearch-plugin : 安裝插件 bin/elasticsearch-keystore: 創建keystore bin/elasticsearch-certutil: 證書相關操作
elasticsearch啟動時還需要配置文件,它們位於config目錄下。其中,elasticsearch.yml是主配置文件,log4j2.properties是日誌配置文件,jvm.options是jvm參數文件。
另外,我們還可以通過在config目錄下創建scripts目錄,來添加一些啟動時的自定義腳本,如:
config/scripts -> setup_system_check -> 配置節點檢查腳本 config/scripts -> setup -> 安裝時的腳本
四、啟動參數
在啟動elasticsearch時,我們可以使用一些啟動參數來指定其行為。例如,通過以下命令啟動elasticsearch時,可以指定監聽的IP和埠:
./bin/elasticsearch -E http.host=0.0.0.0 -E http.port=9200
elasticsearch一共有兩類啟動參數:
- -E參數:用來設置elasticsearch的屬性值,如 http.host=127.0.0.1。
- -D參數:用來設置jvm的屬性值,如 -Djava.net.preferIPv4Stack=true。
五、集群啟動
在elasticsearch中,我們可以將多個節點組成一個集群來提高搜索和索引的速度。啟動elasticsearch集群時,我們需要設置集群名稱,如:
./bin/elasticsearch -E cluster.name=es_cluster
對於每個節點,我們還需要設置節點名稱、數據路徑、監聽地址、是否為主節點等屬性。
具體來說,可以在elasticsearch.yml中設置以下屬性:
node.name: "node-1" path.data: "/path/to/data" network.host: "0.0.0.0" http.port: 9200 discovery.seed_hosts: ["host1:port1", "host2:port2"] cluster.initial_master_nodes: ["node-1", "node-2"]
六、啟動過程中的錯誤處理
在啟動elasticsearch時,有可能會發生各種錯誤。例如:
- 埠被佔用
- 路徑不存在
- 配置文件格式錯誤
- 內存不足
針對不同的錯誤,我們可以採取不同的措施。例如,如果埠被佔用,我們可以修改http.port屬性,如:
./bin/elasticsearch -E http.port=9201
如果路徑不存在,我們需要創建相應的目錄,如:
mkdir /path/to/data
七、總結
通過以上對elasticsearch啟動的分析和總結,我們了解了elasticsearch的啟動流程、啟動腳本、啟動參數、集群啟動以及啟動過程中的錯誤處理。同時,我們還給出了一些示例代碼,希望對大家學習elasticsearch有所幫助。
原創文章,作者:UYHHC,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/343262.html