![](/img/trans.png)
[英]Missing artifact org.springframework.cloud:spring-cloud-starter-openfeign:jar:3.0.3-SNAPSHOT
[英]Spring-cloud-starter-openfeign: SSL handshake exception with feign-httpclient
当尝试将 feign-httpclient 与 Spring-cloud-starter-openfeign 一起使用时,我收到 SSL 握手异常,而如果我不使用 feign-httpclient,则相同的代码有效。
我需要使用 feign-httpclient,因为我想使用连接工厂。
build.gradle
//on commenting the below dependency the code works fine.
compile('io.github.openfeign:feign-httpclient:9.4.0')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
假装客户端
@FeignClient(name = "testClient", url = "https://test:9820")
public interface TestClient {
@RequestMapping(method = RequestMethod.POST, value = "/test", consumes = "application/json", produces = "application/json")
TesteDto get(TestRequestDto testRequestDto);
}
调用代码:
testClient.get(new TestRequestDto("test"));
应用.yml
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: full
httpclient:
maxConnections: 200
maxConnectionsPerRoute: 200
enabled: true
例外:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
需要以下配置:
feign:
httpclient:
disableSslValidation: true
如果要使用自签名证书,请使用以下代码:
@FeignClient(name = "testClient", url = "https://test:9820", configuration = CustomFeignConfiguration.class)
public interface TestClient {
@RequestMapping(method = RequestMethod.POST, value = "/test", consumes =
"application/json", produces = "application/json")
TesteDto get(TestRequestDto testRequestDto);
}
public class CustomFeignConfiguration {
@Bean
public Client feignClient() {
return new ApacheHttpClient(getHttpClient());
}
private CloseableHttpClient getHttpClient() {
int timeout = 10000;
try {
SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial(new TrustSelfSignedStrategy()).build();
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();
return HttpClientBuilder
.create()
.useSystemProperties()
.setDefaultRequestConfig(config)
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
就我而言,我需要在我的应用程序属性中添加feign.httpclient.disable-ssl-validation=true
另外,我需要在pom.xml
中添加这些依赖项
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
最后请不要因为添加了新的依赖项而忘记重启您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.