- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为我的 Spring Boot 项目实现一个弹性池,我也在使用 Spring Boot 2.1.4 和 Elasticsearch 7.3.0。我被困在这一点上。当任何 API 尝试查询时,它会给出 java.net.ConnectException: Connection refused
.我想在 customizeHttpClient
中使用配置具有设置线程数。因此,当应用程序启动并使用该连接查询数据库时,它只建立一个连接,直到 bean 销毁。
我试过这个 弹性配置 :
import java.io.IOException;
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.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
public class ElasticConfig{
public static String host;
private static String port;
private static String protocol;
private static String username;
private static String password;
private RestHighLevelClient client;
@Value("${dselastic.host}")
public void setHost(String value) {
host = value;
}
@Value("${dselastic.port}")
public void setPort(String value) {
port = value;
}
@Value("${dselastic.protocol}")
public void setProtocol(String value) {
protocol = value;
}
@Value("${dselastic.username}")
public void setUsername(String value) {
username = value;
}
@Value("${dselastic.password}")
public void setPassword(String value) {
password = value;
}
@Bean(destroyMethod = "cleanUp")
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public void prepareConnection() {
RestClientBuilder restBuilder = RestClient.builder(new HttpHost(host, Integer.valueOf(port), protocol));
if (username != null & password != null) {
final CredentialsProvider creadential = new BasicCredentialsProvider();
creadential.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
restBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(creadential)
.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
}
});
restBuilder.setRequestConfigCallback(requestConfigBuilder ->
requestConfigBuilder.setConnectTimeout(10000) // time until a connection with the server is established.
.setSocketTimeout(60000) // time of inactivity to wait for packets[data] to receive.
.setConnectionRequestTimeout(0)); // time to fetch a connection from the connection pool 0 for infinite.
client = new RestHighLevelClient(restBuilder);
}
}
public void cleanUp() {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DisposableBean
界面,它是
destroy
方法,但我得到了同样的异常(exception)。
public class IndexNameController {
@Autowired
RestHighLevelClient client;
@GetMapping(value = "/listAllNames")
public ArrayList<Object> listAllNames(HttpSession session) {
ArrayList<Object> results = new ArrayList<>();
try {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchRequest.indices("indexname");
String[] fields = { "name", "id" };
searchSourceBuilder.fetchSource(fields, new String[] {});
searchSourceBuilder.query(QueryBuilders.matchAllQuery()).size(10000);
searchSourceBuilder = searchSourceBuilder.sort(new FieldSortBuilder("createdTime").order(SortOrder.DESC));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit searchHit : searchHits) {
Map<String, Object> map = new HashMap<>();
map.put("value", searchHit.getSourceAsMap().get("id"));
map.put("name", searchHit.getSourceAsMap().get("name"));
results.add(map);
}
return results;
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<>();
}
}
client.search()
处给出异常.这是
堆栈跟踪 :
java.net.ConnectException: Connection refused
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:788)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1454)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:930)
at com.incident.response.controller.IncidentController.listAllIncidents(IncidentController.java:569)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvent(DefaultConnectingIOReactor.java:171)
at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:145)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:351)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:221)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64)
... 1 more
最佳答案
@Configuration
public class ElasticConfig {
@Autowired
Environment environment;
private RestHighLevelClient client;
@Bean
public RestHighLevelClient prepareConnection() {
RestClientBuilder restBuilder = RestClient
.builder(new HttpHost(environment.getProperty("zselastic.host").toString(),
Integer.valueOf(environment.getProperty("zselastic.port").toString()),
environment.getProperty("zselastic.protocol").toString()));
String username = new String(environment.getProperty("zselastic.username").toString());
String password = new String(environment.getProperty("zselastic.password").toString());
if (username != null & password != null) {
final CredentialsProvider creadential = new BasicCredentialsProvider();
creadential.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
restBuilder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(creadential)
.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
}
});
restBuilder.setRequestConfigCallback(requestConfigBuilder ->
requestConfigBuilder.setConnectTimeout(10000) // time until a connection with the server is established.
.setSocketTimeout(60000) // time of inactivity to wait for packets[data] to receive.
.setConnectionRequestTimeout(0)); // time to fetch a connection from the connection pool 0 for infinite.
client = new RestHighLevelClient(restBuilder);
return client;
}
return null;
}
/*
* it gets called when bean instance is getting removed from the context if
* scope is not a prototype
*/
/*
* If there is a method named shutdown or close then spring container will try
* to automatically configure them as callback methods when bean is being
* destroyed
*/
@PreDestroy
public void clientClose() {
try {
this.client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
RestHighLevelClient
它将被使用java.net.ConnectException: Connection refused
. http://host:port/_nodes/stats/http
.当我的 Spring Bootcurrent_open
.后current_open
中删除条目. 关于java - Spring Boot + Elasticsearch : Connection Refused with Java RestHighLevelClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62039091/
我正在尝试从需要访问外部请求代理的网络中访问Cloud Elasticsearch安装。这是我用来传递Elasticsearch凭证和我们的代理设置的代码片段: CredentialsProvider
如果elasticsearch在单一模式下运行,我可以使用以下代码轻松建立RestHighLevel连接: RestHighLevelClient client = new RestHighLevel
我想用RestHighLevelClient在不同集群上使用 Cross Cluster mechanizem 不支持的命令(例如关闭和打开索引)。 我的问题是我是否使用了多个 RestHighLev
我需要模拟 RestHighLevelClient 来测试我的代码。基本上,当我调用 RestHighLevelClient 的“搜索”方法时,我得到 UnfinishedStubbingExcept
我正在对包含空格的字段执行搜索,我希望不对其进行分析,即不将其拆分为克并被视为单个实体。我如何在 Java 中执行此操作? 我正在使用 RESTHighLevelClient 版本 7.4。 最佳答案
我正在尝试通过此依赖项使用 RestHighLevelClient org.elasticsearch.client elasticsearch-rest-h
我正在尝试在 Scala 程序中插入 ElasticSearch(ES)。 在 build.sbt 我添加了 libraryDependencies += "org.elasticsearch.cli
我正在使用vpc端点进行 flex 搜索,以使用RestHighLevelClient索引我的文档。 我正在使用Java高级休息客户端v6.2.2连接Elasticsearch 6.0版 我无需任何身
我想知道这里是否有人使用 RestHighLevelClient 连接到 AWS ElasticSearch。不确定 AWS ElasticSearch 是否支持此功能。目前,每次尝试连接时,我都会收
我正在使用 Elasticsearch RestHighLevelClient 并尝试实现下面的 sql: select format(date,'yyyy-MM-dd'), count(*) fro
我使用 MultiSearchRequest 按字段的一部分在 Elasticsearch 中进行搜索: @Override public Collection> findContractsByInd
我在 Elasticsearch 中为 RestHighLevelClient 使用以下代码。 val credentialsProvider = new BasicCredentialsProvid
当索引 100k 文档时,以下行出现 listener 超时异常 IndexResponse response = SearchEngineClient.getInstance2().index(re
我试过下面的代码它工作正常,但它使用 运输客户端 删除所有文档。 DeleteByQueryRequestBuilder deleteByQueryRequestBuilder = DeleteByQ
我们的应用程序在 elasticsearch 服务器中索引(存储)文档时遇到超时异常。它不会经常发生,但大约每天一次。这是详细信息。 pom.xml org.elasticsearch.cl
Elastic 正在更新他们的文档以使用带有 Java 的 RestHighLevelClient。它还具有映射 API: https://www.elastic.co/guide/en/elasti
我在 SpringBoot 应用程序中创建 HighEndRestClient bean 时遇到错误。我已经完成了一个测试“应用程序”,我检查了我可以实例化我想要的对象,然后进行我想要的调用,现在我正
如何在下面使用bool创建RestHighLevelClient查询? 我的尝试未返回任何内容: BoolQueryBuilder query = boolQuery()
在我的 Elasticsearch 索引中,我保存了大约 30000 个实体。我想使用 RestHighLevelClient 获取它们的所有 id。我读过最好的方法是使用滚动 api。但是,当我这样
在我的 Scala 项目中,我试图用新的 RestHighLevelClient 更改旧的 transportClient 以连接到 Elasticsearch (6.1)。 但我在尝试创建 Bulk
我是一名优秀的程序员,十分优秀!