elasticsearch 클라이언트 탐지기
5971 단어 elasticsearch
6.2.3
org.elasticsearch
elasticsearch
${elasticsearch.version}
org.elasticsearch.client
elasticsearch-rest-high-level-client
${elasticsearch.version}
org.elasticsearch.client
elasticsearch-rest-client
${elasticsearch.version}
org.elasticsearch.client
elasticsearch-rest-client-sniffer
${elasticsearch.version}
클라이언트 탐지기 코드
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.sniff.SniffOnFailureListener;
import org.elasticsearch.client.sniff.Sniffer;
public class EsClientFactory {
private String host = "127.0.0.1";
private String scheme="http";
private int port=9200;
private int maxRetryTimeout=30000;
private int connectTimeOut = 2000;
private int socketTimeOut = 15000;
private int connectionRequestTimeOut = 500;
private int maxConnectNum = 100;
private int maxConnectPerRoute = 100;
private HttpHost[] httpHosts;
private boolean uniqueConnectTimeConfig = true;
private boolean uniqueConnectNumConfig = true;
private boolean enableSniff=true;
private RestClientBuilder builder;
private Sniffer sniffer = null;
private RestClient restClient = null;
private volatile RestHighLevelClient highClient;
private static volatile EsClientFactory esClientFactory;
private EsClientFactory() {
}
public static EsClientFactory getInstance() {
if (esClientFactory == null) {
synchronized (EsClientFactory.class) {
if (esClientFactory == null) {
esClientFactory = new EsClientFactory();
esClientFactory.init();
}
}
}
return esClientFactory;
}
/**
* builder
* @return
*/
public RestClientBuilder init() {
String[] hosts = host.split(",");
httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < hosts.length; i++) {
httpHosts[i] = new HttpHost(hosts[i], port, scheme);
}
builder = RestClient.builder(httpHosts);
Header[] defaultHeaders = new Header[] {
new BasicHeader("Content-Type", "application/json")
};
builder.setDefaultHeaders(defaultHeaders);
builder.setMaxRetryTimeoutMillis(maxRetryTimeout);
if (uniqueConnectTimeConfig) {
setConnectTimeOutConfig();
}
if (uniqueConnectNumConfig) {
setMutiConnectConfig();
}
restClient = builder.build();
//
if(enableSniff){
SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener();
builder.setFailureListener(sniffOnFailureListener);
sniffer = Sniffer.builder(restClient).setSniffIntervalMillis(60000).setSniffAfterFailureDelayMillis(30000).build();
sniffOnFailureListener.setSniffer(sniffer);
}
return builder;
}
/**
* api
* @return
*/
public RestHighLevelClient getHighLevelClient() {
if (highClient == null) {
synchronized (EsClientFactory.class) {
if (highClient == null) {
highClient = new RestHighLevelClient(builder);
}
}
}
return highClient;
}
/**
* api
* @return
*/
public RestClient getLowLevelClient() {
return restClient;
}
/**
* httpclient
*/
public void setConnectTimeOutConfig() {
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
}
/**
* httpclient
*/
public void setMutiConnectConfig() {
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
return httpClientBuilder;
}
});
}
/**
*
*/
public void close() {
IOUtils.closeQuietly(restClient);
IOUtils.closeQuietly(highClient);
}
ES 고급 및 하위 클라이언트 가져오기
/**
* api
* @return
*/
public static RestHighLevelClient getEsHighClient() {
return EsClientFactory.getInstance().getHighLevelClient();
}
/**
* api
* @return
*/
public static RestClient getEsLowClient() {
return EsClientFactory.getInstance().getLowLevelClient();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
kafka connect e elasticsearch를 관찰할 수 있습니다.No menu lateral do dashboard tem a opção de connectors onde ele mostra todos os clusters do kafka connect conectados atu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.