千万万苦利用科学上网找到了,记录一下
package com.warn.config.baseconfig;import co.elastic.clients.elasticsearch.ElasticsearchClient;import co.elastic.clients.json.jackson.JacksonJsonpMapper;import co.elastic.clients.transport.ElasticsearchTransport;import co.elastic.clients.transport.rest_client.RestClientTransport;import com.alibaba.fastjson.JSONObject;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.common.util.text.Convert;import com.warn.entity.SettingVar;import com.warn.mapper.SettingVarMapper;import com.warn.util.EncryptUtil;import lombok.extern.slf4j.Slf4j;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.client.config.RequestConfig;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;import org.apache.http.ssl.SSLContextBuilder;import org.apache.http.ssl.SSLContexts;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.annotation.Resource;import javax.net.ssl.SSLContext;import java.io.File;/** * @author fjl * @date 2022/5/6 */@Slf4j@Configurationpublic class ElasticSearchClientConfig {@Resourceprivate SettingVarMapper varMapper;@Value("${es.connectTimeout}")private Integer connectTimeout;@Value("${es.socketTimeout}")private Integer socketTimeout;private static final Integer ES_ID = 30;@Value("${filePath}")private String filePath;private static String esInfo = "esInfo.txt";public static String esUrl;public static Integer esPort;public static String esAccount;public static String esPassword;//配置RestHighLevelClient依赖到spring容器中待用@Beanpublic ElasticsearchClient restHighLevelClient() throws Exception {log.info("--- 初始化es链接开始 --- ");EncryptUtil.checkFileExist(filePath);String esFilePath = filePath + esInfo;File esFile = new File(esFilePath);boolean esExists = esFile.exists();log.info("---检查es地址文件是否存在 --- {} esPath:{} ", esExists, esFilePath);if (!esExists) {// 获取es地址LambdaQueryWrapper<SettingVar> wrapper = new LambdaQueryWrapper<SettingVar>().eq(SettingVar::getId, ES_ID).select(SettingVar::getValue);SettingVar settingVar = varMapper.selectOne(wrapper);if (null != settingVar) {log.info("--- es地址信息{}", settingVar.getValue());String value = settingVar.getValue();getEsAddr(value);// 创建文件并写入EncryptUtil.createFile(esFilePath, value);}} else {// 存在读取赋值String value = EncryptUtil.readLine(esFilePath);getEsAddr(value);log.info("***es地址读取完成 --- {} ", value);}try {CredentialsProvider credentialsProvider =new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(esAccount, esPassword));SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(null, (x509Certificates, s) -> true);final SSLContext sslContext = sslBuilder.build();RestClientBuilder restBuilder = RestClient.builder(new HttpHost(esUrl, esPort, "https")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {@Overridepublic HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {return httpClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setDefaultCredentialsProvider(credentialsProvider);}}).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {@Overridepublic RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(120000);}});RestClient restClient = restBuilder.build();ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());ElasticsearchClient client = new ElasticsearchClient(transport);log.info("*** 初始化es链接完成 *** ");return client;} catch (Exception e) {throw new Exception("******es初始化连接失败");}}public void getEsAddr(String value) {JSONObject jsonObject = JSONObject.parseObject(value);String url = com.common.util.text.Convert.toStr(jsonObject.get("addr"));int port = com.common.util.text.Convert.toInt(jsonObject.get("port"));String account = com.common.util.text.Convert.toStr(jsonObject.get("account"));String password = Convert.toStr(jsonObject.get("password"));esUrl = url;esPort = port;esAccount = account;esPassword = password;}}