首页 > 解决方案 > 使用 java jersey 客户端异常丢弃 HTTP 流

问题描述

对于我们的一个项目,我们正在使用 java jersey 客户端使用 HTTP 提要流

客户端Feed消费正常,但10分钟后,流异常下降;即使流生产者已启动并运行并产生流

这就是我尝试过的;

import java.util.Date;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ChunkedInput;

public class StreamClient {
    
    private static final String BOUNDARY = "\n";

    public void makeCall() {
        System.out.println("Start Time"+ new Date()) ;
        Client client = ClientBuilder.newClient();
        BasicAuth auth = new BasicAuth();
        auth.setPassword("username");
        auth.setUserName("password");
        BasicAuthentication basicAuthentication = new BasicAuthentication(auth);
        client.register(basicAuthentication);
        WebTarget target = client.target("https://localhost:7211/stream/v1/");
        Builder request = target.request(MediaType.APPLICATION_JSON);
        Response response = request.get();
        
        final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
        });
        
        chunkedInput.setParser(ChunkedInput.createParser(BOUNDARY));
        String chunk;
        do {
            if((chunk = chunkedInput.read()) != null) 
                System.out.println(chunk);  
        }while (!chunkedInput.isClosed());
        System.out.println("End Time " + new Date());
    }
    
    public static void main(String[] args) {
        StreamClient client = new StreamClient();
        client.makeCall();
    }


}

如果流没有下降;线程必须在 while 循环内;一旦流关闭,线程就会出来并打印 sysout 语句

这里的问题是;为什么 chunkedinput 被关闭。

在这里,对于服务器端的每个有效负载/块分离,它都是 \r\n 并使其与服务器的连接处于活动状态,发送 \n (在客户端,我必须忽略这一点。)

标签: javajerseyjersey-client

解决方案


我建议使用wireshark 在连接丢失时准确分析流量,可能是防火墙正在切断连接。

在任何情况下,尝试在没有双面记录的情况下调试这些场景都是 50% 的俄罗斯规则情况。

如果要丢弃场景尝试改变正在传输的数据量,连接的参数(使用其他构造函数方法)并查看超时是否及时一致,如果超时取决于通常的流量您在防火墙等中间设备中触发某些阈值的线索。


推荐阅读