[英]Elasticsearch TransportClient connection [Java]
我在使用Maven的Java 項目中使用Elasticsearch :
...
<elasticsearch.version>6.7.0</elasticsearch.version>
...
<!-- Elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>
</dependency>
<!-- Elasticsearch -->
當我嘗試初始化 TransportClient 以索引文檔時,它給了我錯誤:
NoNodeAvailableException[None of the configured nodes are available:
[{#transport#-1}{BHMBbfcrSUOM_Pyaf1LcnA}{localhost}{127.0.0.1:9300}]]
也許需要在 config/elasticsearch.yaml 中添加更多關於 transportartion 或當前配置錯誤的信息。
爪哇代碼:
TransportAddress address;
TransportClient client;
Settings settings;
try {
address = new TransportAddress(InetAddress.getByName("localhost"), 9300);
settings = Settings
.builder()
.put("cluster.name", "lib2life")
.put("client.transport.sniff", true)
.build();
/* Initiate Transport Client */
client = new PreBuiltTransportClient(settings)
.addTransportAddress(address);
/* Verify it cluster is healthy */
ClusterHealthResponse clusterResponse = client
.admin()
.cluster()
.prepareHealth()
.setWaitForGreenStatus()
.setTimeout(TimeValue.timeValueSeconds(5))
.execute() //Error here
.actionGet();
... (more code)
}
彈性搜索.yaml:
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: lib2life
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
network.host:localhost
network.transport.tcp.port:9300
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
(elasticsearch.yaml 中的其他信息已注釋)
本地主機:9200給我:
{
"name" : "IRINAMW7",
"cluster_name" : "lib2life",
"cluster_uuid" : "-wL1-xdESnyoknD2ZqALDQ",
"version" : {
"number" : "7.1.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "7a013de",
"build_date" : "2019-05-23T14:04:00.380842Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
我解決了這個問題。 問題是我將 Elasticsearch 6.7.0 版本與 TransportClient 一起使用,該版本已被棄用並由 RestHighLevelClient 取代。 另外,我必須使用端口 9200 並取消注釋 http.port: 9200 和 discovery.seed_hosts: ["host1", "host2"] 從 elasticsearch.yaml
問題在於您的端口配置,您使用端口9300
進行 REST 調用,它實際上用於 Elasticsearch 集群中的節點間通信。 請更改以下 java 代碼以使用端口9200
。
address = new TransportAddress(InetAddress.getByName("localhost"), 9200);
請閱讀有關 es 端口的更多信息https://discuss.elastic.co/t/elasticsearch-port-9200-or-9300/72080/2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.