springboot 长连接 keepalive 记录

maven引入依赖

<dependency>
 <groupId>org.jodd</groupId>
 <artifactId>jodd-http</artifactId>
 <version>5.1.4</version>
</dependency>

设置keepalive java配置:

@Configuration
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        //使用对应工厂类提供给我们的接口定制化我们的tomcat connector
        ((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                //定制KeepAliveTimeout,设置10秒内没有请求则服务器自动断开keepalive连接
                protocol.setKeepAliveTimeout(10000);
                //当客户端发送超过5个请求则自动断开keepalive连接
                protocol.setMaxKeepAliveRequests(5);
            }
        });
    }
}

测试代码:
 

@Slf4j
public class TestHttp11 {

    public static void main(String[] args) throws Exception {
        HttpResponse response = null;
        for (int i = 0; i < 12; i++) {

            response = http11(i, response, i == (10 - 1));
            Thread.sleep(9000);
            if (i > 1) {
                Thread.sleep(3000);
            }
        }
    }

    /*
    服务端设置最大请求次数(MaxKeepAliveRequests)为5,超时时间(KeepAliveTimeout)为10秒
    1、请求10次,每次间隔1秒
        抓包发现5次后服务端返回  Connection: close
        第6次请求无异常 创建新的连接
    2、请求3次,每次间隔12秒
        第二次请求后异常报错
    3、请求3次,每次间隔9秒
        无异常
    4、请求6次,1-2次间隔9秒,2次以后间隔12秒
        第四次后异常

    <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>5.1.4</version>
        </dependency>
     */
    public static HttpResponse http11(int num, HttpResponse response, boolean last) throws UnsupportedEncodingException {
        HttpRequest request = HttpRequest.get("http://127.0.0.1:8080/test/http11?num=" + num);
        if (response == null) {
            response = request.connectionKeepAlive(true).send();
        } else {
            if (!last) {
                response = request.keepAlive(response, true).send();
            } else {
                response = request.keepAlive(response, false).send();
            }
        }
        String info = new String(response.body().getBytes("iso8859-1"), "utf-8");
        System.out.println(info);
        return response;
    }

}

源码下载:
https://download.csdn.net/download/zjh1n795/16135081

 

参考:
https://blog.csdn.net/weixin_41657493/article/details/90819987
https://blog.51cto.com/15015181/2556402?source=drt

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gRPC 提供了 Keepalive 机制来维护客户端和服务端之间的长连接Keepalive 机制可以让客户端和服务端定期交换心跳信号,以确保连接的存活性。 gRPC 的 Keepalive 机制有两种实现方式:基于 HTTP/2 ping 的实现和基于 gRPC 自定义的实现。 基于 HTTP/2 ping 的实现方式是默认的方式,它通过发送 ping 帧来检测连接是否存活。当客户端在一段时间内没有收到服务端的任何消息时,就会发送 ping 帧;当服务端在一段时间内没有收到客户端的任何消息时,就会发送 goaway 帧,关闭连接。这种实现方式可以通过以下方式进行配置: ```java ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port) .keepAliveTime(30, TimeUnit.SECONDS) // 客户端长连接的时间 .keepAliveTimeout(5, TimeUnit.SECONDS) // 客户端长连接空闲时间 .keepAliveWithoutCalls(true) // 客户端空闲时是否发送 ping 请求 .build(); ``` 基于 gRPC 自定义的实现方式是在 gRPC 层面上实现 Keepalive,它可以更加灵活地配置 Keepalive 参数。这种实现方式需要客户端和服务端同时支持,需要在客户端和服务端都进行配置。客户端配置方式如下: ```java ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port) .keepAliveTime(30, TimeUnit.SECONDS) // 客户端长连接的时间 .keepAliveTimeout(5, TimeUnit.SECONDS) // 客户端长连接空闲时间 .keepAliveWithoutCalls(true) // 客户端空闲时是否发送 ping 请求 .keepAliveIdle(10, TimeUnit.SECONDS) // 客户端空闲时发送 ping 请求的间隔时间 .keepAliveMaxPings(1) // 客户端最大发送 ping 请求数量 .keepAliveMaxIdle(5, TimeUnit.MINUTES) // 客户端最大空闲时间,超时会关闭连接 .build(); ``` 服务端配置方式如下: ```java Server server = ServerBuilder.forPort(port) .keepAliveTime(30, TimeUnit.SECONDS) // 服务端长连接的时间 .keepAliveTimeout(5, TimeUnit.SECONDS) // 服务端长连接空闲时间 .keepAliveWithoutCalls(true) // 服务端空闲时是否发送 ping 请求 .keepAliveIdle(10, TimeUnit.SECONDS) // 服务端空闲时发送 ping 请求的间隔时间 .keepAliveMaxPings(1) // 服务端最大接收 ping 请求数量 .keepAliveMaxIdle(5, TimeUnit.MINUTES) // 服务端最大空闲时间,超时会关闭连接 .addService(service) .build(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值