官网 https://www.elastic.co/cn/downloads/elasticsearch
本项目基于windows环境下,其他环境操作类似
1、初始化配置
打开config/elasticsearch.yaml
添加如下配置
cluster.name: dams_cluster
network.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 client
esClient = new ElasticsearchClient(transport);
return esClient;
}