【问题标题】:Elasticsearch not working after spring-boot upgrade to 2.2.0.RELEASE在 spring-boot 升级到 2.2.0.RELEASE 后 Elasticsearch 不工作
【发布时间】:2021-12-22 18:58:17
【问题描述】:

我正在将我们的一个应用程序从 spring-boot 版本 2.1.3.RELEASE 升级到 2.2.0.RELEASE,但我在使用 Elasticsearch 时遇到了一些问题。这是我得到的错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customCdrElasticConfig': Unsatisfied dependency expressed through field 'elasticsearchTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=elasticsearchTemplate)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.elasticsearch.core.ElasticsearchTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=elasticsearchTemplate)}

代码:

package com.codex.reporting.config;

import com.codex.reporting.elasticsearch.service.impl.CustomElasticsearchTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@Configuration
public class CustomCdrElasticConfig {

    @Autowired
    @Qualifier("elasticsearchTemplate")
    private ElasticsearchTemplate elasticsearchTemplate;

    @Bean
    public CustomElasticsearchTemplate customElasticsearchTemplate(){
        return new CustomElasticsearchTemplate(elasticsearchTemplate.getClient());
    }
}

pom.xml

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>6.8.13</version>
        </dependency>
        <!-- Dependency for high level rest client, for Rest Client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>6.8.13</version>
        </dependency>
        <!-- For Rest High Level Client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.8.13</version>
        </dependency>
        <!-- Dependency for high level rest client, for elasticsearch Core -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch-core</artifactId>
            <version>6.8.13</version>
        </dependency>

【问题讨论】:

  • 我在 Spring Boot 项目的 POM 文件中检查了 here,并验证了 6.8.13 是使用 Spring Boot 2.2.x 时 ElasticSearch 的正确版本。
  • "ElasticsearchTemplate" (org.springframework.data.elasticsearch.core.ElasticsearchTemplate) 是 Spring Data Elasticsearch 的一部分,我在您共享的 pom 中看不到这种依赖关系。
  • @Shailendra :抱歉错过了之前在问题中添加的内容,它已经存在于 pom.xml 中。我使用的是 3.2.3.RELEASE 版本。

标签: spring-boot elasticsearch spring-data-elasticsearch


【解决方案1】:

您可能面临this 问题。

我正在尝试测试并有以下 pom - 根据您的构建文件进行调整

    <name>demo</name>
    <description>Spring Boot ElasticSearch</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath />
    </parent>

   <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
                <version>3.2.3.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>x-pack-transport</artifactId>
                <version>6.8.13</version>
            </dependency>
            <!-- Dependency for high level rest client, for Rest Client -->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>6.8.13</version>
            </dependency>
            <!-- For Rest High Level Client -->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.8.13</version>
            </dependency>
            <!-- Dependency for high level rest client, for elasticsearch Core -->
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch-core</artifactId>
                <version>6.8.13</version>
            </dependency>
    </dependencies>

我的服务类看起来像这样

@Service
public class TestService {
    
    
    @Autowired
    @Qualifier("elasticsearchTemplate")
    private ElasticsearchTemplate elasticsearchTemplate;
    
    public void ping() {
        System.out.println(elasticsearchTemplate.getClient().settings().names());
        
    }

}

以及 application.properties 中的以下属性

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9200

有了这个 - 应用程序启动正常,这意味着 elasticsearchTemplate 自动连接正确

2021-11-11 00:06:03.767  INFO 24304 --- [           main] com.test.elasticsearch.demo.Application  : No active profile set, falling back to default profiles: default
2021-11-11 00:06:06.408  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2021-11-11 00:06:06.445  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 24ms. Found 0 repository interfaces.
2021-11-11 00:06:06.460  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2021-11-11 00:06:06.481  INFO 24304 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20ms. Found 0 repository interfaces.
2021-11-11 00:06:08.810  INFO 24304 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-11-11 00:06:08.830  INFO 24304 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-11 00:06:08.831  INFO 24304 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2021-11-11 00:06:09.279  INFO 24304 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-11-11 00:06:09.280  INFO 24304 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5432 ms
2021-11-11 00:06:11.090  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : no modules loaded
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2021-11-11 00:06:11.092  INFO 24304 --- [           main] o.elasticsearch.plugins.PluginsService   : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2021-11-11 00:06:14.516  INFO 24304 --- [           main] o.s.d.e.c.TransportClientFactoryBean     : Adding transport node : 127.0.0.1:9200
2021-11-11 00:06:48.707  INFO 24304 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2023-02-04
    • 2021-09-06
    • 2022-01-06
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多