在做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>
posted on 2020-04-29 19:20  微笑点燃希望  阅读(2617)  评论(0编辑  收藏  举报
font=white