天天看點

關于Elasticsearch的簡單搭建和使用

簡單介紹

Elasticsearch 是一個分布式、高擴充、高實時的搜尋與資料分析引擎。它能很友善的使大量資料具有搜尋、分析和探索的能力。

ELK是三個開源軟體的縮寫,分别表示:Elasticsearch , Logstash, Kibana , 它們都是開源軟體。新增了一個FileBeat,它是一個輕量級的日志收集處理工具(Agent),Filebeat占用資源少,适合于在各個伺服器上搜集日志後傳輸給Logstash,官方也推薦此工具。

Elasticsearch是個開源分布式搜尋引擎,提供搜集、分析、存儲資料三大功能。它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多資料源,自動搜尋負載等。

Logstash

主要是用來日志的搜集、分析、過濾日志的工具,支援大量的資料擷取方式。一般工作方式為c/s架構,client端安裝在需要收集日志的主機上,server端負責将收到的各節點日志進行過濾、修改等操作在一并發往elasticsearch上去。

Kibana 也是一個開源和免費的工具,Kibana可以為 Logstash 和 ElasticSearch 提供的日志分析友好的 Web

界面,可以幫助彙總、分析和搜尋重要資料日志

我這邊主要使用在儲存日志中,本文主要是一些簡單使用

1準備工作

elasticsearch-6.4.3.tar.gz

kibana-6.4.3-linux-x86_64.tar.gz

jdk1.8

本次使用的都是6.4.3版本 在配置中盡可能儲存版本一緻

關于Elasticsearch的簡單搭建和使用

2 單機搭建

Elasticsearch 在啟動使用的時候是不能使用root 指令來進行的 是以我們在解壓後應該建立立使用者組
2.1 : 建立使用者組并授予權限
useradd jiang
passwd jiang
chown -R jiang:jiang elasticsearch-6.4.3
           
cd /elasticsearch-6.4.3/config  修改配置檔案
vi config/elasticsearch.yml

#在末尾添加配置


cluster.name: elk  #叢集名稱 
node.name: node-1 #節點名稱 
network.host: 0.0.0.0 #通路本地ip
http.port: 9200  #端口
           
啟動之前先需要修改計算機的一些配置
1: vim /etc/security/limits.conf
	末尾加上
	# End of file
	jiang soft nofile 65536  #jiang 你的使用者組名稱
	jiang hard nofile 65536   #jiang 你的使用者組名稱
2: vim /etc/security/limits.d/90-nproc.conf
			*          soft    nproc     4096
			root       soft    nproc     unlimited
			jiang      soft    nofile    65536
			jiang      hard    nofile    65536

3: vim /etc/sysctl.conf
#es 在末尾加上
	vm.max_map_count = 655360
	wq儲存推出
	sysctl -p 重新整理生效
           

配置好之後啟動

注意切換到建立組使用者啟動 root使用者是無法啟動的

進入bin目錄 /opt/elasticsearch-6.4.3/bin
./elasticsearch  非背景啟動
./elasticsearch & 背景啟動
           

啟動踩過的坑

1,使用root指令啟動

2, 沒有配置計算機參數: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

修改見 配置 3

3,max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

修改見 配置 1和2

4,啟動後産生的日志 /opt/elasticsearch-6.4.3/logs 裡産生了一些非建立組使用者權限的目錄 重新權重限為目錄組即可

例如:chown -R jiang:jiang elasticsearch_access.log

注意檢查下 當時好幾個目錄裡都産生了root使用者組的檔案 !!!

單機啟動完成測試

浏覽器 輸入 http://192.168.2.131:9200
傳回結果
{
  "name" : "node-1",
  "cluster_name" : "elk",
  "cluster_uuid" : "kjfgT0BqSuSqwjdMUtlzhQ",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
           

看到這個結果 恭喜成功

3 叢集搭建

我這邊是使用一台集器做了個僞叢集 通過修改端口号就行了

3.1 複制三份 elasticsearch

關于Elasticsearch的簡單搭建和使用

同樣的道理 都是不能已 root 指令啟動的

同時建立data 檔案和logs 檔案

mkdir data
mkdir logs
           

修改三個elasticsearch配置檔案

配置檔案簡單介紹

集器名稱
cluster.name: elk
 節點名稱
node.name: node-1
最大集器節點數
node.max_local_storage_nodes: 3 
是否有資格主節點
node.master: true
是否儲存資料
node.data: true
ip位址
network.host: 0.0.0.0
端口
http.port: 9200
集器直接通信端口
transport.tcp.port: 9700
es7之後新增的配置 發現節點
discovery.seed_hosts: ["localhost:9700","localhost:9800","localhost:9900"]
es7 之後新增的配置 用來節點選舉
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
儲存日志和檔案
path.data: /opt/data
path.logs: /opt/logs
           
開始elasticsearch1的配置
在結尾添加配置
cluster.name: elk
node.name: node-1
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9201
transport.tcp.port: 9700
#因為我們是用的 7之前的配置 是以不用添加這個 加上會報錯
#discovery.seed_hosts["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.ping.unicast.hosts: ["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.minimum_master_nodes: 2
#因為我們是用的 7之前的配置 是以不用添加這個 加上會報錯
#cluster.initial_master_nodes: ["node-1","node-2","node-3"]
path.data: /opt/data
path.logs: /opt/logs
           
開始elasticsearch2的配置
#添加配置
cluster.name: elk
node.name: node-2
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9202
transport.tcp.port: 9800
#discovery.seed_hosts: ["localhost:9700","localhost:9800","localhost:9900"]
#cluster.initial_master_nodes: ["node-1","node-2","node-3"]
discovery.zen.ping.unicast.hosts: ["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.minimum_master_nodes: 2

path.data: /opt/data
path.logs: /opt/logs
           

開始elasticsearch3的配置

#添加配置
cluster.name: elk
node.name: node-3
node.master: true
node.data: true
node.max_local_storage_nodes: 3
network.host: 0.0.0.0
http.port: 9203
transport.tcp.port: 9900
#discovery.seed_hosts: ["localhost:9700","localhost:9800","localhost:9900"]
#cluster.initial_master_nodes: ["node-1","node-2","node-3"]
discovery.zen.ping.unicast.hosts: ["localhost:9700","localhost:9800","localhost:9900"]
discovery.zen.minimum_master_nodes: 2

path.data: /opt/data
path.logs: /opt/logs
           

切記一定要授權到建立使用者組哦!!!!!

設定es 的jvm占用記憶體參數 我這邊示範修改一台

vim /opt/elasticsearch1/config/bin/elasticsearch

關于Elasticsearch的簡單搭建和使用

修改 vim /opt/elasticsearch1/config/jvm.options

es 啟動預設最小1G 最大1G 我們修改為 256m

關于Elasticsearch的簡單搭建和使用
切換使用者 依次啟動 bin目錄下 ./elasticsearch
http://192.168.2.131:9201/_cat/health?v

           
關于Elasticsearch的簡單搭建和使用
關于Elasticsearch的簡單搭建和使用

看到這 恭喜你 ok 拉

這樣看起來非常不簡明

引入 kibana

關于Elasticsearch的簡單搭建和使用

安裝包如上

直接進入修改配置檔案

vim /opt/kibana-6.4.3-linux-x86_64/config/kibana.yml
#通路端口
server.port: 5601
#ip位址 0.0.0.0 表示可遠端通路
server.host: "0.0.0.0"
#服務名
server.name: kibana-elk
#elasticsearch 位址
#高版本的話 es7之後新增的配置 配置如下
#elasticsearch.hosts:["http://localhost:9201","http://localhost:9202","http://localhost:9203"]
elasticsearch.url: "http://localhost:9201"
# 請求elasticsearch 逾時時間 預設30000
elasticsearch.requestTimeout: 99999
           

啟動 可以root下啟動 高版本是不建議的 我這個版本非root 是啟動不了的

bin目錄下 ./kibana

通路 192.168.2.131:5601 進入圖形化操作界面

關于Elasticsearch的簡單搭建和使用
點選 Dev Tools 進行 elasticSearch的一些簡單操作
#索引就類似我們的資料庫
1 #新增索引
PUT /test_index
#查詢索引
GET /test_index
#傳回結果
{
  "test_index": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1605141744625",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "E37aELtMRB27S8tdy-1Jbw",
        "version": {
          "created": "6040399"
        },
        "provided_name": "test_index"
      }
    }
  }
}
2 #添加資料
PUT /test_index/user/1
{
  "name":"test",
  "sex":"1",
  "age":"12"
}
3 #擷取資料
GET /test_index/user/1
#傳回結果
{
  "_index": "test_index",
  "_type": "user",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "name": "test",
    "sex": "1",
    "age": "12"
  }
}
4 #修改資料和添加是一樣的操作
5 #删除資料
DELETE /test_index/user/1
6 #關閉索引
POST /test_index/_close
7 #打開索引
POST /test_index/_open
#....等等就不列舉了

           

至此一個簡單的存儲和擷取就搭建成功了

4 springBoot整合 Elasticsearch

版本 
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

          <!-- 加入es核心jar包 -->
    <dependency>
          <groupId>org.elasticsearch.client</groupId>
          <artifactId>elasticsearch-rest-client</artifactId>
          <version>6.4.3</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-elasticsearch</artifactId>
          <version>3.2.6.RELEASE</version>
      </dependency>
           
之前springBoot采用了高版本一直找不到jar包 切換成2.2.2.RELEASE這個可以正常使用
yml配置
spring:
  data:
    elasticsearch:
      cluster-name: elk #elasticsearch.yml 配置的
      cluster-nodes: 192.168.2.131:9700 # 外部連接配接節點
           

代碼測試:

1 建立實體類

@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "estest", type = "user")
@Data
public class User implements Serializable {
    private String id;
    private String name;
    private int sex;
    private int age;
}
           
2 接口層(dao) 簡單一個查詢
@Component
public interface UserReposiory extends ElasticsearchRepository<User ,String> {

    User queryEmployeeById(String id);
}
           
3 controller層
@RestController
@RequestMapping("es")
public class ElasticSearchController {

    @Autowired
    private UserReposiory userReposiory;

    //增加
    @RequestMapping("/add/{id}")
    public String add(@PathVariable("id")String id){
        User user =new User ();
        user.setId(id);
        user.setName("哈哈");
        user.setAge(26);
        user.setSex(1);
        # save (elasticsearch内置自帶方法,可以直接使用)
        userReposiory.save(user);
        log.info("😁添加成功了》》》》");

        return "添加成功了";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(){
        User user =new User ();
        user.setId("1");
         # delete(elasticsearch内置自帶方法,可以直接使用)
        userReposiory.delete(user);
        log.info("😁删除成功了》》》》");
        return "删除成功了";
    }

    //局部更新
    @RequestMapping("/update")
    public String update(){
       
        User user =  userReposiory.queryEmployeeById("1");
        user.setName("我來更新了");
        userReposiory.save(employee);
        log.info("😁更新成功了》》》》");
        return "更新成功";
    }

}
           

添加後進入控制台檢視 發現資料和我們添加的一緻

關于Elasticsearch的簡單搭建和使用

這些就是關于es的一些簡單操作 後續有更新 請繼續關注