创建RestHighLevelClient bean

时间:2017-10-20 18:09:32

标签: java spring elasticsearch

我在SpringBoot应用程序中创建HighEndRestClient bean时遇到错误。我做了一个测试应用程序'在我检查的地方,我可以实例化我想要的对象,然后拨打我想要的电话,现在我正在制作新的应用程序。

我在pom中有这些依赖

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>5.6.3</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>5.6.3</version>
    </dependency>

我已经在配置类中编写了这个非常基本的代码,但是还没有做很多事情

@Configuration
@PropertySource(value = "classpath:application.properties")
@EnableElasticsearchRepositories(basePackages = "com.indexbuilder.es.repo")
public class ElasticsearchConfiguration {
    @Value("${elasticsearch.host}")
    private String EsHost;

    @Value("${elasticsearch.port}")
    private int EsPort;

    @Value("${elasticsearch.clustername}")
    private String EsClusterName;


    @Bean
    public RestClientBuilder coreBuilder() throws Exception {
        RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
        builder.setMaxRetryTimeoutMillis(10000);

        builder.setFailureListener(new RestClient.FailureListener() {
            @Override
            public void onFailure(HttpHost host) {
                System.out.println("FAILURE !!!! FailureListener HAS WOKEN UP!!!! CREATYE A FAILURE LISTENER BEAN" );
            }
        });

        return builder;
      }

    @Bean
    public RestClient restLowLevelClient() throws Exception{
        RestClient restClient = coreBuilder().build();
        return restClient;
    }

就我所见,这种方法很好(我还没有做太多...)

当我添加它时(最初我传入的是RestClient bean,但现在我暂时创建了一个本地对象以便更清晰)

    @Bean
    public RestHighLevelClient restHighLevelClient() throws Exception{
        RestClient restClient = coreBuilder().build();
        RestHighLevelClient client = new RestHighLevelClient(restClient);
        return client;
    }

我得到了这个java.lang.NoClassDefFoundError错误

  

========= | _ | ============== | ___ / = / / / _ / :: Spring Boot: :(v1.5.1.RELEASE)

     

[警告] java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:527)     在java.lang.Thread.run(Thread.java:745)引起:   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; restHighLevelClient&#39;的bean在类路径中定义   资源   [COM / indexbuilder /配置/ ElasticsearchConfiguration.class]:   通过工厂方法进行Bean实例化失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[org.elasticsearch.client.RestHighLevelClient]:工厂   方法&#39; restHighLevelClient&#39;抛出异常;嵌套异常是   java.lang.NoClassDefFoundError:   org / elasticsearch / action / main / MainRequest at   org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:598)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1140)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:525)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:304)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:744)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)     在   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)     在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)     在   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:314)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)     在   com.indexbuilder.SpringBootStartUpConfig.main(SpringBootStartUpConfig.java:84)     ... 6更多引起:   org.springframework.beans.BeanInstantiationException:失败   实例化[org.elasticsearch.client.RestHighLevelClient]:工厂   方法&#39; restHighLevelClient&#39;抛出异常;嵌套异常是   java.lang.NoClassDefFoundError:   org / elasticsearch / action / main / MainRequest at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)     在   org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587)     ... 24更多引起:java.lang.NoClassDefFoundError:   org / elasticsearch / action / main / MainRequest at   com.indexbuilder.configuration.ElasticsearchConfiguration.restHighLevelClient(ElasticsearchConfiguration.java:95)     在   com.indexbuilder.configuration.ElasticsearchConfiguration $$ EnhancerBySpringCGLIB $$ 62f74d9a.CGLIB $ restHighLevelClient $ 1()     在   com.indexbuilder.configuration.ElasticsearchConfiguration $$ EnhancerBySpringCGLIB $$ 62f74d9a $$ FastClassBySpringCGLIB $$ 2b29ad7b.invoke()     在   org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)     在   org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)     在   com.indexbuilder.configuration.ElasticsearchConfiguration $$ EnhancerBySpringCGLIB $$ 62f74d9a.restHighLevelClient()     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)     ... 25更多引起:java.lang.ClassNotFoundException:   org.elasticsearch.action.main.MainRequest at   java.net.URLClassLoader.findClass(URLClassLoader.java:381)at   java.lang.ClassLoader.loadClass(ClassLoader.java:424)at   java.lang.ClassLoader.loadClass(ClassLoader.java:357)... 36更多

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:6)

您可能还需要核心依赖项:

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.6.3</version>
</dependency>

NoClassDefFoundError通常是配置错误 - 这意味着您使用的代码引用了某个类,但类本身并不在类路径中。在这种情况下,这也可能是相关Elasticsearch poms本身的依赖关系管理错误,因为它们应该包含所需的类 - 但除了可能提出问题之外,你可以做的事情并不多。

答案 1 :(得分:0)

我遇到了同样的问题。 ElasticSearch指向旧版本:

org.elasticsearch:elasticsearch:6.2.3 -> 1.5.2

我使用了dependencyManagement gradle插件来强制使用我提到的版本:

dependencyManagement {
dependencies {
    dependency 'org.elasticsearch:elasticsearch:6.2.3'
}}

了解更多信息:https://github.com/spring-gradle-plugins/dependency-management-plugin