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<DiscoveryNode> 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" .lang.NoClassDefFoundError: org//xpack/core/XPackClientPlugin
	at org..xpack.client.PreBuiltXPackTransportClient.<init>(PreBuiltXPackTransportClient.:59)
	at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.<init>(PreBuiltXPackTransportClient.:54)
	at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.<init>(PreBuiltXPackTransportClient.:50)
	at com.jiadun.ReadDataToEs.utils.ElasticSearchOperator.<clinit>(ElasticSearchOperator.:58)
	at com.jiadun.ReadDataToEs.handler.ReadCsvFileToEs.main(ReadCsvFileToEs.:61)
Caused by: .lang.ClassNotFoundException: org.elasticsearch.xpack.core.XPackClientPlugin
	at .net.URLClassLoader.findClass(URLClassLoader.:381)
	at .lang.ClassLoader.loadClass(ClassLoader.:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.:331)
	at .lang.ClassLoader.loadClass(ClassLoader.: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如下:

 <dependencies>
      <!-- add the x-pack jar as a dependency -->
      <dependency>
         <groupId>org.elasticsearch.client</groupId>
         <artifactId>x-pack-transport</artifactId>
              <version>{version}</version>
      </dependency>
      ...
   </dependencies>

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

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

下面是无问题后的pom

        <dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>x-pack-transport</artifactId>
			<version>6.2.3</version>
		</dependency>
 
 		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>x-pack-core</artifactId>
			<version>6.2.3</version>
		</dependency> 
 
		<dependency>
			<groupId>com.unboundid</groupId>
			<artifactId>unboundid-ldapsdk</artifactId>
			<version>3.2.0</version>
		</dependency>

参考地址:https://github.com/elastic/elasticsearch/issues/31946

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

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注