|
@@ -1,18 +1,23 @@
|
|
|
package org.dbsyncer.connector.util;
|
|
|
|
|
|
-import org.dbsyncer.common.util.StringUtil;
|
|
|
import org.apache.http.HttpHost;
|
|
|
import org.apache.http.auth.AuthScope;
|
|
|
-import org.apache.http.auth.Credentials;
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
import org.apache.http.client.CredentialsProvider;
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.conn.ssl.TrustAllStrategy;
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
+import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
|
+import org.dbsyncer.common.util.StringUtil;
|
|
|
import org.dbsyncer.connector.ConnectorException;
|
|
|
import org.dbsyncer.connector.config.ESConfig;
|
|
|
+import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
import org.elasticsearch.client.RestClientBuilder;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Objects;
|
|
@@ -20,21 +25,27 @@ import java.util.Objects;
|
|
|
public abstract class ESUtil {
|
|
|
|
|
|
public static final String PROPERTIES = "properties";
|
|
|
- private static final int ADDRESS_LENGTH = 2;
|
|
|
|
|
|
private ESUtil() {
|
|
|
}
|
|
|
|
|
|
public static RestHighLevelClient getConnection(ESConfig config) {
|
|
|
String[] ipAddress = StringUtil.split(config.getUrl(), ",");
|
|
|
- HttpHost[] hosts = Arrays.stream(ipAddress).map(node -> makeHttpHost(node, config.getSchema())).filter(Objects::nonNull).toArray(
|
|
|
+ HttpHost[] hosts = Arrays.stream(ipAddress).map(node -> HttpHost.create(node)).filter(Objects::nonNull).toArray(
|
|
|
HttpHost[]::new);
|
|
|
RestClientBuilder builder = RestClient.builder(hosts);
|
|
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
- Credentials credentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
|
|
|
- credentialsProvider.setCredentials(AuthScope.ANY, credentials);
|
|
|
- builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
|
|
|
- return new RestHighLevelClient(builder);
|
|
|
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(config.getUsername(), config.getPassword()));
|
|
|
+ try {
|
|
|
+ SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustAllStrategy()).build();
|
|
|
+ SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sslContext, NoopHostnameVerifier.INSTANCE);
|
|
|
+ builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLStrategy(sessionStrategy));
|
|
|
+ final RestHighLevelClient client = new RestHighLevelClient(builder);
|
|
|
+ client.ping(RequestOptions.DEFAULT);
|
|
|
+ return client;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ConnectorException(String.format("Failed to connect to ElasticSearch on %s, %s", config.getUrl(), e.getMessage()));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static void close(RestHighLevelClient client) {
|
|
@@ -47,21 +58,4 @@ public abstract class ESUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据配置创建HttpHost
|
|
|
- *
|
|
|
- * @param address
|
|
|
- * @param scheme
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static HttpHost makeHttpHost(String address, String scheme) {
|
|
|
- String[] arr = StringUtil.split(address, ":");
|
|
|
- if (arr.length == ADDRESS_LENGTH) {
|
|
|
- String ip = arr[0];
|
|
|
- int port = Integer.parseInt(arr[1]);
|
|
|
- return new HttpHost(ip, port, scheme);
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
}
|