gpt4 book ai didi

java - 泽西客户端 HTTPS 性能问题

转载 作者:太空狗 更新时间:2023-10-29 22:58:37 24 4
gpt4 key购买 nike

我正在使用 JUnit 测试和 JERSEY Client + HTTPS 来测试在 Jetty 上运行的 secure web service >。我对 wr.get(ClientResponse.class) 的调用之一在每 block 9-10 请求 之后挂起 10 秒。等效的 Apache 客户端 代码在几毫秒内运行。如果我将 Jetty 切换到 HTTP 模式,问题就会消失。

我正在使用 Jersey bundle & client 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();
}
}



Jersey 客户端挂了

@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 模式下并针对我的网络服务器运行(不是 google、facebook 等)。我在 Jetty 上运行了 Jersey ServletContainer 和 Struts ActionServlet,但两者都出现了问题。我还在我的 subnet 上的不同机器上运行了 Web 服务器,并在多台机器上进行了单元测试。



Jersey 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/random 实现有关(通过 SecureRandom)。

此处相关讨论:

How to solve performance problem with Java SecureRandom?

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

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

不知道这是否严格来说是推荐的(但它解决了我的问题)。

关于java - 泽西客户端 HTTPS 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13277105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com