GCP java SDK在20秒后抛出套接字连接超时
Posted
技术标签:
【中文标题】GCP java SDK在20秒后抛出套接字连接超时【英文标题】:GCP java SDK throwing socket connection timeout after 20seconds 【发布时间】:2022-01-18 08:30:30 【问题描述】:目前在我的 POC 中,我正在尝试使用 java SDK 获取 gcp 项目的列表,但是当我在 20 秒后执行 API 时得到“java.net.SocketTimeoutException: connect timed out”
注意:在我的代码中,我已经将 setConnectTimeout 值设置为 3 分钟。
这是我的代码 sn-p:
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.fromStream(
new ByteArrayInputStream(credentials.getBytes(Charset.forName("UTF-8"))));
if (credential.createScopedRequired())
credential = credential.createScoped(
Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
CloudResourceManager cloudResourceManager= new CloudResourceManager.Builder(new NetHttpTransport(), jsonFactory,
setHttpTimeout(credential)).setApplicationName("Google-CloudResourceManagerSample/0.1").build();
List<Project> projects = cloudResourceManager.projects().list().execute().getProjects();
private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer)
return httpRequest ->
requestInitializer.initialize(httpRequest);
httpRequest.setConnectTimeout(3 * 60000)// 3 minutes connect timeout
.setReadTimeout(3 * 60000); // 3 minutes read timeout
;
错误跟踪:
java.net.SocketTimeoutException: connect timed out
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 sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:681)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
你能帮我解决一下吗?
【问题讨论】:
【参考方案1】:根据documentation 使用下面的代码 sn-p
private static HttpRequestInitializer setHttpTimeout(final HttpRequestInitializer requestInitializer)
return new HttpRequestInitializer()
@Override
public void initialize(HttpRequest httpRequest) throws IOException
requestInitializer.initialize(httpRequest);
httpRequest.setConnectTimeout(3 * 60000); // 3 minutes connect timeout
httpRequest.setReadTimeout(3 * 60000); // 3 minutes read timeout
;
public static Analytics initializeAnalytics() throws Exception
// Initializes an authorized analytics service object.
// Construct a GoogleCredential object with the service account email
// and p12 file downloaded from the developer console.
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
.setServiceAccountScopes(AnalyticsScopes.all())
.build();
// Construct the Analytics service object.
return new Analytics.Builder(httpTransport, JSON_FACTORY,setHttpTimeout(credential))
.setApplicationName(APPLICATION_NAME).build();
也可以参考堆栈溢出链接link1,link2了解更多详情
【讨论】:
感谢重播,我有 json 文件作为凭据,但上述程序期望 P12 文件作为输入。您在我的示例代码中看到任何错误吗?我的 dobut 就是为什么它没有采用我配置的值(setconnectiontimeout 值) 看来你的 setReadTimeout 是错误的,应该是httpRequest.setReadTimeout(3 * 60000);
。可以换一个试试吗?
我之前也尝试过,但结果没有变化。 java支持方法链,所以应该没什么区别。
httpRequest.setConnectTimeout(3); // 3 分钟连接超时 httpRequest.setReadTimeout(3); // 3分钟读超时试试
***.com/questions/31312692/… 参考这个链接也会很有帮助以上是关于GCP java SDK在20秒后抛出套接字连接超时的主要内容,如果未能解决你的问题,请参考以下文章
当外部服务尚未回复时,Websphere 7.0 恰好在 180 秒后抛出 503
GoogleApiClient 在调用 onConnected 函数后抛出“GoogleApiClient 尚未连接”
构建 Docker mysql 数据库后抛出 OperationalError