spring restemplate:403异常(有时)

ecbunoof  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(377)

我有一个对api(get)的restemplate调用。这个调用,是我们唯一的get类型,并通过代理。似乎有时在一周内,调用会返回一个403禁止的异常:“sun.security.validator.validatorexception”
我们在spring和api之间有一个证书,但是这个证书工作得很好(应用程序在一天内返回数千个“200ok”)。
但有时,只有这个调用(而不是其他post)返回“403 forbidden”。
我们已经做到了:
通过代理使用curl启动jmeter(一切似乎都正常)
禁用信任库仅用于测试(结果为ko)
这是resttemplate代码:

SSLConnectionSocketFactory socketFactory;
socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder()
    .loadTrustMaterial(ResourceUtils.getFile(this.trustStorePath), this.trustStorePassword.toCharArray())
    .loadKeyMaterial(ResourceUtils.getFile(this.keyStorePath), this.keystorePassword.toCharArray(),
        this.keystorePassword.toCharArray())
    .build(), NoopHostnameVerifier.INSTANCE);

CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(socketFactory).setProxy(host)
    .disableCookieManagement().disableRedirectHandling().build();

ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);

RestTemplate restTemplateVar = new RestTemplate(requestFactory);

这就是电话:

response = this.restTemplate.getForEntity(this.host, String.class);

并发连接的数量可能是原因吗?
为什么只有得到和有时?
最后一个:如果我们通过httpconnection更改restemplate,结果可能会有所不同?
提前谢谢

rkttyhzu

rkttyhzu1#

设置此属性可以很好地工作(这取决于您的度量)
.setMaxConntTotal(1000)
.setmaxconnperroute(40)

CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.setProxy(host)
.disableCookieManagement()
.disableRedirectHandling()
.setMaxConnTotal(1000)        
.setMaxConnPerRoute(40)
.build();

相关问题