java – Jersey客户端HTTPS性能问题

我正在使用JEREY Client HTTPS的JUnit测试来测试在Jetty上运行的安全Web服务.我对wr.get(ClientResponse.class)的一个调用在每个9-10个请求的每个块之后挂起10秒钟.等效的Apache客户端代码在几毫秒内运行.如果我将Jetty切换到HTTP模式,则问题消失.

我使用泽西束客户端1.14和Jetty 6.1.26

apache客户端工作

@Test
public void testApacheClient() throws Exception
{
  HttpClient client = new HttpClient();
  ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
  Protocol https = new Protocol( "https", socketFactory, 443 );
  Protocol.registerProtocol( "https", https );

  //Finishes in < 1 second
  for ( int i = 0; i < 30; i++ )
  {
    GetMethod getMethod = new GetMethod( "https://localhost:8443/home" );
    client.executeMethod( getMethod );
    String entity = getMethod.getResponseBodyAsString();
    getMethod.releaseConnection();
  }
}

泽西客户端挂起

@Test
public void testJerseyClient() throws Exception

  HostnameVerifier hv = getHostnameVerifier();
  ClientConfig config = new DefaultClientConfig();
  SSLContext ctx = getSslContext();
  config.getProperties().put( HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
      new HTTPSProperties( hv, ctx ) );

  Client jerseyClient = Client.create( config );

  //Finishes in < 1 second
  for ( int i = 0; i < 30; i++ )
  {
    WebResource wr = jerseyClient.resource( "https://www.google.com" );
    ClientResponse cr = wr.get( ClientResponse.class );
    String entity = cr.getEntity( String.class );
    cr.close();
  }

  /* Pauses for 10 seconds after the 10th request, and subsequently after every 9th request.
     Debugging shows waiting at line 'ClientResponse cr = ...'
   */
  for ( int i = 0; i < 30; i++ )
  {
    WebResource wr = jerseyClient.resource( "https://localhost:8443/home" );
    ClientResponse cr = wr.get( ClientResponse.class );  //10 second pause after requests 9, 18, 27
    String entity = cr.getEntity( String.class ); //This is triggering the problem
    cr.close();
  }

  //Finishes in < 1 second
  for ( int i = 0; i < 30; i++ )
  {
    WebResource wr = jerseyClient.resource( "https://localhost:8443/home" );
    ClientResponse cr = wr.get( ClientResponse.class );
    cr.close();
  }
}

从ClientResponse中检索实体似乎触发了问题,但只能在HTTPS模式下运行,而不是在我的Web服务器(而不是谷歌,Facebook等)上运行.我在Jetty上运行Jersey ServletContainer和Struts ActionServlet,并且两者都出现了问题.我也在我的子网上的不同机器上运行Web服务器,并在多台机器上测试单元.

泽西HTTPS类

private HostnameVerifier getHostnameVerifier() {
  HostnameVerifier hv = new HostnameVerifier() {
    public boolean verify( String arg0, SSLSession arg1 ) { return true; }
  };
  return hv;
}

private SSLContext getSslContext() throws Exception {      
  private final SSLContext sslContext = SSLContext.getInstance( "SSL" );
  sslContext.init( null, new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
        public void checkClientTrusted( X509Certificate[] certs, String authType ) {}
        public void checkServerTrusted( X509Certificate[] certs, String authType ) {}
      }
    }, new SecureRandom()
  );
  return sslContext;
}

Jetty SSL连接器.如果我使用SelectChannelConnector和使用HTTP的单元测试,问题就会消失.

<Call name="addConnector">
  <Arg>
    <New class="org.mortbay.jetty.security.SslSocketConnector">
      <Set name="allowRenegotiate">true</Set>
      <Set name="Port">8443</Set>
      <Set name="reuseAddress">true</Set>
      <Set name="Host">mgmt-int</Set>
      <Set name="maxIdleTime">30000</Set>
      <Set name="handshakeTimeout">2000</Set>
      <Set name="keystore"><SystemProperty name="jetty.home" default="/usr/app/web-app"/>/conf/keystore.jetty</Set>
      <Set name="password">OBF:1vaaaaaaaaaaaaaaaaaaaa111111111v</Set>
      <Set name="keyPassword">OBF:1vaaaaaaaaaaaaaaaaaaaa111111111v</Set>
    </New>
  </Arg>
</Call>
这些运行在哪些操作系统?

只是一个猜测,但它可能与慢/ dev /随机实现(通过SecureRandom)有关.

相关讨论:

How to solve performance problem with Java SecureRandom?

由于我不能总是控制Web应用程序中的JVM参数,所以我一直在使用它作为解决方法:

static {
    System.setProperty("java.security.egd", "file:///dev/urandom");
}

不知道是否严格地推荐(但是解决了我的问题).

http://stackoverflow.com/questions/13277105/jersey-client-https-performance-issue

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – Jersey客户端HTTPS性能问题