ElasticSearch6.2.3 Java Client 连接安装Xpack的ES集群的Bug问题

ES集群安装X-pack之后java client连接报错:

Settings settings = Settings.builder()
                .put("cluster.name", Config.clusterName)
                .put("client.transport.sniff", true)
                .put("xpack.security.transport.ssl.enabled", false)
                .put("xpack.security.user", "elastic:zhoushaowei520.")
                .build();//指定集群名称 
       // XPack
        try {
			client = new PreBuiltXPackTransportClient(settings)
					.addTransportAddress(new TransportAddress(InetAddress.getByName(Config.nodeHost), Config.nodePort));
		    List connectedNodes = client.connectedNodes();  
		    for(DiscoveryNode node : connectedNodes)  
		    {  
		    	System.out.println(node.getHostAddress());
		    	LOG.info("sniff host :" + node.getHostAddress());
		    }
		} catch (UnknownHostException e) {
			e.printStackTrace();
			LOG.error(" Host Is Unknown ");
		}

报错信息:

Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/xpack/core/XPackClientPlugin
	at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:59)
	at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:54)
	at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.(PreBuiltXPackTransportClient.java:50)
	at com.jiadun.ReadDataToEs.utils.ElasticSearchOperator.(ElasticSearchOperator.java:58)
	at com.jiadun.ReadDataToEs.handler.ReadCsvFileToEs.main(ReadCsvFileToEs.java:61)
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.xpack.core.XPackClientPlugin
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 5 more

在6.2和更早的版本中,x- package -transport POM依赖于x- package -api,它引入了第三方依赖关系,例如UnboundId LDAP SDK和Bouncy Castle(用于读取TLS键)

(缩写)依赖关系树是:

+- org.elasticsearch.client:x-pack-transport:jar:6.2.4:compile
|  +- org.elasticsearch.plugin:x-pack-api:jar:6.2.4:compile
|  |  +- com.unboundid:unboundid-ldapsdk:jar:3.2.0:compile
|  |  +- org.bouncycastle:bcprov-jdk15on:jar:1.58:compile
|  |  +- org.bouncycastle:bcpkix-jdk15on:jar:1.58:compile

由于6.3,x- package -api POM不再存在,x- package -transport只依赖于x- package -core,不包含任何第三方依赖。

从grepping可以看出,我们生成的POM文件中没有一个包含ldapsdk的依赖项(我假设也不包含bouncy castle)。

这意味着文档中发布的依赖项是不够的。

官网给的依赖pom如下:

 
      
      
         org.elasticsearch.client
         x-pack-transport
              {version}
      
      ...
   

单独依赖这个jar包是不行的,这是6.2版本以及之前的一个bug

解决办法github上有详细解答,在此只列出我修改之后的pom文件,仅做参考,x-pack-core.jar依赖之后会增加很多无用的包,我没有进一步去精简,网上有编译好的x-pack-api.jar包。

下面是无问题后的pom

        
			org.elasticsearch.client
			x-pack-transport
			6.2.3
		

 		
			org.elasticsearch.client
			x-pack-core
			6.2.3
		 

		
			com.unboundid
			unboundid-ldapsdk
			3.2.0
		

参考地址:

https://github.com/elastic/elasticsearch/issues/31946

相同问题:https://discuss.elastic.co/t/error-while-creating-elastic-search-x-pack-transport-client/143839

我的个人小站:成都有什么好玩的 锦里好玩吗 成都春熙路好玩吗 春熙路上有什么好吃的

你可能感兴趣的:(ElasticSearch,JAVA)