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>
  • 相关阅读:
    http 请求头设置缓存
    手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单
    django如何用邮箱代替用户名登录
    python函数式编程学习之map,reduce,filter,sorted
    python traceback学习(转)
    python logging模块学习(转)
    python pip安装lxml失败(转)
    python下性能提示
    python移植性提示
    python测试与调试提示
  • 原文地址:https://www.cnblogs.com/NowShowTimeChenKang/p/11358798.html
Copyright © 2011-2022 走看看