您的位置:首页 > 文旅 > 美景 > 【Elasticsearch】-7.17.24版本接入

【Elasticsearch】-7.17.24版本接入

2024/12/23 1:28:28 来源:https://blog.csdn.net/xcg340123/article/details/142211231  浏览:    关键词:【Elasticsearch】-7.17.24版本接入

官网 https://www.elastic.co/cn/downloads/elasticsearch

本项目基于windows环境下,其他环境操作类似

1、初始化配置

打开config/elasticsearch.yaml

添加如下配置

cluster.name: dams_clusternetwork.host: 127.0.0.1
http.port: 9200# 不开启geo数据库
ingest.geoip.downloader.enabled: false# 设置访问账号密码
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: false 

2、设置访问密码

使用elasticsearch提供的内置工具

2.1、手动设置密码

bin/elasticsearch-setup-passwords interactive

2.2、自动分配密码

密码配置需要elasticsearch处于运行状态,否则执行失败

bin/elasticsearch-setup-passwords auto

其中,用户权限分别如下:

  • elastic 账号:拥有 superuser 角色,是内置的超级用户。
  • kibana 账号:拥有 kibana_system 角色,用户 kibana 用来连接 elasticsearch 并与之通信。Kibana 服务器以该用户身份提交请求以访问集群监视 API 和 .kibana 索引。不能访问 index。
  • logstash_system 账号:拥有 logstash_system 角色。用户 Logstash 在 Elasticsearch 中存储监控信息时使用。

2.3、修改密码

curl -H "Content-Type:application/json" -XPOST -u elastic 'http://127.0.0.1:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'

2.4、带密码查询

curl -XGET --user user:passwd 'http://XXXX:9200/XX/XXX'

3、Java客户端连接

官方Java接入说明 

3.1、pom依赖

 <properties><es.version>7.17.24</es.version></properties><dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>${es.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.17.0</version></dependency><dependency><groupId>jakarta.json</groupId><artifactId>jakarta.json-api</artifactId><version>2.0.1</version></dependency>

 3.2、初始化连接

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;/*** 初始化服务,可以对外*/public synchronized ElasticsearchClient init(EsConfig esConfig) {if (null != esClient) {return esClient;}final BasicCredentialsProvider credential = new BasicCredentialsProvider();// 配置身份验证if (esConfig.openAuth()) {credential.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(esConfig.getLoginName(), esConfig.getPassword()));}RestClient restClient = RestClient.builder(new HttpHost(esConfig.getIp(), esConfig.getPort(), esConfig.getScheme())).setRequestConfigCallback(requestConfigBuilder -> {// 设置连接超时和请求超时return requestConfigBuilder.setConnectTimeout(5000)    // 连接超时时间.setSocketTimeout(60000)    // 套接字超时时间.setConnectionRequestTimeout(1000); // 请求超时时间}).setHttpClientConfigCallback(httpClientBuilder -> {// 设置 keep-alive 策略return httpClientBuilder.setDefaultCredentialsProvider(credential).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);}).build();ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());// And create the API clientesClient = new ElasticsearchClient(transport);return esClient;}

4、启动依赖问题

由于elasticsearch-java 默认加载7.12.1 版本的rest-client ,导致代码冲突

并且手动引入 elasticsearch-rest-client包

调整pom结构如下

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.1</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpasyncclient</artifactId><version>4.1.5</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore-nio</artifactId><version>4.4.15</version></dependency><dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>7.17.24</version><exclusions><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>7.17.24</version><systemPath>${project.basedir}/libs/elasticsearch-rest-client-7.17.24.jar</systemPath><scope>system</scope></dependency>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com