ElasticSearch TransportClient失败,无法初始化类org.elasticsearch.common.lucene.Lucene

时间:2016-08-18 17:10:07

标签: java elasticsearch lucene

我的项目取决于

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>shield</artifactId>
        <version>2.3.3</version>
    </dependency>

我的TransportClient看起来像这样:

public synchronized Client getClient(String clusterName, List<String> hosts, Integer port) {
// Build the settings for our client.
Settings settings = Settings.settingsBuilder()
        .put("transport.ping_schedule", "5s")
        .put("cluster.name", clusterName)
        .put("action.bulk.compress", false)
        .put("shield.transport.ssl", true)
        .put("request.headers.X-Found-Cluster", clusterName)
        .put("shield.user", "USERNAME:PASSWORD")
        .build();

TransportClient client = clients.get(clusterName);
if (client == null) {
    client = TransportClient.builder().addPlugin(ShieldPlugin.class).settings(settings).build();

    clients.put(clusterName, client);

    for(String host : hosts) {
        try {
            for (InetAddress address : InetAddress.getAllByName(host)) {
                if (address instanceof Inet4Address) {
                    client.addTransportAddress(new InetSocketTransportAddress(address, port));
                }
            }
        } catch (UnknownHostException e) {
            throw new ElasticSearchException(e);
        }
    }

    try {
        logger.info("Getting cluster health... ");
        ActionFuture<ClusterHealthResponse> healthFuture = client.admin().cluster().health(Requests.clusterHealthRequest());
        ClusterHealthResponse healthResponse = healthFuture.get(5, TimeUnit.SECONDS);
        logger.info("Got cluster health response: [{}]", healthResponse.getStatus());
    } catch(Throwable t) {
        logger.error("Unable to get cluster health response: [{}]", t.getMessage());
    }
}

return client;
}

我的传输客户端如果失败并显示以下堆栈跟踪:

org.elasticsearch.client.transport:383 [Abraham Cornelius] failed to get node info for {#transport#-1}{000.000.000.000}{XXX.us-east-1.aws.found.io/000.000.000.000:9343}, disconnecting...
org.elasticsearch.transport.RemoteTransportException: [Failed to deserialize response of type [org.elasticsearch.action.admin.cluster.node.liveness.LivenessResponse]]
org.elasticsearch.transport.TransportSerializationException: Failed to deserialize response of type [org.elasticsearch.action.admin.cluster.node.liveness.LivenessResponse]
    at org.elasticsearch.transport.netty.MessageChannelHandler.handleResponse(MessageChannelHandler.java:180) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:138) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_67]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_67]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_67]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.common.lucene.Lucene
    at org.elasticsearch.Version.fromId(Version.java:568) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.elasticsearch.Version.readVersion(Version.java:310) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.elasticsearch.cluster.node.DiscoveryNode.readFrom(DiscoveryNode.java:339) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.elasticsearch.cluster.node.DiscoveryNode.readNode(DiscoveryNode.java:322) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.elasticsearch.action.admin.cluster.node.liveness.LivenessResponse.readFrom(LivenessResponse.java:52) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    at org.elasticsearch.transport.netty.MessageChannelHandler.handleResponse(MessageChannelHandler.java:178) ~[phoenix-1.0.0-SNAPSHOT-fat.jar:na]
    ... 30 common frames omitted

当我运行IDE,IntelliJ时,一切运行正常。但是当我直接运行胖罐时,kaboom。

1 个答案:

答案 0 :(得分:5)

here描述了这个问题的解释。简而言之,ElasticSearch及其lucene依赖项在META-INF / services中具有冲突的文件,当它们组合在一个jar中时,它们会被相互覆盖。

如果你遇到这个问题,你可能会建立一个罐子。如果您正在使用maven,您可以使用shade插件来解决这个问题,以合并这些文件并防止它们被覆盖:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/services/org.apache.lucene.codecs.Codec</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/services/org.apache.lucene.codecs.DocValuesFormat</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/services/org.apache.lucene.codecs.PostingsFormat</resource>
                                </transformer>
                            </transformers>
                         <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>fat</shadedClassifierName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>