Я прошу помощи сообщества, потому что у меня возникли проблемы с запросом базы данных ElasticSearch со специальным перенаправленным URL.
Не могли бы вы помочь мне в этом?
Большое спасибо.
Сначала давайте начнем с хорошего случая.
Предположим, у меня есть этот стандартный URL-адрес ElasticSearch: http://192.168.75.197: 9200
С документацией ElasticSearch : https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_changing_the_client_8217_s_initialization_code.html
Я могу запросить его с помощью этого Java кода:
package com.acme.elasticsearch;
import java.net.URL;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
public class TestRedirect {
private static final String rawURL = "http://192.168.75.197:9200";
private static final String index = "demoindex02";
private static final String id = "Zw6AXnEB8ovVkXyeZ9wF";
public static void main(String[] args) throws Exception {
// parse URL
System.out.println("URL = " + rawURL);
URL parsedHttpUrl = new URL(rawURL);
String protocol = parsedHttpUrl.getProtocol();
String host = parsedHttpUrl.getHost();
int port = parsedHttpUrl.getPort();
String path = parsedHttpUrl.getPath();
System.out.println("protocol = " + protocol);
System.out.println("host = " + host);
System.out.println("port = " + port);
System.out.println("path = " + path);
// instantiate HTTP credentials
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(Credential.user, Credential.password));
// instantiate REST high level client
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, protocol));
restClientBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
// simple get request
GetRequest getRequest = new GetRequest(index, id);
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println("getResponse = " + getResponse);
}
}
Я вижу в своей консоли прекрасный результат:
URL = http://192.168.75.197:9200
protocol = http
host = 192.168.75.197
port = 9200
path =
getResponse = {"_index":"demoindex02","_type":"_doc","_id":"Zw6AXnEB8ovVkXyeZ9wF","_version":1,"_seq_no":16,"_primary_term":1,"found":true,"_source":{"id":"1","firstName":"Jason","lastName":"GIBBS","title":"Mister","company":"Mister","phones":["(413)442-5250","(413)442-5252","(413)454-5663"]}}
Теперь возникает проблема: URL-адрес ElasticSearch перенаправляется (например, перед Apache, Nginx или F5 ...)
URL становится: http://192.168.75.197: 8060 / v1 / es /
Этот новый URL действителен:
1. изменить только URL
Я адаптирую свой код java только для нового URL (клиент и запрос остаются прежними):
private static final String rawURL = "http://192.168.75.197:8060/v1/es/";
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, protocol));
GetRequest getRequest = new GetRequest(index, id);
Проблема: 404 Not Found
Exception in thread "main" ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ElasticsearchStatusException[Unable to parse response body]; nested: ResponseException[method [GET], host [http://192.168.75.197:8060], URI [/demoindex02/_doc/Zw6AXnEB8ovVkXyeZ9wF], status line [HTTP/1.1 404 Not Found]
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
];
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1686)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1443)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1403)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1373)
at org.elasticsearch.client.RestHighLevelClient.get(RestHighLevelClient.java:699)
at com.acme.elasticsearch.TestRedirect.main(TestRedirect.java:52)
Suppressed: java.lang.IllegalStateException: Unsupported Content-Type: text/html
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1703)
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1683)
... 5 more
Caused by: org.elasticsearch.client.ResponseException: method [GET], host [http://192.168.75.197:8060], URI [/demoindex02/_doc/Zw6AXnEB8ovVkXyeZ9wF], status line [HTTP/1.1 404 Not Found]
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:260)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:238)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1433)
... 4 more
2. изменить только URL и клиента
Я адаптирую свой код java только для нового URL и добавляю путь к клиенту (запрос остается прежним):
private static final String rawURL = "http://192.168.75.197:8060/v1/es/";
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host + path, port, protocol));
GetRequest getRequest = new GetRequest(index, id);
Проблема заключается в следующем: Неизвестный хост 192.168.75.197/v1/es/
URL = http://192.168.75.197:8060/v1/es/
protocol = http
host = 192.168.75.197
port = 8060
path = /v1/es/
Exception in thread "main" java.io.IOException: 192.168.75.197/v1/es/
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:809)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:225)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1433)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1403)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1373)
at org.elasticsearch.client.RestHighLevelClient.get(RestHighLevelClient.java:699)
at com.acme.elasticsearch.TestRedirect.main(TestRedirect.java:52)
Caused by: java.net.UnknownHostException: 192.168.75.197/v1/es/
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324)
at java.net.InetAddress.getAllByName0(InetAddress.java:1277)
at java.net.InetAddress.getAllByName(InetAddress.java:1193)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:664)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:635)
at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:474)
at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:280)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:295)
at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:377)
at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:129)
at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:141)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:221)
... 6 more
3. изменить URL, клиент и запрос
Я адаптирую свой код java только для нового URL, добавлю путь к клиенту и запросу:
private static final String rawURL = "http://192.168.75.197:8060/v1/es/";
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host + path, port, protocol));
GetRequest getRequest = new GetRequest(path + index, id);
Проблема точно такая же, как в пункте № 2 выше: Неизвестный хост 192.168.75.197/v1/es/
4. изменить только URL и запрос
Я адаптирую свой код java только для нового URL и добавляю путь к запросу (клиент остается прежним):
private static final String rawURL = "http://192.168.75.197:8060/v1/es/";
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(host, port, protocol));
GetRequest getRequest = new GetRequest(path + index, id);
Проблема точно такая же, как в пункте № 1 выше: 404 Не найдено