Google cloud platform 启动时创建任务时出现死线异常
我在Google Cloud Run上部署了一个Spring Boot 2.4.5应用程序(使用Jib创建的图像)。启动时,我想创建一个云任务,但我得到了一个Google cloud platform 启动时创建任务时出现死线异常,google-cloud-platform,google-cloud-run,Google Cloud Platform,Google Cloud Run,我在Google Cloud Run上部署了一个Spring Boot 2.4.5应用程序(使用Jib创建的图像)。启动时,我想创建一个云任务,但我得到了一个死线异常异常 如果我运行了任务创建代码,但由HTTP请求触发,则会创建任务。启动时应该创建的任务也被创建了。这就像启动时缺少阻止创建任务的东西一样 启动事件 @EventListener(ApplicationReadyEvent.class) public void doSomethingAfterStartup() { LOG
死线异常异常
如果我运行了任务创建代码,但由HTTP请求触发,则会创建任务。启动时应该创建的任务也被创建了。这就像启动时缺少阻止创建任务的东西一样
启动事件
@EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() {
LOGGER.info("ApplicationReadyEvent");
String message = "GCP New Instance Start " + Instant.now();
cloudTasksService.createTask("xxxx", "us-central1", "xxxx", message, 60);
}
@GetMapping("/tasks")
public ResponseEntity<Void> task(@RequestParam Integer delay) throws IOException {
cloudTasksService.createTask("xxxx", "us-central1", "xxxx", "using HTTP request", delay);
return ResponseEntity.accepted().build();
}
任务创建代码
public void createTask(String projectId, String locationId, String queueId, String message, Integer delay) throws IOException {
try (CloudTasksClient client = CloudTasksClient.create()) {
LOGGER.info("Client created");
String url = "xxxxxxxxx";
String payload = String.format("{ \"text\": \"%s\"}", message);
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
Instant eta = Instant.now().plusSeconds(delay);
Task.Builder taskBuilder =
Task.newBuilder()
.setScheduleTime(Timestamp.newBuilder().setSeconds(eta.getEpochSecond()).build())
.setHttpRequest(
HttpRequest.newBuilder()
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setUrl(url)
.setHttpMethod(HttpMethod.POST)
.build());
LOGGER.info("TaskBuilder ready");
Task task = client.createTask(queuePath, taskBuilder.build());
LOGGER.info("Task created: {}", task.getName());
}
}
HTTP端点
@EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() {
LOGGER.info("ApplicationReadyEvent");
String message = "GCP New Instance Start " + Instant.now();
cloudTasksService.createTask("xxxx", "us-central1", "xxxx", message, 60);
}
@GetMapping("/tasks")
public ResponseEntity<Void> task(@RequestParam Integer delay) throws IOException {
cloudTasksService.createTask("xxxx", "us-central1", "xxxx", "using HTTP request", delay);
return ResponseEntity.accepted().build();
}
当任务排队时,工人似乎还没有准备好。我建议在启动时创建任务,因为这可能经常发生,因为在处理任务时,工作人员没有通过就绪检查,然后任务就会失败。这也解释了为什么任务在HTTP触发时正常运行
您可以通过减少启动后的时间来解决这个问题。另外,当您将Java与Springboot一起使用时,您可能还需要查看推荐信息。Hello@Sydney。有进展吗?