1

我正在开发一个 Spring Boot 应用程序,它需要连接到远程 Elasticsearch 6.1 出于某种原因,当应用程序启动时,它默认为本地 ES 连接,而忽略了 settingsBuilder。

这是我的 ElasticConfiguration.java:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.techprimers.elastic.repository")
public class ElasticConfiguration {


    @Bean
    public NodeBuilder nodeBuilder() {
        return new NodeBuilder();
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws IOException {
        File tmpDir = File.createTempFile("elastic", Long.toString(System.nanoTime()));
        System.out.println("Temp directory: " + tmpDir.getAbsolutePath());
        Settings.Builder elasticsearchSettings =
                Settings.settingsBuilder()
                        .put("network.host","34.230.72.180")
                        .put("cluster.name", "my-es-cluster")
                        .put("http.enabled", "true") // 1
                        .put("index.number_of_shards", "1")
                        .put("path.data", new File(tmpDir, "data").getAbsolutePath()) // 2
                        .put("path.logs", new File(tmpDir, "logs").getAbsolutePath()) // 2
                        .put("path.work", new File(tmpDir, "work").getAbsolutePath()) // 2
                        .put("path.home", tmpDir); // 3



        return new ElasticsearchTemplate(nodeBuilder()
                .local(false)
                .settings(elasticsearchSettings.build())
                .node()
                .client());
    }
}

这是我的pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.techprimers.elastic</groupId>
    <artifactId>spring-elastic-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-elastic-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jna</groupId>
            <artifactId>jna</artifactId>
            <version>3.0.9</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

如何强制应用连接到我的远程 ES 服务器?

4

2 回答 2

1

我不知道你是否解决了你的连接问题,但是站在Spring Data Elasticsearch Github 项目页面中的兼容性矩阵上,你不能。

| spring data elasticsearch | elasticsearch | |:-----------------------------------:|:-------------:| | 3.0.0.RC2 | 5.5.0 | | 3.0.0.M4 | 5.4.0 | | 2.0.4.RELEASE | 2.4.0 | | 2.0.0.RELEASE | 2.2.0 | | 1.4.0.M1 | 1.7.3 | | 1.3.0.RELEASE | 1.5.2 | | 1.2.0.RELEASE | 1.4.4 | | 1.1.0.RELEASE | 1.3.2 | | 1.0.0.RELEASE | 1.1.1 |

该页面已过时。但是,我刚刚下载了 Spring Data Elasticsearch 的 2.1.9 版本,而我拥有的 Elasticsearch jar 是我在 2.4.6 中的 .m2 存储库。

于 2018-01-22T10:41:17.217 回答
0

您不能将 NodeBuilder 与 elasticsearch 6 一起使用,请阅读内容,使用 Node(Settings) 并将 Spring Boot 版本升级到“2.0.1.RELEASE”等。

于 2018-04-23T13:42:40.587 回答