无法连接到弹性搜索

时间:2017-09-14 11:52:01

标签: java elasticsearch spring-boot

我正在构建一个查询ElasticSearch的简单REST API。我没有使用ElasticSearch REST API方式,而不是使用TCP方式。 我的目标是使用spring boot创建REST API,它将通过TCP与ElasticSearch进行通信 我正在使用ElasticSearch sever 5.6
我有一个ElasticSearchClientFactory,它包含与ElasticSearch服务器建立连接的代码。

ElasticSearchClientFactory.java'buildClient

void buildClient() {

        Settings settings = Settings.builder().put("cluster.name", elasticSearchProperties.getClusterName()).build();
        TransportClient transportClient = new PreBuiltTransportClient(settings);
        if (!StringUtils.isEmpty(elasticSearchProperties.getHostname())
                && !StringUtils.isEmpty(elasticSearchProperties.getPort() + "")) {

            try {

                transportClient.addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName(elasticSearchProperties.getHostname()),
                                elasticSearchProperties.getPort()));

                transportClient.connectedNodes();

                this.client = transportClient;

            } catch (UnknownHostException e) {

                LOGGER.error("Unable to Connect to ElasticSearch,UnknownHost" + elasticSearchProperties.getHostname());
            }
        } else {

            LOGGER.error("Missing ElasticSearch configuration hostname and portNumber");
            throw new DataAccessException(CommonConstants.DAE001, "Unable to connect to ElasticSearch");
        }

    }

pom.xml包含以下与ElasticSearch

相关的相关依赖项
   <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.5.2</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.5.2</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
    </dependency>

在服务器启动时打印日志 -

2017-09-14 16:52:28:840 search org.springframework.boot.SpringApplication [main] ERROR - Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticSearchClientFactory' defined in file [D:\projects\verizon\mesh_workspace\search\target\classes\com\sapient\mesh\productlisting\config\ElasticSearchClientFactory.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: io.netty.util.internal.ObjectUtil.checkPositive(ILjava/lang/String;)I
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:732)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
    at com.sapient.mesh.SearchApplication.main(SearchApplication.java:20)
Caused by: java.lang.NoSuchMethodError: io.netty.util.internal.ObjectUtil.checkPositive(ILjava/lang/String;)I
    at io.netty.util.NettyRuntime$AvailableProcessorsHolder.setAvailableProcessors(NettyRuntime.java:44)
    at io.netty.util.NettyRuntime.setAvailableProcessors(NettyRuntime.java:87)
    at org.elasticsearch.transport.netty4.Netty4Utils.setAvailableProcessors(Netty4Utils.java:82)
    at org.elasticsearch.transport.netty4.Netty4Transport.<init>(Netty4Transport.java:138)
    at org.elasticsearch.transport.Netty4Plugin.lambda$getTransports$0(Netty4Plugin.java:93)
    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:174)
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:130)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:116)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:106)
    at com.sapient.mesh.productlisting.config.ElasticSearchClientFactory.buildClient(ElasticSearchClientFactory.java:62)
    at com.sapient.mesh.productlisting.config.ElasticSearchClientFactory.afterPropertiesSet(ElasticSearchClientFactory.java:53)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    ... 16 common frames omitted

1 个答案:

答案 0 :(得分:1)

在java NoSuchMethodError中,通常告诉您有多个相同库的实例(或者有时你只有一个,但它是错误的)。 如果我是你,我会:

  • 使用mvn dependency:tree -Dverbose检查maven树。在树中,您将看到2个相同库的实例(可能是2个不同的版本)。您需要从具有此lib作为依赖项的条目中手动排除pom.xml中的依赖项。好吧,有时额外的lib可以来自server(例如glassfish)而不是maven,在这种情况下请查看下面的内容。
  • 将断点放到NettyRuntime.java:44,以调试模式连接,当你点击它时,评估错误库的路径。要评估路径,您需要询问liLad所在的classLoader。您可以这样做,在调试模式下评估代码*。 (io.netty.util.internal.ObjectUtil class.getClassLoader()).getResource("io.netty.util.internal.ObjectUtil.class"))这将为您提供图书馆的路径,从那时起您就会知道需要排除的内容及其来源

* Evaluate codeEvaluate expression是一个应该在IDE中以调试模式提供的选项(Eclipse / Jetbrains / Netbeans或者您使用的w / e,应该支持它)