Skip to content

elasticsearch 启动报错NoSuchMethodError Logger

最近需要使用es,故搭建相关的环境,在搭建过程中,按照官方文档却出现如下错误:

Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.common.logging.Loggers.getLogger(Ljava/lang/String;)Lorg/apache/logging/log4j/Logger;
	at org.elasticsearch.transport.netty4.Netty4InternalESLogger.(Netty4InternalESLogger.java:34)
	at org.elasticsearch.transport.netty4.Netty4Utils$1.newInstance(Netty4Utils.java:53)
	at io.netty.util.internal.logging.InternalLoggerFactory.getInstance(InternalLoggerFactory.java:93)
	at io.netty.util.internal.logging.InternalLoggerFactory.getInstance(InternalLoggerFactory.java:86)
	at io.netty.util.internal.PlatformDependent.(PlatformDependent.java:70)
	at io.netty.util.ConstantPool.(ConstantPool.java:32)
	at io.netty.util.AttributeKey$1.(AttributeKey.java:27)
	at io.netty.util.AttributeKey.(AttributeKey.java:27)
	at org.elasticsearch.transport.netty4.Netty4Transport.(Netty4Transport.java:231)
	at org.elasticsearch.transport.Netty4Plugin.getSettings(Netty4Plugin.java:56)
	at org.elasticsearch.plugins.PluginsService.lambda$getPluginSettings$0(PluginsService.java:89)
	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:89)
	at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:147)
	at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:288)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:128)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:114)
	at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:104)
	at com.cqmaple.search.es.Application.main(Application.java:20)

经过追查代码,发现是依赖的问题,我所使用的是:

compile 'org.elasticsearch.client:transport:6.6.0'

但是默认情况下transport-netty4-client依赖的却是6.4.3;
其中构造函数中:

import org.elasticsearch.common.logging.Loggers;
Netty4InternalESLogger(final String name) {
super(name);
this.logger = Loggers.getLogger(name);
}

而在Loggers 中只有:

public static Logger getLogger(Class<?> clazz, ShardId shardId, String... prefixes) {
    return getLogger(clazz, shardId.getIndex(), asArrayList(Integer.toString(shardId.id()), prefixes).toArray(new String[0]));
}

/**
 * Just like {@link #getLogger(Class, ShardId, String...)} but String loggerName instead of
 * Class and no extra prefixes.
 */
public static Logger getLogger(String loggerName, ShardId shardId) {
    String prefix = formatPrefix(shardId.getIndexName(), Integer.toString(shardId.id()));
    return new PrefixLogger(LogManager.getLogger(loggerName), prefix);
}

public static Logger getLogger(Class<?> clazz, Index index, String... prefixes) {
    return getLogger(clazz, asArrayList(Loggers.SPACE, index.getName(), prefixes).toArray(new String[0]));
}

public static Logger getLogger(Class<?> clazz, String... prefixes) {
    return new PrefixLogger(LogManager.getLogger(clazz), formatPrefix(prefixes));
}

public static Logger getLogger(Logger parentLogger, String s) {
    Logger inner = LogManager.getLogger(parentLogger.getName() + s);
    if (parentLogger instanceof PrefixLogger) {
        return new PrefixLogger(inner, ((PrefixLogger)parentLogger).prefix());
    }
    return inner;
}

解决办法:
强制添加 transport-netty4-client 版本为6.6.0

compile 'org.elasticsearch.plugin:transport-netty4-client:6.6.0'

发现最新的6.6.2 也是一样 需要修改为一致的版本号。

发表评论

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