ES6.8.1集成springBoot报错:nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.transport.Netty3Plugin
第一步:配置pom文件依赖
<!-- ElasticSearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.13.Final</version>
</dependency>
第二步:配置文件添加配置属性
第三步:注意这是ES6.8.1加密插件链接配置
第四步:创建ES配置实例化客户端
package com.mobartisan.oms.historyalarm.configurer;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.client.Client;
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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.URL;
@Configuration
public class ElasticSearchConfig {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfig.class);
@Value("${elasticsearch.cluster.nodes}")
private String clusterNodes;
@Value("${elasticsearch.cluster.name}")
private String clusterName;
@Value("${elasticsearch.auth}")
private String securityUser;
@Value("${elasticsearch.secretKey}")
private String secretKey;
@Bean
public Client client(){
URL resource = this.getClass().getResource("/");
TransportClient client = null;
if(resource!=null){
File file = new File(resource.getPath());
Settings settings = Settings.builder().put("cluster.name", clusterName)
// .put("client.transport.sniff", true)
.put("xpack.security.user", securityUser)
.put("xpack.ssl.key", file+"/"+secretKey+"/elasticsearch.key")
.put("xpack.ssl.certificate", file+"/"+secretKey+"/elasticsearch.crt")
.put("xpack.ssl.certificate_authorities", file+"/"+secretKey+"/ca.crt")
.put("xpack.security.transport.ssl.enabled", "true").build();
client = new PreBuiltXPackTransportClient(settings);
try {
if (StringUtils.isNotEmpty(clusterNodes)) {
for (String node : clusterNodes.split(",")) {
String[] nodeInfo = node.split(":");
client.addTransportAddress(new TransportAddress(new InetSocketAddress(nodeInfo[0],Integer.parseInt(nodeInfo[1]))));
}
}
} catch (Exception e) {
logger.error("elastic search connection errr:"+e);
}
logger.info("连接ES成功");
}
return client;
}
}
报出异常:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.Client]: Factory method 'client' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.transport.Netty3Plugin
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
... 65 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.transport.Netty3Plugin
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.elasticsearch.plugins.PluginsService.loadPlugin(PluginsService.java:609)
at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:119)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:110)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:135)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:288)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:133)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.<init>(PreBuiltXPackTransportClient.java:47)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.<init>(PreBuiltXPackTransportClient.java:42)
at org.elasticsearch.xpack.client.PreBuiltXPackTransportClient.<init>(PreBuiltXPackTransportClient.java:38)
at com.mobartisan.oms.historyalarm.configurer.ElasticSearchConfig.client(ElasticSearchConfig.java:49)
at com.mobartisan.oms.historyalarm.configurer.ElasticSearchConfig$$EnhancerBySpringCGLIB$$4862b273.CGLIB$client$0(<generated>)
at com.mobartisan.oms.historyalarm.configurer.ElasticSearchConfig$$EnhancerBySpringCGLIB$$4862b273$$FastClassBySpringCGLIB$$2d824a57.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
at com.mobartisan.oms.historyalarm.configurer.ElasticSearchConfig$$EnhancerBySpringCGLIB$$4862b273.client(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 66 more
排查异常问题:
问题发现:表面是引入了ES插件6.8.1版本,实质其中的客户端jar使用的还是5.6.8版本,所以导致启动时匹配到的版本较低。
解决问题:
排除x-pack-transport:6.8.1插件jar下的transport:5.6.8客户端jar。
新增6.8.1的客户端transport的jar。
例如:
<!-- ElasticSearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.13.Final</version>
</dependency>
重新启动,问题解决。