SpringBoot ElasticSearch elk

 谁在使用ELK

维基百科, github都使用 ELK

docker下载并启动ElasticSearch


#
下载 docker pull docker.io/elasticsearch:6.5.1 # 安装 docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d -p 9200:9200 -p 9300:9300 --name myELK elkImageId

 

ElasticSearch默认2个G堆内存, 为了防止虚拟机内存不够,要手动指定elk启动参数 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" , 9200是elk暴露的RestFUL HTTP服务端口, 9300是java客户端调用的端口.

在启动时遇到异常:   max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]  , 意为ElasticSearch用户虚拟机能使用的虚拟内存区域太小,需要调大到262144以上 , 按如下操作:

# 修改配置文件
vi /etc/sysctl.conf
# 最后一行添加
vm.max_map_count=655300
# 重载该配置文件
sysctl -p

 

 

访问

http://ip:9200/ 返回如下信息表示成功启动

{
  "name" : "vcabSCo",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "LPiwCqIYQACz5Eir5P1qBQ",
  "version" : {
    "number" : "6.5.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "8c58350",
    "build_date" : "2018-11-16T02:22:42.182257Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

 

 http://ip:9200/index/type/id 为查询指定id

http://ip:9200/index/type/_search 为查询所有

 

ElasticSearch 各组件概念

索引 = mysql数据库

类型 = mysql数据表

文档 = mysql表字段内容 ( ES中使用json数据保存 )

processon地址: https://www.processon.com/diagraming/5ceb8caee4b0899ed43db40e

ElasticSearch和Mysql对应关系

技术库名 
mysqlDatabasesTablesRowsColumns 
ElasticSearchIndicesTypesDocumentsFields 

 

 

java连接ELK有两种方式

方式一: jest

jest是用于直联ElasticSearch服务的, 就好像redisClient 直联redis服务一样.

默认jest是不生效的, 需要导入jest的工具包(io.searchbox.client.JestClient) , 注意jest的大版本号和ES的大版本号要一致.

该模式的自动配置类为: JestAutoConfiguration

pom.xml导依赖 

导入之后JestAutoConfiguration里的内容就不会报红色错误了, 但是我发现高版本springboot好像不导也正常.

        <!--取消spring-boot-starter-data-elasticsearch依赖-->
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-data-elasticsearch</artifactId>-->
        <!--</dependency>-->


        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>

 

遇到错误,Jest无法存数据到ElasticSearch中, java后台不报错, ElasticSearch也不报错, 也没有日志显示错误 ,

目前jest5.3.3对ElasticSearch5.6.12 可以正常交互 ,   但jest5.3.3对ES6 或jest6对ES6无法正常交互. 晕死人.

 

方式二: SpringData ElasticSearch

通过使用Spring工具类转发,步骤如下

1. pom.xml导入spring-boot-starter-data-elasticsearch依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

 

2. 需要在application.properties中配置clusterNodes;clusterName

#使用springData连接ES
#集群名,访问 http://centos:9200/ 后返回的cluster_name即是节点名
spring.data.elasticsearch.cluster-name=elasticsearch
#IP:9300, 用的是9300通信
spring.data.elasticsearch.cluster-nodes=centos:9300

 3. 编写ElasticsearchRepository子接口来操作ES (就好像hibernateTemplate一样, 和JPA用法一致)

 

4. ElasticSearchTemplate 操作ES , 就好像(jdbcTemplate, 一样, 未实验, 只做了Repository的实验)

 

 

另外特别注意在使用SpringData elasticSearch 和 elasticSearch对接时的要注意版本问题 , 如何找版本匹配参考下图流程 , 懒人直接打开 https://github.com/spring-projects/spring-data-elasticsearch

 遇上版本不匹配的概率较高, 会发生异常"NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{3mA3x7OSSjustRG1_ZytWg}{centos}{192.168.2.106:9300}]"   一般有两种处理方式 

1. 调整springboot的版本(不建议)

2. 调整ElasticSearch的版本(建议)

3. Elasticsearch报 NoNodeAvailableException 的解决办法==>https://blog.csdn.net/adsl624153/article/details/78935796   (未亲测)

 

参考

SpringBoot整合ElasticSearch实现多版本的兼容==>https://www.cnblogs.com/xuwujing/p/8998168.html

elasticsearch安装之各种坑==>https://www.cnblogs.com/gudulijia/p/6761231.html

 

我的项目git地址

https://gitee.com/KingBoBo/springboot-04-elastic-search

转载于:https://www.cnblogs.com/whatlonelytear/p/10894013.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值