本文介绍: / 设置KeepAlive为5分钟的时间,不设置默认为-1,也就是持续连接,然而这会受到外界的影响比如Firewall,会将TCP连接单方面断开,从而会导致Connection reset by peer报错。// 设置KeepAlive为5分钟的时间,不设置默认为-1,也就是持续连接,然而这会受到外界的影响比如Firewall,会将TCP连接单方面断开,从而会导致Connection reset by peer报错。//.setConnectTimeout(5000) //5秒。
如何处理:
代码的心跳代码删除服务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);
    }

原文地址:https://blog.csdn.net/weixin_44399790/article/details/134593318

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_2954.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注