Подтвердить что ты не робот

Невозможно использовать настраиваемую автоконфигурацию с помощью spring -data-elasticsearch

Я не могу получить spring -data-elasticsearch, работающий для версий Elasticsearch 2.0+.

При использовании следующего POM

<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.project.treasure</groupId>
    <artifactId>search</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>
    <inceptionYear>2015</inceptionYear>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.project.treasure.search.application.Application</start-class>
        <java.version>1.8</java.version>
        <elasticsearch.version>2.2.0</elasticsearch.version>
        <springdata.commons>1.12.0.BUILD-SNAPSHOT</springdata.commons>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.project.treasure.search</groupId>
            <artifactId>search-client</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- Json format -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3</version>
        </dependency>
        <!-- Kafka dependencies -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.10</artifactId>
            <version>0.8.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.9.0.0</version>
        </dependency>
    </dependencies>
        <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>conf/*.properties</exclude>
                </excludes>
            </resource>
        </resources>
        <finalName>ROOT</finalName>
    </build>
</project>

И добавление пользовательской настройки ElasticsearchConfiguration

package com.package.project.module.application;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import java.net.InetSocketAddress;

import javax.annotation.Resource;

@Configuration
@PropertySources({
        @PropertySource(value = "classpath:elasticsearch.properties", ignoreResourceNotFound = true),
        @PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = true) })
@EnableElasticsearchRepositories(basePackages = "com.package.project.module.repository")
public class ElasticsearchConfiguration {
    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        Settings settings = Settings.settingsBuilder().put("cluster.name", environment.getProperty("cluster.name")).build();
        Client client = TransportClient.builder().settings(settings).build();
        String[] addresses = environment.getProperty("cluster.nodes").split(",");
        for (int i = 0; i < addresses.length; i++) {
            System.out.println(addresses[i].trim());
            TransportAddress address = new InetSocketTransportAddress(new InetSocketAddress(addresses[i].trim(), Integer.parseInt(environment.getProperty("elasticsearch.port"))));
            ((TransportClient) client).addTransportAddress(address);
        }
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

В следующем разделе Application.java

@Configuration
@ComponentScan(basePackages = "com.package.project.module")
@EnableAutoConfiguration(exclude = {ElasticsearchConfiguration.class})
public class Application extends SpringBootServletInitializer {

Я по-прежнему получаю elasticsearch 1.5.2 (вместе с дополнительной версией 2.0.1.RELEASE) как зависимость от моего эффективного POM.

Получение следующей ошибки при запуске приложения.

java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration.elasticsearchClient
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:178)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:140)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
at com.snapdeal.treasure.search.application.Application.main(Application.java:20)
Caused by: java.lang.IllegalArgumentException: @ConditionalOnMissingBean annotations must specify at least one bean (type, name or annotation)
at org.springframework.util.Assert.isTrue(Assert.java:68)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.validate(OnBeanCondition.java:279)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.<init>(OnBeanCondition.java:275)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:111)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
... 17 common frames omitted

Я следую инструкциям, указанным в spring -data-elasticsearch github repo и docs.

4b9b3361

Ответ 1

Проблема заключается в том, что spring-boot 1.3.2.RELEASE пока не работает с spring-data-elasticsearch 2.0.0-RELEASE.

spring-boot 1.3.2 будет включать spring-boot-starter-data-elasticsearch 1.3.2-RELEASE, который все еще зависит от elasticsearch 1.5.2

ES 2.0 будет поддерживаться как spring-boot 1.4.0-RELEASE, который еще не выпущен (но вы можете попробовать с 1.4.0.BUILD-SNAPSHOT, чтобы убедиться, что это помогает)

Поскольку вы также явно включаете spring-data-elasticsearch 2.0.0-RELEASE, вы получаете 2.0 версию elasticsearch, но вы должны удалить эту зависимость, поскольку она управляется самой spring-boot-starters

Ответ 2

Вам нужно будет подождать spring boot 1.4.x для использования elasticsearch v2.2.2

Spring -boot branch 1.3.x все еще использует elasticsearch v1.5.2.


попробуйте изменить свой pom и измените:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.BUILD-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

и добавьте:

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

Ответ 3

Пожалуйста, выполните следующие шаги

  • Удалить следующую зависимость от вашего POM

        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>2.0.0.RELEASE</version>

  • Добавьте следующую зависимость spring -boot dependency

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.3.5.RELEASE</version>