国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot集成Elasticsearch7.16,使用https方式連接并忽略SSL證書

這篇具有很好參考價(jià)值的文章主要介紹了springboot集成Elasticsearch7.16,使用https方式連接并忽略SSL證書。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

千萬萬苦利用科學(xué)上網(wǎng)找到了,記錄一下文章來源地址http://www.zghlxwxcb.cn/news/detail-697447.html

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
@Configuration
public class ElasticSearchClientConfig {

    @Resource
    private 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容器中待用
    @Bean
    public 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);
                // 創(chuàng)建文件并寫入
                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() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder
                                    .setSSLContext(sslContext)
                                    .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                                    .setDefaultCredentialsProvider(credentialsProvider);
                        }
                    })
                    .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                        @Override
                        public 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;
    }
}

到了這里,關(guān)于springboot集成Elasticsearch7.16,使用https方式連接并忽略SSL證書的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ElasticSearch 學(xué)習(xí)9 spring-boot ,elasticsearch7.16.1實(shí)現(xiàn)中文拼音分詞搜索

    ElasticSearch 學(xué)習(xí)9 spring-boot ,elasticsearch7.16.1實(shí)現(xiàn)中文拼音分詞搜索

    一、elasticsearch官網(wǎng)下載:Elasticsearch 7.16.1 | Elastic 二、拼音、ik、繁簡(jiǎn)體轉(zhuǎn)換插件安裝 ik分詞:GitHub - medcl/elasticsearch-analysis-ik: The IK Analysis plugin integrates Lucene IK analyzer into elasticsearch, support customized dictionary. 拼音分詞:GitHub - medcl/elasticsearch-analysis-pinyin: This Pinyin Analysis plugin is

    2024年01月22日
    瀏覽(27)
  • docker安裝ElasticSearch7.13.4、配置登錄密碼、開啟HTTPS、安裝ik分詞器

    提示: 運(yùn)行容器命令 推薦 使用單機(jī)多節(jié)點(diǎn)的命令執(zhí)行,所需配置文件要提前準(zhǔn)備

    2024年02月14日
    瀏覽(18)
  • Spring boot 2.3.12集成ElasticSearch7.6.2并進(jìn)行CRUD

    Spring boot 2.3.12集成ElasticSearch7.6.2并進(jìn)行CRUD

    本篇博客主要講解Spring boot 2.3.12集成ElasticSearch7.6.2并進(jìn)行CRUD操作。其它版本的spring boot集成ElasticSearch類似,只需要具體各自的版本是否匹配。通過本篇博客能夠成功集成ElasticSearch并進(jìn)行CRUD操作,適合剛接觸ElasticSearch需要進(jìn)行簡(jiǎn)單CRUD操作的讀者。 在集成ElasticSearch之前需要明

    2023年04月08日
    瀏覽(21)
  • Springboot實(shí)戰(zhàn)16 消息驅(qū)動(dòng):如何使用 RabbitTemplate 集成 RabbitMQ?

    15 講我們介紹了基于 ActiveMQ 和 JmsTemplate 實(shí)現(xiàn)消息發(fā)送和消費(fèi),并重構(gòu)了 SpringCSS 案例系統(tǒng)中的 account-service 和 customer-service 服務(wù)。 今天,我們將介紹另一款主流的消息中間件 RabbitMQ,并基于 RabbitTemplate 模板工具類為 SpringCSS 案例添加對(duì)應(yīng)的消息通信機(jī)制。 AMQP 規(guī)范與 RabbitM

    2024年02月02日
    瀏覽(30)
  • SpringBoot3 整合 ElasticSearch7 示例

    SpringBoot3 整合 ElasticSearch7 示例

    做仿牛客項(xiàng)目需要使用 es 做搜索,但是老師示例的是 SpringBoot2 + es6 去做的,然而我用的是 Spring3 + es7.17.10,于是踩了很多的坑。 在 es7 中,配置文件和查詢所需的實(shí)現(xiàn)類都做了很大的改動(dòng),我以能成功運(yùn)行的代碼為例,大概說一下怎么配置和使用。 首先 yml 配置文件發(fā)生了變

    2024年02月07日
    瀏覽(29)
  • 【SpringBoot整合ElasticSearch7.x及實(shí)戰(zhàn)】

    【SpringBoot整合ElasticSearch7.x及實(shí)戰(zhàn)】

    此筆記內(nèi)容為狂神說SpringBoot整合ElasticSearch部分 目錄 一、SpringBoot整合 1、創(chuàng)建工程 2、導(dǎo)入依賴 導(dǎo)入elasticsearch依賴 提前導(dǎo)入fastjson、lombok 3、創(chuàng)建并編寫配置類 4、創(chuàng)建并編寫實(shí)體類 5、測(cè)試 索引的操作 文檔的操作 二、ElasticSearch實(shí)戰(zhàn) 防京東商城搜索(高亮) 1、工程創(chuàng)建(

    2024年02月08日
    瀏覽(18)
  • elasticsearch使用SpringBoot 連接Https問題

    elasticsearch使用SpringBoot 連接Https問題

    SpringBoot整合elasticsearch初探: ?導(dǎo)入相關(guān)的jar包。然后在配置文件中配置es的相關(guān)屬性: 然后新建一個(gè)客戶端工具類,將與es交互的所有基礎(chǔ)操作寫進(jìn)工具類中,工具類注入連接es的客戶端: ?client直接注入就好,因?yàn)镾pringBoot已經(jīng)將客戶端自動(dòng)注冊(cè)到Spring容器中了。 客戶端嘗

    2024年02月14日
    瀏覽(18)
  • SpringBoot集成elasticsearch使用(增刪改查)

    SpringBoot集成elasticsearch使用(增刪改查)

    提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔 Elasticsearch 是一個(gè)實(shí)時(shí)的分布式存儲(chǔ)、搜索、分析的引擎。(全文引擎) 系統(tǒng) :windows 10 elasticsearch官網(wǎng)下載地址鏈接: 下載好對(duì)應(yīng)的windows版本,解壓到任意工作目錄,es的安裝非常方便,解壓即用。 剛下載

    2024年02月06日
    瀏覽(25)
  • Java + SpringBoot 操作 ElasticSearch7.x.x工具類RestHighLevelClientUtils

    ElasticSearch創(chuàng)建索引,刪除索引,判斷 index 是否存在,根據(jù) id 刪除指定索引中的文檔, 根據(jù) id 更新指定索引中的文檔,根據(jù) id 更新指定索引中的文檔,根據(jù)某字段的 k-v 更新索引中的文檔, 添加文檔 手動(dòng)指定id,簡(jiǎn)單模糊匹配 默認(rèn)分頁為 0,10, term 查詢 精準(zhǔn)匹配,term 查詢

    2024年02月13日
    瀏覽(20)
  • python使用ElasticSearch7.17.6筆記

    python使用ElasticSearch7.17.6筆記

    數(shù)操作系統(tǒng):windows10 我開始使用最新的版本,8.4.1但是使用過程中kibana啟動(dòng)不了,就索性使用舊版; 下載地址: es7.17.6 下載地址? kibana7.17.6下載地址 解壓到合適的位置,更改elasticsearch.yml 添加配置如下: 更改kibana.yml配置 到各自的bin目錄下啟動(dòng)兩個(gè)服務(wù)bat文件, 在瀏覽器中

    2024年02月07日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包