zoukankan      html  css  js  c++  java
  • Elasticsearch连接类(带密码)

    /**
     * 获取ES连接类
     *
     * @author 陈康
     * @description
     * @create 2019/08/15
     **/
    @Component("ElasticsearchRestClient")
    @Configuration
    public class ElasticsearchRestClient {
    
        private static String host = "你的es地址";
    
        private static  int port = 9200;
    
        private static String userName = "你的账号";
    
        private static String passWord = "你的密码";
        
        public static RestClient getRestClient() {
            //初始化ES操作客户端
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(userName, passWord));  //es账号密码
            esClient = new RestHighLevelClient(
                RestClient.builder(
                    new HttpHost(host, port)
                ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        httpClientBuilder.disableAuthCaching();
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                })
            );
            return esClient.getLowLevelClient();
        }
    
    }

    我使用的ES版本是6.3.2的所有使用的pom.xml是(注意:使用的版本一定要与你的ES版本相同!

    <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.elasticsearch</groupId>
                        <artifactId>elasticsearch</artifactId>
                    </exclusion>
                </exclusions>
                <version>6.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>6.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>6.3.2</version>
            </dependency>
  • 相关阅读:
    centos8.2安装nginx
    Centos8安装PostgreSQL
    PostgreSQL 安装时提示下载元数据失败
    MySQL8定时备份
    Centos8安装Docker
    Centos8安装中文字体
    Centos8源码安装libgdiplus
    MySQL拖拽排序
    MySQL8修改事务隔离级别
    MySQL启动问题
  • 原文地址:https://www.cnblogs.com/NowShowTimeChenKang/p/11358798.html
Copyright © 2011-2022 走看看