Elasticsearch java.lang.ClassNotFoundException: org.elasticsearch.common.transport.InetSocketTransportAddress


在做Elasticsearch 版本升級的時候,報出了
java.lang.ClassNotFoundException: org.elasticsearch.common.transport.InetSocketTransportAddress 這個錯誤,在此記錄一下解決方法

開發環境
IntelliJ IDEA 2018.1.6 maven3.3.3 SpringBoot 2.0.2.RELEASE ElasticSearch 6.1.1 
連接ElasticSearch 代碼
    @Value("${spring.data.elasticsearch.cluster-name}") private static String clusterName; @Value("${spring.data.elasticsearch.cluster-nodes}") private static String host; @Value("${spring.data.elasticsearch.properties.port}") private static Integer port; /** 構建Settings 對象 */ private static Settings settings = Settings.builder().put("cluster.name", clusterName).build(); /** TransportClient 對象, 用於連接ES集群 */ private static volatile TransportClient client; /** * 同步synchronized(*.class)代碼塊的作用和synchronized static方法作用一樣, * 對當前對應的*.class 進行持鎖, static方法和.class 一樣都是鎖的該類本身,同一個監聽器 * @return */ public static TransportClient getClient(){ if(client == null){ synchronized (TransportClient.class){ client = new PreBuiltTransportClient(settings); try { String[] allHost = host.split(","); for (String str:allHost) { client.addTransportAddresses(new TransportAddress(InetAddress.getByName(str), port)); } } catch (UnknownHostException e) { e.printStackTrace(); } } } return client; } 
錯誤日志
Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/common/transport/InetSocketTransportAddress at org.elasticsearch.transport.Netty4Plugin.getSettings(Netty4Plugin.java:55) at org.elasticsearch.plugins.PluginsService.lambda$getPluginSettings$0(PluginsService.java:82) at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) at org.elasticsearch.plugins.PluginsService.getPluginSettings(PluginsService.java:82) at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:135) at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:262) at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:128) at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:114) at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:104) at com.zichan360.fica.controller.TestController.getClient(TestController.java:49) at com.zichan360.fica.controller.TestController.getAdminClient(TestController.java:64) at com.zichan360.fica.controller.TestController.isExists(TestController.java:79) at com.zichan360.fica.controller.TestController.main(TestController.java:68) Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.transport.InetSocketTransportAddress at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 19 more
解決方法

添加 transport-netty4-client maven 依賴之后可以成功獲取到連接

   <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>6.1.1</version> </dependency>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM