Docker安裝ElasticSearch 以及使用LogStash實現索引庫和數據庫同步


1:下載 ElasticSearch 鏡像

docker pull docker.io/elasticsearch:5.6.8

 

2:創建 ElasticSearch 容器:

注意:5.0默認分配jvm空間大小為2g 5.0之前好像是1g

docker  run  -di  --name=my_es -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -p 9200:9200 -p 9300:9300 elasticsearch:5.6.8

啟動成功后在瀏覽器地址欄輸入:http://宿主機ip:9200  出現如下,表示啟動成功

 

3:但是如果 Java 使用 9300 端口連接ES 會出現如下錯誤

NoNodeAvailableException[None of the configured nodes are available:
[{#transport#‐1}{exvgJLR‐RlCNMJy‐hzKtnA}{192.168.184.135}{192.168.184.135:9300}]] at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodes AreAvailable at org.elasticsearch.client.transport.TransportClientNodesService.execute at org.elasticsearch.client.transport.TransportProxyClient.execute

 

 4:修改docker容器中的配置文件:

docker  exec  -it  my_es  /bin/bash

 進入 config 文件夾有一個  elasticsearch.yml  文件,但是發現 vim / vi 命令失效(因為是在docker容器中)

 

5 :首先退出容器,然后執行命令,拷貝配置文件到宿主機(必須保證容器中的ES是啟動狀態):

docker  cp  my_es:/usr/share/elasticsearch/config/elasticsearch.yml /usr/share/elasticsearch.yml

 

6:停止 和 刪除原來創建的容器

docker stop elasticsearch:5.6.8
docker rm my_es

 

7:重新執行創建容器命令(重點:掛載文件

docker run -di --name=my_es -p 9200:9200 -p 9300:9300 -v /usr/share/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml  elasticsearch:5.6.8

 

8: 修改  /usr/share/elasticsearch.yml  將 transport.host: 0.0.0.0 前的 # 去掉后保存文件退出。  其作用是允許任何ip地址訪問 elasticsearch 開發測試階段可以這么做,生產環境下指定具體的IP

9:重啟后發現重啟啟動失敗了(純宿主機問題),這與我們剛才修改的配置有關,因為elasticsearch在啟動的時候會進行一些檢查,比如最多打開的文件的個數以及虛擬內存區域數量等等

 

10:系統調優

(1)修改  /etc/security/limits.conf  追加內容

* soft nofile 65536
* hard nofile 65536

說明:nofile是單個進程允許打開的最大文件個數 soft nofile 是軟限制 hard nofile是硬限制


(2)修改  /etc/sysctl.conf  追加內容

vm.max_map_count=655360

說明:限制一個進程可以擁有的VMA(虛擬內存區域)的數量

 

11: 執行下面命令 修改內核參數馬上生效:sysctl  ‐p    重新啟動虛擬機,再次啟動容器,發現已經可以啟動並遠程訪問

 


 使用 RestApi 操作 ElasticSearch

  (PUT)創建索引:http://localhost:9200/index_name
  (GET)查詢所有:http://localhost:9200/index_name/type_name/_search
  (GET)根據id查詢:http://localhost:9200/index_name/type_name/1
  (GET)條件查詢:http://localhost:9200/index_name/type_name/_search?q=title:Spring
  (GET)模糊查詢:http://localhost:9200/index_name/type_name/_search?q=title:*spring*
  (DELETE)刪除:http://localhost:9200/index_name/type_name/1
  (POST)新增類型和文檔:http://localhost:9200/index_name/type_name
    {
      "title": "Spring框架",
      "content" : "Spring框架是由於軟件開發的復雜性而創建的"
    }
  (PUT)修改id不存在新增:http://localhost:9200/type_name/article/1
    {
      "title": "Spring框架",
      "content" : "Spring框架是由於軟件開發的復雜性而創建的"
    }

 安裝IK分詞器

docker  cp  ik  my_es:/usr/share/elasticsearch/plugins
默認分詞:http://127.0.0.1:9200/_analyze?analyzer=chinese&pretty=true&text=我是程序員
最少切分:http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序員
最細切分:http://127.0.0.1:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序員
定制詞匯
(1):elasticsearch-5.6.8\plugins\ik\config 文件夾下創建文件名 xxx.dic (2):修改 IKAnalyzer.cfg.xml 文件:<entry key="ext_dict">xxx.dic</entry>

 使用  ElasticSearch Head 連接ES會出現跨域問題的解決方法: 在 elasticsearch.yml 文件 添加

 

http.cors.enabled: true
http.cors.allow-origin: "*"

 


使用 LogStash 實現索引庫和數據庫同步

(1):安裝 LogStash (直接解壓)
2):啟動命令:logstash -e 'input { stdin { } } output { stdout {} }'
3):配置同步 創建 xxx.conf 文件:
input { jdbc { # mysql jdbc connection string to our backup databse jdbc_connection_string
=> "jdbc:mysql://192.168.2.130:3306/article?characterEncoding=UTF8" # the user we wish to excute our statement as jdbc_user => "root" jdbc_password => "root" # the path to our downloaded jdbc driver jdbc_driver_library => "D:\logstash-5.6.8\mysqletc\mysql-connector-java-5.1.46.jar" # the name of the driver class for mysql jdbc_driver_class => "com.mysql.jdbc.Driver" jdbc_paging_enabled => "true" jdbc_page_size => "50" #以下對應着要執行的sql的絕對路徑。 #statement_filepath => "" statement => "select id, title, content, state FROM tb_article" #定時字段 各字段含義(由左至右)分、時、天、月、年,全部為*默認含義為每分鍾都更新(測試結果,不同的話請留言指出) schedule => "* * * * *" } } output { elasticsearch { #ESIP地址與端口 hosts => "127.0.0.1:9200" #ES索引名稱(自己定義的) index => "tensquare_article" #自增ID編號 document_id => "%{id}" document_type => "article" } stdout { #以JSON格式輸出 codec => json_lines } } 啟動 logstash:logstash -f conf.xml

 


 ElaticSearch配套軟件(安裝包,Head,LogStash,Node,js):

鏈接:https://pan.baidu.com/s/1JhL75TGtxkVXplZqrluAWw 
提取碼:7a69 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM