栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

spring集成elasticSearch es spring-boot-starter-data-elasticsearch 权限 密码配置

spring集成elasticSearch es spring-boot-starter-data-elasticsearch 权限 密码配置

本文讲述使用spring集成elasticSearch

1、引入spring-boot-starter-data-elasticsearch包

  org.springframework.boot
  spring-boot-starter-data-elasticsearch

2、添加es配置

不同的版本的配置方法不太一致。这里我呈现两个版本的配置方式

4.30版本

方法一、yml文件添加

spring:
  elasticsearch:
    uris: ["127.0.0.1:9200"]
    username: "elastic"
    password: "yuyuyu"
    

方法二、便携config文件

package com.example.esdemo;

import lombok.extern.log4j.Log4j2;
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.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration;
import org.springframework.http.HttpHeaders;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class ReactiveRestClientConfig extends AbstractElasticsearchConfiguration {
    private String host = "127.0.0.1";
    private Integer port = 9200;
    private String userName = "elastic";
    private String password = "yuyuyu";

    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .withBasicAuth(userName,password)
                .build();

        return RestClients.create(clientConfiguration).rest();
    }
}

如果是带密码的最好再yml配置下忽略将康检测。否则启动会报错(无伤大雅,非强迫症可以不理)。

// 去除es数据库健康检查
management:
  health:
    elasticsearch:
      enabled: false
4.01版本

方法一、配置yml文件

  elasticsearch:
    rest:
      uris: [ "127.0.0.1:9200" ]
      username: "elastic"
      password: "yuyuyu"

方法二、便携config文件

package com.example.esdemo;

        import lombok.extern.log4j.Log4j2;
        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.RequestOptions;
        import org.elasticsearch.client.RestClient;
        import org.elasticsearch.client.RestClientBuilder;
        import org.elasticsearch.client.RestHighLevelClient;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.data.elasticsearch.client.ClientConfiguration;
        import org.springframework.data.elasticsearch.client.RestClients;
        import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
        import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
        import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
        import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration;
        import org.springframework.http.HttpHeaders;

        import java.time.Duration;
        import java.time.LocalDateTime;
        import java.time.format.DateTimeFormatter;

@Configuration
public class ReactiveRestClientConfig extends AbstractElasticsearchConfiguration {
    private String host = "127.0.0.1";
    private Integer port = 9200;
    private String userName = "elastic";
    private String password = "yuyuyu";

    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .withBasicAuth(userName,password)
                .build();

        return RestClients.create(clientConfiguration).rest();
    }
}

如果是带密码的最好再yml配置下忽略将康检测。否则启动会报错(无伤大雅,非强迫症可以不理)。

// 去除es数据库健康检查
management:
  health:
    elasticsearch:
      enabled: false

之后有其他版本都根据下图自己摸索配置方法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672633.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号