2017-03-16 74 views
0

我試圖使用彈性數據庫附帶的彈簧數據彈性。我的elasticsearch服務器版本是5.2.2;並且xpack已啓用。也就是說,它需要用戶名和密碼才能連接到它。通過spring-data-elasticsearch連接到xpack啓用elasticsearch 5.x

的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] 

回答

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

+0

是的,你說得對。我錯過了版本衝突。然而,在這個時候,spring-data-elasticsaerch並不幸地支持elasticsearch 5.x。因此,只將elasticsearch客戶端升級到5.x並不能解決問題。我將放棄spring-data-elasticsearch並改爲使用裸機。 – Muatik

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)); 
} 

這就是全部。