已安装好elasticsearch5.6.2
下载x-pack插件
上传插件到服务器 /data路径下
切换用户,然后安装
./elastic-plugin install file:///data/x-pack-5.6.2.zip
参考这个
Java 连接ElasticSearch5.6.2
maven配置
elasticsearch-releases
https://artifacts.elastic.co/maven
true
false
......
org.elasticsearch
elasticsearch
${elasticsearch.version}
org.elasticsearch.client
transport
${elasticsearch.version}
org.elasticsearch.client
x-pack-transport
${elasticsearch.version}
读取配置文件中的属性值,最好设置默认值
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
@Configuration
@Slf4j
public class ElasticSearchConfig {
@Value("${elasticsearch.host}")
private String hostName;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.username:elastic}")
private String username;
@Value("${elasticsearch.password:abcd@12345}")
private String password;
@Value("${elasticsearch.clusterName}")
private String clusterName;
@Value("${elasticsearch.pool}")
private int poolSize;
@Bean(name = "transportClient")
public TransportClient init() {
TransportClient transportClient = null;
try {
// 配置信息
Settings esSetting = Settings.builder()
.put("cluster.name", clusterName)
//增加线程池个数,暂时设为5
.put("thread_pool.search.size", poolSize)
.put("xpack.security.user", username + ":" + password)
.build();
transportClient = new PreBuiltXPackTransportClient(esSetting);
InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(InetAddress.getByName(hostName), port);
transportClient.addTransportAddresses(inetSocketTransportAddress);
} catch (Exception e) {
log.error("elasticsearch TransportClient create error!!!", e);
}
return transportClient;
}
}



