Mystery0の小站

Mystery0の小站

alpine镜像发送ssl请求报错踩坑记录

错误信息

Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Caused by: java.net.ConnectException: Failed to connect to xxx.xxx

Caused by: java.net.ConnectException: Network unreachable (connect failed)

排查过程

乍一看,似乎就是一个很普通的连接错误,特别是最后的 Network unreachable (connect failed)

但是前面部分的 Received fatal alert: handshake_failure 看起来又是一个不那么普通的错误

尝试使用Google搜索关键词,似乎没找到比较重要的信息,结果里面的csdn也是几年前的那种老文章,也就是JDK7版本的一些TLS问题

于是,开始将重心放到TLS上面,但是按照相关的文章修改了 java.security 或者 指定启动参数 都没有什么效果,依旧在报错

这是,我尝试将代码直接在本地IDE中运行,连上生产的数据库,测试了一下相关接口,功能是好的,说明提供接口的服务端没有问题。

似乎,和TLS有关。

我突然想到之前使用alpine镜像的时候都会给里面装一个 ca-certificates 包,于是也 apk add ca-certificates 了一下,没什么帮助,有一个特殊的地方是,再容器中 curl接口是好的。

看起来,和JVM有关系了。

最后,带上alpine关键词再扔到搜索引擎去搜索,果然出来了一些别的东西

指向的是 openjdk 的镜像构建仓库 https://github.com/adoptium/containers/issues/319

issue中说是因为 libgcc 这个包导致的原因,同时指向了另一个issue https://github.com/adoptium/temurin-build/issues/3002

抱着试一试的态度,走了一发 apk add libgcc ,然后重启服务,好了

原因说明

其实就是现在编出来的 eclipse-temurin:8-jre-alpine 镜像和以前编出来的对比,缺了一些对于TLS的支持,导致握手失败,这属于基础镜像里面的错误,执行一下 apk add libgcc就行了。

错误信息完整堆栈

Caused by: com.qiniu.common.QiniuException: java.net.ConnectException: Failed to connect to uc.qbox.me/240e:97c:38:500:3:0:0:3f4:443 on uc.qbox.me/240e:97c:38:500:3:0:0:3f4:443
	at com.qiniu.http.Client.send(Client.java:416)
	at com.qiniu.http.Client.get(Client.java:195)
	at com.qiniu.http.Client.get(Client.java:190)
	at com.qiniu.storage.AutoRegion.queryRegionInfoFromServerIfNeeded(AutoRegion.java:54)
	at com.qiniu.storage.AutoRegion.queryRegionInfo(AutoRegion.java:93)
	at com.qiniu.storage.AutoRegion.queryRegionInfo(AutoRegion.java:122)
	at com.qiniu.storage.AutoRegion.getRsHost(AutoRegion.java:204)
	at com.qiniu.storage.ConfigHelper.rsHost(ConfigHelper.java:55)
	at com.qiniu.storage.BucketManager.rsPost(BucketManager.java:1087)
	at com.qiniu.storage.BucketManager.statResponse(BucketManager.java:279)
	at com.qiniu.storage.BucketManager.stat(BucketManager.java:272)
	at com.xxx.yyy(XXX.java:110)
	... 98 common frames omitted
Caused by: java.io.IOException: java.net.ConnectException: Failed to connect to uc.qbox.me/240e:97c:38:500:3:0:0:3f4:443 on uc.qbox.me/240e:97c:38:500:3:0:0:3f4:443
	at com.qiniu.http.Client$3.intercept(Client.java:119)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
	at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
	at com.qiniu.http.Client.send(Client.java:414)
	... 109 common frames omitted
Caused by: java.net.ConnectException: Failed to connect to uc.qbox.me/240e:97c:38:500:3:0:0:3f4:443
	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:297)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
	at com.qiniu.http.Client$3.intercept(Client.java:116)
	... 113 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
		at sun.security.ssl.Alert.createSSLException(Alert.java:131)
		at sun.security.ssl.Alert.createSSLException(Alert.java:117)
		at sun.security.ssl.TransportContext.fatal(TransportContext.java:318)
		at sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:293)
		at sun.security.ssl.TransportContext.dispatch(TransportContext.java:185)
		at sun.security.ssl.SSLTransport.decode(SSLTransport.java:152)
		at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1401)
		at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1309)
		at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:440)
		at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.kt:379)
		at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:337)
		at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
		... 126 common frames omitted
	Suppressed: java.net.ConnectException: Failed to connect to uc.qbox.me/240e:97c:38:500:3:0:0:3f3:443
		... 128 common frames omitted
	Caused by: java.net.ConnectException: Network unreachable (connect failed)
		at java.net.PlainSocketImpl.socketConnect(Native Method)
		at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
		at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
		at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
		at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
		at java.net.Socket.connect(Socket.java:607)
		at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:128)
		at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
		... 127 common frames omitted
Caused by: java.net.ConnectException: Network unreachable (connect failed)
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:607)
	at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:128)
	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
	... 127 common frames omitted