我使用這個 docker compose 檔案在 docker 中運行 elasticsearch 8.1:
version: '3.7'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.1.0
container_name: es-node
environment:
- xpack.security.enabled=false
- discovery.type=single-node
volumes:
- ./elastic-data:/usr/share/elasticsearch/data
ports:
- 9200:9200
cap_add:
- IPC_LOCK
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65536
hard: 65536
kibana:
image: docker.elastic.co/kibana/kibana:8.1.0
container_name: kibana
environment:
- ELASTICSEARCH_HOST=http://localhost:9200
ports:
- 5601:5601
depends_on:
- elasticsearch
我正在嘗試使用 org.elasticsearch.client.RestClient 向 es 集群發出一個簡單的 GET 請求。
要求:
Request request = new Request("GET", "_cluster/health");
try {
return restClient.performRequest(request).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
休息客戶端初始化:
var hosts = buildClusterHosts(transportAddresses);
restClient = RestClient.builder(hosts).build();
var esTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
elasticsearchClient = new ElasticsearchClient(esTransport);
主要方法:
var es = ElasticEightClient.builder()
.transportAddresses("localhost:9200")
.isElasticSniffEnabled(true)
.build();
System.out.println("Started elasticsearch with health: " es.getHealth());
buildClusterHosts() 方法正確地構建了一個 HttpHost 陣列(在這種情況下只有一個)并將其提供給其余客戶端構建器。
理論上這應該足夠了,但我不斷得到原因:java.net.ConnectException:超時連接到[/172.20.0.2:9200],我不知道為什么?
uj5u.com熱心網友回復:
特長;
看來您混淆了Elasticsearch的Transport port和。Rest Api port
修理
您首先需要公開 的埠transport layer,這是9300默認情況下
services:
elasticsearch:
ports:
- 9300:9300
然后更新main方法
var es = ElasticEightClient.builder()
.transportAddresses("localhost:9200")
.isElasticSniffEnabled(true)
.build();
System.out.println("Started elasticsearch with health: " es.getHealth());
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/472212.html
標籤:爪哇 码头工人 弹性搜索 码头工人撰写 弹性搜索休息客户端
