其他分享
首页 > 其他分享> > docker容器内elasticsearch安装

docker容器内elasticsearch安装

作者:互联网

                                           一、elasticsearch 安装

docker 启动ES: docker run -d --name es2 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" f29a1ee41030

docker 安装中文分词器

docker exec -it es  /bin/bash

elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip

重启容器

docker restart es

                                            二、elasticsearch7 设置xpack账号密码---整合springboot

1、进入容器&打开文件

   docker exec -it es /bin/bash或 docker exec -it es bash  

    cd config

    vi elasticsearch.yml

  2、编辑文件

      http.cors.enabled: true

      http.cors.allow-origin: "*"

      http.cors.allow-headers: Authorization

      xpack.security.enabled: true

      xpack.security.transport.ssl.enabled: true

      记得重启 es

  3、设置密码   

     cd bin

        elasticsearch-setup-passwords interactive

   4、退出&重启

      exit

      docker restart es

5、进入kibana容器中&打开文件

   docker exec -it kibana /bin/bash

   cd config

   vi kibana.yml

配置:

server.name: kibana

server.host: "0.0.0.0"

elasticsearch.hosts: [ "http://{IpAddress}:9200" ]

monitoring.ui.container.elasticsearch.enabled: true

elasticsearch.username: "elastic"

elasticsearch.password: "password"

i18n.locale: "zh-CN"

查看es容器的ip地址

docker inspect --format='{{.NetworkSettings.IPAddress}}' es

 

参考博客:https://www.cnblogs.com/lwc1st/p/14060309.html

 

                三、elasticsearch第一次访问ES服务器超时

                     elasticsearch 放置一段时间后 报30,000 milliseconds timeout on connection http-ou tgoing-2 [ACTIVE]

              长时间停止ES访问后,与es服务器建立的HTTP连接死掉,需要重新建立连接

  builder.setHttpClientConfigCallback(httpClientBuilder -> {

        httpClientBuilder.setMaxConnTotal(MAX_CONN_TOTAL);

        httpClientBuilder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE);

        httpClientBuilder.setKeepAliveStrategy((response, context) ->        Duration.ofMinutes(5).toMillis());

        httpClientBuilder.disableAuthCaching();

        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

        return httpClientBuilder;

    });

         增加:
            httpClientBuilder.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
            配置,保持client 每 5分钟 发送数据保持http存活

 

       或者

 

@Configuration

public class ElasticSearchConfig {

 

    @Value("${spring.elasticsearch.cluster-nodes}")

    private String hostname;

 

    @Value("${spring.elasticsearch.cluster-port}")

    private int port;

 

    @Value("${spring.elasticsearch.username}")

    private String userName;

 

    @Value("${spring.elasticsearch.password}")

    private String password;

 

//  @Bean

//  public RestHighLevelClient restHighLevelClient() {

//      RestHighLevelClient client = new RestHighLevelClient(

//              RestClient.builder(new HttpHost("47.113.206.200", 9200, "http")));

//      return client;

//  }

 

    @Bean

    public RestHighLevelClient restHighLevelClient() {

        /* 用户认证对象 */

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        /* 设置账号密码 */

        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

        /* 创建rest client对象 */

        RestClientBuilder builder = RestClient.builder(new HttpHost(hostname, port))

                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

                    @Override

                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {

                        // elasticsearch第一次访问ES服务器超时,保持client 每 5分钟 发送数据保持http存活

                        httpAsyncClientBuilder

                                .setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());

                        return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

                    }

                });

        RestHighLevelClient client = new RestHighLevelClient(builder);

        return client;

    }

}

 

                         四、elasticsearch中max_result_window有上限限制

    PUT index/_settings

        {

        "index":{

         "max_result_window":100000000

            }

        }

 

                         Elasticsearch 查询10000条限制与totalhits值最大10000的解决

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().trackTotalHits(true);

相关资料

https://www.jianshu.com/p/de838a665eec

 

 

标签:容器,http,httpClientBuilder,elasticsearch,new,docker,es
来源: https://blog.csdn.net/yechangfeng47/article/details/114551795