我刚刚在我的代码中设置了 Google 自然语言客户端。在我的本地系统上一切正常,谷歌自然语言 API 可以在几秒钟内回答。
但是当我尝试在Tomcat(localhost)上运行我的代码时,请求会在1分钟后终止,DEADLINE EXCEEDED ERROR
同时进行一个非常简单的测试调用。
这是一些代码:
public static List<Entity> getLanguageEntities(String input, InputStream credentialInputStream) {
try {
// Instantiates a client
LanguageServiceClient language = getClient(credentialInputStream);
// build document
Document doc = Document.newBuilder().setContent(input).setType(Type.PLAIN_TEXT).build();
//HERE IT BREAKS..
AnalyzeEntitiesResponse analyzeEntitiesResponse = language.analyzeEntities(doc, EncodingType.NONE);
List<Entity> entityList = analyzeEntitiesResponse.getEntitiesList();
return entityList;
}catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static LanguageServiceClient getClient(InputStream credentialInputStream) throws Exception {
final LanguageServiceSettings languageServiceSettings;
final GoogleCredentials myCredentials = GoogleCredentials
.fromStream(credentialInputStream)
.createScoped(
Collections.singleton("https://www.googleapis.com/auth/cloud-platform")
);
final CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(myCredentials);
final InstantiatingChannelProvider channelProvider = LanguageServiceSettings
.defaultChannelProviderBuilder()
.setCredentialsProvider(credentialsProvider)
.build();
languageServiceSettings = LanguageServiceSettings
.defaultBuilder()
.setChannelProvider(channelProvider)
.build();
// Create Google API client
LanguageServiceClient languageServiceClient = LanguageServiceClient.create(languageServiceSettings);
return languageServiceClient;
}