通过spring-data-elasticsearch

时间:2017-03-16 15:33:27

标签: java elasticsearch spring-boot

我正在尝试使用带有弹簧数据弹性的弹性存储库。我的elasticsearch服务器版本是5.2.2;并且启用了xpack。也就是说,它需要用户名和密码才能连接到它。

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
    </dependency>
</dependencies>

application.yml

spring:
  data:
    elasticsearch:
      cluster-nodes: localhost:9200
      properties:
        shield:
          user: "elastic:changeme"

我找不到如何正确设置用户名和密码。我总是收到以下错误:

2017-03-16 16:54:53.222  INFO 4005 --- [           main] org.elasticsearch.client.transport       : [Fixx] failed to get node info for {#transport#-1}{127.0.0.1}{127.0.0.1:9200}, disconnecting...

org.elasticsearch.transport.ReceiveTimeoutTransportException: [][127.0.0.1:9200][cluster:monitor/nodes/liveness] request_id [0] timed out after [5006ms]
    at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:698) ~[elasticsearch-2.4.4.jar:2.4.4]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_111]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_111]

2 个答案:

答案 0 :(得分:0)

  

〜[elasticsearch-2.4.4.jar:2.4.4]

这让我相信您已经使用2.4.4客户端连接到ES 5.2.2。虽然许多API兼容,但有些API不兼容(参见breaking changes in release notes)。我会将您的客户端库升级到5.2,这里有信息:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html

答案 1 :(得分:0)

您应该使用 x-pack-transport

1,更新pom.xml

...
<!-- add the elasticsearch repo -->
<repository>
    <id>elasticsearch-releases</id>
    <url>https://artifacts.elastic.co/maven</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>x-pack-transport</artifactId>
    <version>5.2.2</version>
</dependency>
...

2,重新实例化 TransportClient

@Bean
public TransportClient transportClient() throws UnknownHostException {
    return new PreBuiltXPackTransportClient(Settings.builder()
            .put("cluster.name", "es-cluster")
            .put("xpack.security.user", "elastic:changeme")
            .build())
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
}

就是这样。