ElasticSearch版本:7.7.1
配置
修改es的配置文件:elasticsearch.yml,添加如下配置
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
es的bin目录下,执行设置用户名和密码的命令
./elasticsearch-setup-passwords interactive
这里会设置六个账号的密码:elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
修改kibana的配置kibana.yml
:
es修改密码的命令如下:
curl -H "Content-Type:application/json" -XPOST -u elastic 'http://192.168.140:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'
重启es和kibana
访问http://192.168.1.40:9200,需要输入账号密码才可以访问
同样需要认证才可登陆,登陆账号密码为elastic/123456
java客户端连接
需要与springdata es整合
RestHighLevelClient
@Bean
public RestHighLevelClient restHighLevelClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("192.168.1.40:9200")
.withConnectTimeout(Duration.ofSeconds(5))
.withSocketTimeout(Duration.ofSeconds(3))
.withBasicAuth("elastic", "123456")
.build();
return RestClients.create(clientConfiguration).rest();
}
SpringData ES配置
spring:
elasticsearch:
rest:
uris: "http://192.168.1.40:9200"
read-timeout: "10s"
username: "elastic"
password: "123456"
connection-timeout: "10s"