接入集群 使用RestHighLevelClient建立客户端 其中使用HttpHost类负责http请求,并在HttpHost类中将CredentialsProvider和SSLIOSessionStrategy配置参数类封装在自定义的SecuredHttpClientConfigCallback类配置请求连接参数。 SecuredHttpClientConfigCallback:封装所有自定义的连接参数。 CredentialsProvider:Elasticsearch API,主要使用Elasticsearch提供的方法封装用户名和密码。 SSLIOSessionStrategy:配置SSL相关参数,包括SSL域名验证方式、证书处理方式等。主要使用SSLContext类封装相关设置。 有两种方式连接集群:忽略证书方式和使用证书方式。 忽略所有证书,跳过证书校验环节进行连接 构造TrustManager,使用默认X509TrustManager,不重写任何方法,相当于忽略所有相关操作。 构造SSLContext:使用第一步的TrustManager为参数,默认方法构造SSLContext。 static TrustManager[] trustAllCerts new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }}; final CredentialsProvider credentialsProvider new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); SSLContext sc null; try{ sc SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); }catch(KeyManagementException e){ e.printStackTrace(); }catch(NoSuchAlgorithmException e){ e.printStackTrace(); } SSLIOSessionStrategy sessionStrategy new SSLIOSessionStrategy(sc, new NullHostNameVerifier()); SecuredHttpClientConfigCallback httpClientConfigCallback new SecuredHttpClientConfigCallback(sessionStrategy,credentialsProvider); RestClientBuilder builder RestClient.builder(new HttpHost(clusterAddress, 9200, "https")).setHttpClientConfigCallback(httpClientConfigCallback); RestHighLevelClient client new RestHighLevelClient(builder); 使用下载的证书(CloudSearchService.cer),加载证书进行连接。 上传证书到客户端,在命令行中使用keytool工具将证书转换成Java可以读取的证书格式:(keytool默认密码为changeit) keytool import alias 自命名 keystore 输出的证书路径和重命名名字 file 上传证书的路径 自定义TrustManager类,继承于X509TrustManager,读取上一步输出的证书,将其加入信任证书里,重写X509TrustManager接口的三个方法; 构造SSLContext:使用第一步的TrustManager为参数,默认方法构造SSLContext。 public static class MyX509TrustManager implements X509TrustManager { X509TrustManager sunJSSEX509TrustManager; MyX509TrustManager() throws Exception { File file new File("certification file path"); if (file.isFile() false) { throw new Exception("Wrong Certification Path"); } System.out.println("Loading KeyStore " + file + "..."); InputStream in new FileInputStream(file); KeyStore ks KeyStore.getInstance("JKS"); ks.load(in, "changeit".toCharArray()); TrustManagerFactory tmf TrustManagerFactory.getInstance("SunX509", "SunJSSE"); tmf.init(ks); TrustManager tms [] tmf.getTrustManagers(); for (int i 0; i < tms.length; i++) { if (tms[i] instanceof X509TrustManager) { sunJSSEX509TrustManager (X509TrustManager) tms[i]; return; } } throw new Exception("Couldn't initialize"); } final CredentialsProvider credentialsProvider new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); SSLContext sc null; try{ TrustManager[] tm {new MyX509TrustManager()}; sc SSLContext.getInstance("SSL", "SunJSSE"); sc.init(null, tm, new SecureRandom()); }catch (Exception e) { e.printStackTrace(); } SSLIOSessionStrategy sessionStrategy new SSLIOSessionStrategy(sc, new NullHostNameVerifier()); SecuredHttpClientConfigCallback httpClientConfigCallback new SecuredHttpClientConfigCallback(sessionStrategy,credentialsProvider); RestClientBuilder builder RestClient.builder(new HttpHost(clusterAddress, 9200, "https")) .setHttpClientConfigCallback(httpClientConfigCallback); RestHighLevelClient client new RestHighLevelClient(builder); 代码示例 代码运行时,传入3个参数,分别是 连接地址 ,集群登录用户名 和 密码 ,示例实现的请求是GET /search{"query": {"matchall": {}}}。 说明 pom文件中客户端版本需与集群版本保持一致。