- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我请求社区帮助,因为我在请求具有特殊重定向 URL 的 ElasticSearch 数据库时遇到一些问题。
您能帮忙解决一下吗?
非常感谢。
首先,让我们从一个很好的案例开始。
假设我有这个标准 ElasticSearch URL: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"]}}
现在问题来了:ElasticSearch URL 被重定向(例如,在 Apache、Nginx 或 F5 前面......)
URL 变为:http://192.168.75.197:8060/v1/es/
这个新网址有效:
<强>1。仅更改 URL
我只使用新的 URL 来调整我的 Java 代码(客户端和请求保持不变):
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 和客户端
我用新的 URL 调整我的 java 代码并添加客户端路径(请求保持不变):
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、客户端和请求
我用新的 URL 调整我的 java 代码,添加客户端和请求的路径:
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 和请求
我只使用新的 URL 来调整我的 Java 代码并添加请求路径(客户端保持不变):
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 Not Found
最佳答案
最后,答案在这里:RestHighLevelClient - Accessing an elastic http endpoint behind reverse proxy
所以我这样修改了我的代码:
// 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);
}
});
restClientBuilder.setPathPrefix(path);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
关于java - 通过重定向 URL 的 ElasticSearch REST HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61138183/
我正在尝试使用谷歌浏览器的 Trace Event Profiling Tool分析我正在运行的 Node.js 应用程序。选择点样本后,我可以在三种 View 之间进行选择: 自上而下(树) 自上而
对于一个可能是菜鸟的问题,我们深表歉意,但尽管在 SO 上研究了大量教程和其他问题,但仍找不到答案。 我想做的很简单:显示一个包含大量数据库存储字符串的 Android ListView。我所说的“很
我已经开始了一个新元素的工作,并决定给 Foundation 5 一个 bash,看看它是什么样的。在创建带有水平字段的表单时,我在文档中注意到的第一件事是它们使用大量 div 来设置样式。所以我在下
我有一个 Windows 窗体用户控件,其中包含一个使用 BeginInvoke 委托(delegate)调用从单独线程更新的第 3 方图像显示控件。 在繁重的 CPU 负载下,UI 会锁定。当我附加
我有一堆严重依赖dom元素的JS代码。我目前使用的测试解决方案依赖于 Selenium ,但 AFAIK 无法正确评估 js 错误(addScript 错误不会导致您的测试失败,而 getEval 会
我正在制作一款基于滚动 2D map /图 block 的游戏。每个图 block (存储为图 block [21][11] - 每个 map 总共 231 个图 block )最多可以包含 21 个
考虑到以下情况,我是前端初学者: 某个 HTML 页面应该包含一个沉重的图像(例如 - 动画 gif),但我不想强制客户缓慢地等待它完全下载才能享受一个漂亮的页面,而是我更愿意给他看一个轻量级图像(例
我正在设计一个小软件,其中包括: 在互联网上获取资源, 一些用户交互(资源的快速编辑), 一些处理。 我想使用许多资源(它们都列在列表中)来这样做。每个都独立于其他。由于编辑部分很累,我想让用户(可能
我想比较两个理论场景。为了问题的目的,我简化了案例。但基本上它是您典型的生产者消费者场景。 (我关注的是消费者)。 我有一个很大的Queue dataQueue我必须将其传输给多个客户端。 那么让我们
我有一个二元分类问题,标签 0 和 1(少数)存在巨大不平衡。由于测试集带有标签 1 的行太少,因此我将训练测试设置为至少 70-30 或 60-40,因此仍然有重要的观察结果。由于我没有过多地衡量准
我是一名优秀的程序员,十分优秀!