elasticsearch Connection reset by peer如何处理

文章讲述了在Java中使用RestHighLevelClient连接Elasticsearch集群时,如何正确配置连接超时、最大连接数以及KeepAlive策略,以提高性能并避免因网络问题导致的连接错误。
摘要由CSDN通过智能技术生成
如何处理:
代码的心跳代码删除,服务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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值