问题:elasticsearch集群开启安全认证后 利用transport访问的时候 报错
org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}
.put("client.transport.sniff", true)//自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列 --->问题出在这 当本地客户端访问远程集群(自动发现时会使用内网IP进行通信,导致无法连接到ES服务器)的时候应该设置为false或者此配置去掉,直接用addTransportAddress方式添加远程集群ip
public TransportClient getTransportClient() throws UnknownHostException {
Settings settings = Settings.builder()
.put("cluster.name", "myClusterName")//集群名称
.put("client.transport.sniff", false)//自动嗅探整个集群 本地客户端访问远程集群注意设置为false
.put("xpack.security.transport.ssl.enabled", true)//设置xpack ssl这里设置为true
.put("xpack.security.user", "xxxxx:xxxxx")//集群设置的用户名:密码
.put("xpack.security.transport.ssl.keystore.path", "D:\\ES\\elastic-certificates.p12")//路径这里自己定义只要可以找到p12认证文件
.put("xpack.security.transport.ssl.truststore.path", "D:\\ES\\elastic-certificates.p12")
.put("xpack.security.transport.ssl.verification_mode", "certificate")
.build();
TransportClient transportClient = new PreBuiltXPackTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("xxxxx"), 9300));//集群的ip地址
log.info("transportClient.listedNodes():{}", transportClient.listedNodes());
log.info("ElasticsearchXPackClient 启动成功");
return transportClient;
}