elasticsearch-8.x용 java api client: https 및 계정 인증을 이용한 ElasticSearchClient
public class ElasticClient {
private static Logger log = LoggerFactory.getLogger(ElasticClient.class);
private static ElasticsearchClient client = null;
private static ElasticsearchTransport transport = null;
public static synchronized ElasticsearchClient getInstacne() {
if(client == null) {
// 아이피1:9200;아이피2:9200
String nodes = "설정파일에서 읽어들인 값";
// elastic에 user/passwd 가 설정되어 있어야 함
// user/passwd설정: bin/elasticsearch-users useradd 아이디 -p 비번 -r superuser
String user = "설정파일에서 읽어들인 값";
String passwd = "설정파일에서 읽어들인 값";
// https 여부
boolean isSsl = true;
new ElasticClient().initConnect(nodes, user, passwd, isSsl);
}
return client;
}
private void initConnect(String nodes, String user, String passwd, boolean isSsl) {
try {
RestClientBuilder builder = null;
String[] clients = StringUtils.split(nodes, ";");
HttpHost[] httpHosts = new HttpHost[clients.length];
for(int i=0; i<clients.length; i++){
String[] clienthost = StringUtils.split(clients[i], ":");
if(clienthost.length > 1){
String host = clienthost[0];
int port = ImStringUtil.parseInt(clienthost[1]);
//System.out.println(host + " : " + port);
if(isSsl) {
httpHosts[i] = new HttpHost(host, port, "https");
} else {
httpHosts[i] = new HttpHost(host, port, "http");
}
}
}
if(StringUtils.isNotEmpty(user) && StringUtils.isNotEmpty(passwd)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(user, passwd));
if(isSsl) {
// FakeX509TrustManager implements X509TrustManager
TrustManager[] trustManagers = new TrustManager[] { new FakeX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new java.security.SecureRandom());
builder = RestClient.builder(httpHosts)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider);
}
});
} else {
builder = RestClient.builder(httpHosts)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider);
}
});
}
} else {
builder = RestClient.builder(httpHosts);
}
// Create the low-level client
RestClient restClient = builder.build();
// Create the transport with a Jackson mapper
transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
client = new ElasticsearchClient(transport);
}catch(Exception e) {
ElasticClient.close();
log.error("innitConnect error: {}", e);
}
}
public static void close() {
// Must close the transport
try { if(transport != null) transport.close(); }catch(Exception e) {}
try { if(client != null) client.shutdown(); }catch(Exception e) {}
}
/** Connect Test **/
public static void main(String[] args) {
ElasticsearchClient client = ElasticClient.getInstacne();
try {
System.out.println("connected");
Thread.sleep(10000);
System.out.println("bye~~");
} catch(Exception e) {
e.printStackTrace();
} finally {
ElasticClient.close();
}
}
}
'즐겁게 > elasticsearch-8' 카테고리의 다른 글
CreateIndexRequest를 이용한 Index 생성 (0) | 2022.10.06 |
---|---|
PutIndexTemplateRequest를 이용한 IndexTemplate 생성 (0) | 2022.10.05 |
elasticsearch-7.x RestHighLevelClient Connect (0) | 2022.09.29 |
elasticsearch-8.4.0 (8.x) 인증서 생성하여 설치 (0) | 2022.09.28 |
elasticsearch-8.4.0 (8.x)기본 설치 (0) | 2022.09.28 |