SpringBoot 调用设置密码的 Elasticsearch

Posted BasicLab基础架构实验室

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 调用设置密码的 Elasticsearch相关的知识,希望对你有一定的参考价值。

        最近项目中使用到的Elasticsearch是设置密码的,所以我们在Springboot中使用的过程中也需要指定密码才可以连接到Elasticsearch,特此记录便于日后查阅。

        增加 bootstrap.yaml 配置:

elasticsearch:
  schema: http
  address: 127.0.0.1:9200
  connectTimeout: 5000
  socketTimeout: 5000
  connectionRequestTimeout: 5000
  maxConnectNum: 100
  maxConnectPerRoute: 100
  UserName: elastic
  Password: 123456

        修改ElasticSearchConfiguration类 

package com.zcdsj.ElasticSearch;
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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/** * @Deacription ElasticSearch 配置 * @Author x **/
@Configuration
public class ElasticSearchConfiguration  
    /** 协议 */
    @Value("$elasticsearch.schema:http")
    private String schema;
    /** 集群地址,如果有多个用“,”隔开 */
    @Value("$elasticsearch.address")
    private String address;
    /** 连接超时时间 */
    @Value("$elasticsearch.connectTimeout")
    private int connectTimeout;
    /** Socket 连接超时时间 */
    @Value("$elasticsearch.socketTimeout")
    private int socketTimeout;
    /** 获取连接的超时时间 */
    @Value("$elasticsearch.connectionRequestTimeout")
    private int connectionRequestTimeout;
    /** 最大连接数 */
    @Value("$elasticsearch.maxConnectNum")
    private int maxConnectNum;
    /** 最大路由连接数 */
    @Value("$elasticsearch.maxConnectPerRoute")
    private int maxConnectPerRoute;
    @Value("$elasticsearch.UserName")
    private String userName;
    @Value("$elasticsearch.Password")
    private String password;
    @Bean(name = "restHighLevelClient")
    public RestHighLevelClient restHighLevelClient()  
        // 拆分地址
        List<HttpHost> hostLists = new ArrayList<>();
        String[] hostList = address.split(",");
        for (String addr : hostList)  
            String host = addr.split(":")[0];
            String port = addr.split(":")[1];
            hostLists.add(new HttpHost(host, Integer.parseInt(port), schema));
        
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
        // 转换成 HttpHost 数组
        HttpHost[] httpHost = hostLists.toArray(new HttpHost[] );
        // 构建连接对象
        RestClientBuilder builder = RestClient.builder(httpHost);
        // 异步连接延时配置
        builder.setRequestConfigCallback(requestConfigBuilder ->  
            requestConfigBuilder.setConnectTimeout(connectTimeout);
            requestConfigBuilder.setSocketTimeout(socketTimeout);
            requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
            return requestConfigBuilder;
        );
        // 异步连接数配置
        builder.setHttpClientConfigCallback(httpClientBuilder ->  
            httpClientBuilder.setMaxConnTotal(maxConnectNum);
            httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        );
        return new RestHighLevelClient(builder);
    

        打开 http://127.0.0.1:8082/all/addDocument 密码访问就完成了。

以上是关于SpringBoot 调用设置密码的 Elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章

springboot怎么进行非空验证之后再进行ajax登录

springboot邮箱设置客户端专用密码怎么

Springboot+SpringSecurity设置默认密码的三种方式

您可以连接到亚马逊以外的 Amazon ElastiСache Redis 吗?

SpringBoot整合MongoDB的连接用户名和密码问题

SpringBoot整合MongoDB的连接用户名和密码问题