elasticsearch Connection reset by peer如何处理

如何处理:
代码的心跳代码删除,服务linux内核参数修改 客户端时间要小于服务端时间



#异常代码
public RestHighLevelClient elasticsearchClient() {
        // 初始化 RestClient, hostName 和 port 填写集群的内网 IP 地址与端口
//        String[] hosts = nodes1.split(",");
//        HttpHost[] httpHosts = new HttpHost[hosts.length];
//        for (int i = 0; i < hosts.length; i++) {
//            httpHosts[i] = new HttpHost(hosts[i], port1, "http");
//        }
        String[] hosts = uris.split(":");
        HttpHost[] httpHosts = new HttpHost[1];
        httpHosts[0] = new HttpHost(hosts[0], Integer.valueOf(hosts[1]), "http");
        // 设置认证信息
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        RestClientBuilder builder = RestClient.builder(httpHosts);
        //异步链接延时配置
        builder.setRequestConfigCallback(requestConfigBuilder ->
                requestConfigBuilder
                        .setConnectTimeout(5000) //5秒
                        .setSocketTimeout(5000 * 1000)
                        .setConnectionRequestTimeout(5000 * 1000));
        //异步链接数配置
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            //最大连接数100个
            httpClientBuilder.setMaxConnTotal(100);
            //最大路由连接数
            httpClientBuilder.setMaxConnPerRoute(100);
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            // 设置KeepAlive为5分钟的时间,不设置默认为-1,也就是持续连接,然而这会受到外界的影响比如Firewall,会将TCP连接单方面断开,从而会导致Connection reset by peer的报错
            
httpClientBuilder.setKeepAliveStrategy((response, context) -> TimeUnit.MINUTES.toMillis(3))
.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).setSoKeepAlive(true).build());
        return new RestHighLevelClient(builder);
    }

正确代码:

 public RestHighLevelClient elasticsearchClient() {
        // 初始化 RestClient, hostName 和 port 填写集群的内网 IP 地址与端口
//        String[] hosts = nodes1.split(",");
//        HttpHost[] httpHosts = new HttpHost[hosts.length];
//        for (int i = 0; i < hosts.length; i++) {
//            httpHosts[i] = new HttpHost(hosts[i], port1, "http");
//        }
        String[] hosts = uris.split(":");
        HttpHost[] httpHosts = new HttpHost[1];
        httpHosts[0] = new HttpHost(hosts[0], Integer.valueOf(hosts[1]), "http");
        // 设置认证信息
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        RestClientBuilder builder = RestClient.builder(httpHosts);
        //异步链接延时配置
        builder.setRequestConfigCallback(requestConfigBuilder ->
                requestConfigBuilder
                        //.setConnectTimeout(5000) //5秒
                        //.setSocketTimeout(5000 * 1000)
                        .setConnectionRequestTimeout(5000 * 1000));
        //异步链接数配置
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            //最大连接数100个
            httpClientBuilder.setMaxConnTotal(100);
            //最大路由连接数
            httpClientBuilder.setMaxConnPerRoute(100);
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            // 设置KeepAlive为5分钟的时间,不设置默认为-1,也就是持续连接,然而这会受到外界的影响比如Firewall,会将TCP连接单方面断开,从而会导致Connection reset by peer的报错
            httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(true).build());
            return httpClientBuilder;
        });

        return new RestHighLevelClient(builder);
    }

你可能感兴趣的:(elasticsearch,大数据,搜索引擎)