由于项目spring boot版本较低,不能够兼容较高版本ES,所以使用elastic6.3.0版本。在此记录下集成步骤(只记录过程),供自己参考。
es开启x-pack验证安装es和kibana等步骤直接解压安装即可。
更改elasticsearch.yml配置文件设置开启x-pack。
xpack.security.enabled: true xpack.security.transport.ssl.enabled: true
重新启动es后先用psotman访问下面链接,开启试用期30天试用,不然直接运行设置账号密码命令可能
会报连接不上的错误。
http://localhost:9200/_xpack/license/start_trial?acknowledge=true
如图所示:
接下来设置账号密码,进入es安装目录执行下面命令。
/bin/elasticsearch-setup-passwords interactive
然后根据窗口提示一步步设置就行,设置完es、kibana等密码。
如有kibana则需要在kibana.yml文件中配置es的密码。
elasticsearch.username: "kibana" elasticsearch.password: "xxxxx"
至此x-pack开启及账号密码设置完成。
如下图所示访问localhost:9200/
访问localhost:5601/
开启x-pack后试用期只有一个月,所以我们需要更新产品license来延长x-pack试用时间。es6.3后官方已开放源码,只要要重写源码中LicenseVerifier和XpackBuild这两个类。经过编译后形成class文件再替换掉源码原有的class文件即可。在此附上编译好的class文件。
https://pan.baidu.com/s/1G_ACslZQZcr37ME0FfNR7g
提取码:9527
替换位置
es安装目录下 modulesx-packx-pack-corex-pack-core-6.3.0.jar
用解压缩软件打开jar包
替换掉class文件后去elastic官网申请license证书
根据提示一步步操作后,elastic官网会生成一个json文件,这就是license文件。把名字改成license.json并打开文件修改下面的信息即可。
"type":"platinum" #改为白金版 "expiry_date_in_millis":2524579200999 #修改license过期时间
修改完成后访问kibana
localhost:5601/
输入自己之前设置好的账号密码
完成后登录
http://127.0.0.1:9200/_license
可看到最新的授权文件内容信息。
至此license证书更新完成
父依赖
org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE
pom.xml
org.springframework.boot spring-boot-starter-data-elasticsearch org.elasticsearch.client x-pack-transport 6.3.0
因为开启了x-pack验证,所以原有的连接elastic的配置方式被舍弃,创建searchConfig类。采用x-pack方式连接elastic。(目前这种方式可用,但es更高版本会弃用)
package com.tyky.search.config;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Configuration
public class SearchConfig {
private final static Logger log = LoggerFactory.getLogger(SearchConfig.class);
@Bean
public TransportClient transportClient() throws UnknownHostException {
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put("cluster.name", "elasticsearch")
.put("xpack.security.user", "elastic:changeme")
.put("xpack.security.transport.ssl.enabled", false)
.build())
.addTransportAddress(new TransportAddress(InetAddress.getByName("es-ip"), 9300));
return client;
}
@Bean(name = "elasticsearchTemplate")
public ElasticsearchOperations elasticsearchTemplateCustom() throws Exception {
ElasticsearchTemplate elasticsearchTemplate;
try {
elasticsearchTemplate = new ElasticsearchTemplate(transportClient());
log.info("初始化ElasticsearchTemplate成功");
return elasticsearchTemplate;
} catch (Exception e) {
e.printStackTrace();
log.error("初始化ElasticsearchTemplate失败");
return new ElasticsearchTemplate(transportClient());
}
}
}
至此,es集成完成,其他如创建索引、增删改查等操作略。
可能遇到的错误一、springboot集成完成后,启动项目可能会报找不到这个类,我不知道为啥会报这个错,如有大佬知道烦请告知。
解决:添加依赖
com.unboundid unboundid-ldapsdk 3.2.0



