docker安装elasticsearch7.6.2及其客户端

docker安装elasticsearch及其客户端

本文将主要记录elasticsearch安装过程中遇到的一些问题

1. 安装配置elasticsearch

(1)docker下载相关镜像

docker pull elasticsearch:7.6.2

(2)创建持久化文件

mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data

初始化配置

echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml

注意这里“:”后的空格,否则会报语法错误

(3)启动镜像

docker run --name elasticsearch -p 9200:9200  -p 9300:9300 \
 -e "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms84m -Xmx512m" \
 -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
 -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
 -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
 -d elasticsearch:7.6.2

-p:端口映射

-e discovery.type=single-node 单点模式启动

-e ES_JAVA_OPTS="-Xms84m -Xmx512m":设置启动占用的内存范围(实验环境启动后可能因为云服务器内存过小而占满)
-v 目录挂载
-d 后台运行

启动后使用docker ps查看发现未正常启动,查看启动日志

docker logs elasticsearch
elasticsearch启动异常.png

发现是文件拒绝访问异常,为该文件夹设置所有用户都有读写执行权限

chmod -R 777 /mydata/elasticsearch/

重新启动elasticsearch

docker restart elasticsearch 

浏览器访问 ip:9200测试,访问成功

success.png

2. 安装使用可视化工具

(1)下载同版本镜像

docker pull kibana:7.6.2

(2)初始化配置

mkdir -p /mydata/kibana
touch /mydata/kibana/kibana.yml
vim /mydata/kibana/kibana.yml
server.host: 0.0.0.0
elasticsearch.hosts: http://你的ip:9200

保存退出

(3)启动kibana

docker run --name kibana -v /mydata/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5601:5601 -d kibana:7.6.2 

启动后可通过docker ps查看是否启动成功

等待一段时间后浏览器访问 ip:5601 进行测试

3.SpringBoot中使用Elasticsearch Java Rest Client

(1)导入相关依赖


        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.6.2
        
        
            org.elasticsearch
            elasticsearch
            7.6.2
        

        
            org.elasticsearch.client
            elasticsearch-rest-client
            7.6.2
        

此处官方文档中只写了导入elasticsearch-rest-high-level-client依赖,但在实际测试运行中会报NoClassDefFoundError错误,检查相关依赖后发现elasticsearch-rest-high-level-client中的elasticsearch依赖并不是7.6.2,因此需要再手工引入对应版本的依赖,导入完成后可正常运行

客户端依赖.png

(2)创建相关Bean

import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 李丰翼
 * @DateTime: 2020/7/13 0013 09:36
 * @Description: TODO
 */
@Configuration
public class ClientConfig {
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }
    @Bean
    public RestHighLevelClient esRestClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")
                        ));
        return client;
    }
}

(3)测试是否正常运行

直接打印client对象看是否成功注入

@SpringBootTest
class GulimallSearchApplicationTests {
@Resource
private RestHighLevelClient client;
    @Test
    void contextLoads() throws IOException {
        System.out.println(client);
    }
}

elasticsearch使用教程可参考官方文档

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl.html

kibana参考文档

https://www.elastic.co/guide/en/kibana/7.x/introduction.html

Java REST Client参考文档

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high.html

你可能感兴趣的:(docker安装elasticsearch7.6.2及其客户端)