Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在okhttp中使用http2时,为什么对同一主机的多个请求没有';不要只用一个接头_Java_Okhttp_Http2 - Fatal编程技术网

Java 在okhttp中使用http2时,为什么对同一主机的多个请求没有';不要只用一个接头

Java 在okhttp中使用http2时,为什么对同一主机的多个请求没有';不要只用一个接头,java,okhttp,http2,Java,Okhttp,Http2,我想测试okhttp的http2函数。我以异步方式对同一主机进行多个请求。但是,我发现,它涉及多个连接,因为协议是h2,它应该只使用一个连接,对吗? 代码如下。 啊,我用的是okhttp2.5 public class Performance { private final OkHttpClient client = new OkHttpClient(); private final Dispatcher dispatcher = new Dispatcher(); pr

我想测试okhttp的http2函数。我以异步方式对同一主机进行多个请求。但是,我发现,它涉及多个连接,因为协议是h2,它应该只使用一个连接,对吗? 代码如下。 啊,我用的是okhttp2.5

public class Performance {
    private final OkHttpClient client = new OkHttpClient();
    private final Dispatcher dispatcher = new Dispatcher();
    private final int times = 20;

    public Performance(){
        dispatcher.setMaxRequestsPerHost(2);
        client.setDispatcher(dispatcher);

        // Configure the sslContext
        // MySSLSocketFactory mySSLSocketFactory = new MySSLSocketFactory();
        // client.setSslSocketFactory(mySSLSocketFactory);
        // client.setHostnameVerifier(new HostnameVerifier() {
        //     public boolean verify(String s, SSLSession sslSession) {
        //         return true;
        //     }
        // });
    }
    public void run()throws Exception{
        for(int i=0; i<times; i++) {
            Request request = new Request.Builder()
                .url("https://http2bin.org/delay/1")
                .build();
            client.newCall(request).enqueue(new Callback() {
                public void onFailure(Request request, IOException e) {
                    e.printStackTrace();
                }

                public void onResponse(Response response) throws IOException {
                    System.out.println(response.headers().get("OkHttp-Selected-Protocol"));
                }
            });
        }
    }
    public static void main(String[] args)throws Exception{
        Performance performance = new Performance();
        performance.run();
    }
}
公共类性能{
私有最终OkHttpClient客户端=新OkHttpClient();
专用最终调度程序=新调度程序();
私人最终整数倍=20;
公共绩效(){
dispatcher.setMaxRequestsPerHost(2);
client.setDispatcher(调度器);
//配置sslContext
//mysslssocketfactory mysslssocketfactory=new mysslssocketfactory();
//客户端.setsssocketfactory(mysslssocketfactory);
//client.setHostnameVerifier(新的HostnameVerifier(){
//公共布尔验证(字符串s,SSLSession SSLSession){
//返回true;
//     }
// });
}
public void run()引发异常{

对于(int i=0;i在OkHttp中,多个同时请求各自创建自己的套接字连接,而不是协调一个共享连接。只有在同时创建连接时才会发生这种情况。在第二次连接之前500毫秒,通过产生解决方法。

请解释如何知道您的请求正在通过我得到的结论是:1.当修改dispatcher的maxRequestsPerHost时,结果会相应地改变,这就是为什么我使用/delay/1路径进行测试。maxRequestsPerHost表示连接数或请求数,尽管在一个连接中可以执行多个请求?;2.我使用其他路径(如/get)将性能与HTTP1.1进行比较,无论我设置了多少个请求,似乎都没有性能差异。您能确认您获得了HTTP/2连接吗?请注意,您需要桌面上的Jetty ALPN。是的,我可以.response.headers().get(“OkHttp选定协议”)我已经在VM选项中使用-Xbootclasspath/p:C:/Users/zfz/.m2/repository/org/mortbay/Jetty/ALPN/ALPN-boot/8.1.4.v20150727/ALPN-boot-8.1.4.v20150727.jar添加了Jetty-ALPN支持。如果存在此错误,这意味着在许多情况下我的应用程序将无法从HTTP/2的多路复用功能中获益,对吗?因为如果我想等待500毫秒来创建第二个连接,可能使用HTTP1.1就足够了。而且,我在
client.newCall(requeust)之后添加
Thread.sleep(1000)
,做了一个实验.enqueue
,它似乎总是像以前一样使用多个连接。错误链接正确吗?我认为这与此问题无关。您的错误描述是相关的,但是,我无法使用sleep解决它。等待您的帮助,thx~他们在OKHttp3.0+中修复了此错误吗?我也想知道。这在3.x中修复了吗?实际上应该虽然相关问题仍需注意,但目前仍需修复。