这个版本仍在开发中,尚未考虑稳定。请使用最新的稳定版本:spring-cloud-task 5.0.1spring-doc.cadn.net.cn

快速开始

如果你刚开始接触 Spring Cloud Task,你应该阅读这一部分。 在这里,我们回答基本的“是什么?”、“怎么做?”和“为什么?”问题。我们从对 Spring Cloud Task 的 gentle 引入开始。然后构建一个 Spring Cloud Task 应用程序,在过程中讨论一些核心原理。spring-doc.cadn.net.cn

介绍 Spring Cloud Task

Spring Cloud Task 使创建短生命周期的微服务变得容易。它提供了让短生命周期的 JVM 进程在生产环境中按需执行的能力。spring-doc.cadn.net.cn

系统要求

您需要安装Java(Java 17或更高版本)。spring-doc.cadn.net.cn

数据库要求

Spring Cloud Task 使用关系型数据库来存储任务执行的结果。 虽然可以开始开发一个任务而不使用数据库(任务的状态会作为任务仓库更新的一部分被记录),但在生产环境中,你希望使用受支持的数据库。Spring Cloud Task 目前支持以下数据库:spring-doc.cadn.net.cn

开发您的第一个 Spring Cloud Task 应用程序

一个好的起点是从一个简单的“Hello, World!”应用程序开始,所以我们创建Spring Cloud Task的等价物来突出该框架的功能。大多数IDE对Apache Maven都有很好的支持,因此我们将其作为该项目的构建工具使用。spring-doc.cadn.net.cn

spring.io 网站包含许多 Getting Started" 的 guides,它们使用 Spring Boot。如果你需要解决特定问题,先去那里检查。 你可以通过前往 Spring Initializr 并创建一个新的项目来跳过以下步骤。这样做会自动生成一个新的项目结构,让你可以立即开始编码。 我们建议通过实验 Spring Initializr 来熟悉它。

创建使用 Spring Initializr 的 Spring 任务项目

现在我们可以创建并测试一个打印 Hello, World! 到控制台的应用程序。spring-doc.cadn.net.cn

要做到这一点:spring-doc.cadn.net.cn

  1. 访问 Spring Initialzr 网站。spring-doc.cadn.net.cn

    1. 创建一个新的 Maven 项目,其 Group 名称设为 io.spring.demoArtifact 名称设为 helloworldspring-doc.cadn.net.cn

    2. 在依赖项文本框中输入 task,然后选择带有 Spring Cloud 标签的 Task 依赖项。spring-doc.cadn.net.cn

    3. 在依赖项文本框中输入 h2,然后选择带有 SQL 标签的 H2 依赖项。spring-doc.cadn.net.cn

    4. 点击 生成项目 按钮spring-doc.cadn.net.cn

  2. 解压 helloworld.zip 文件,并将项目导入到您喜欢的 IDE 中。spring-doc.cadn.net.cn

编写代码

为了完成我们的应用程序,我们需要将生成的 HelloworldApplication 更新为以下内容,以便启动一个 Task。spring-doc.cadn.net.cn

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableTask
public class HelloworldApplication {

	@Bean
	public ApplicationRunner applicationRunner() {
		return new HelloWorldApplicationRunner();
	}

	public static void main(String[] args) {
		SpringApplication.run(HelloworldApplication.class, args);
	}

	public static class HelloWorldApplicationRunner implements ApplicationRunner {

		@Override
		public void run(ApplicationArguments args) throws Exception {
			System.out.println("Hello, World!");

		}
	}
}

虽然看起来很小,但实际上很多事情正在进行中。关于 Spring Boot 的更多具体信息,请参见 Spring Boot 参考文档spring-doc.cadn.net.cn

现在我们可以打开 application.properties 文件在 src/main/resources。 我们需要在 application.properties 中配置两个属性:spring-doc.cadn.net.cn

  • application.name: 以应用程序名称设置(将应用程序名称翻译为任务名称)spring-doc.cadn.net.cn

  • logging.level: 为了将 Spring Cloud Task 的日志设置为 DEBUG 以便 查看正在进行的操作。spring-doc.cadn.net.cn

以下示例展示了如何同时进行这两项操作:spring-doc.cadn.net.cn

logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloWorld

任务自动配置

When including Spring Cloud Task Starter dependency, Task auto configures all beans to bootstrap it’s functionality. Part of this configuration registers the TaskRepository and the infrastructure for its use.spring-doc.cadn.net.cn

在我们的演示中,TaskRepository 使用嵌入式的 H2 数据库来记录任务的结果。这个嵌入式的 H2 数据库并非适用于生产环境,因为 H2 数据库在任务结束时就会消失。然而,为了快速入门体验,我们也可以在示例中使用它,并将对那个仓库的更新内容记录到日志中。在配置部分(本文档稍后的位置),我们将介绍如何自定义 Spring Cloud Task 提供组件的配置。spring-doc.cadn.net.cn

当我们的示例应用程序运行时,Spring Boot 会启动 HelloWorldApplicationRunner 并输出我们的 “Hello, World!” 消息到标准输出。TaskLifecycleListener 会记录任务的开始和结束,并在仓库中记录。spring-doc.cadn.net.cn

主要方法

主要方法是任何 Java 应用程序的入口点。我们的 main 方法 委托给 Spring Boot 的 SpringApplication 类。spring-doc.cadn.net.cn

The 应用程序Starters

Spring 包含许多启动应用程序逻辑的方式。Spring Boot 通过其 *Runner 接口 (CommandLineRunnerApplicationRunner) 提供了一种有组织地进行的便捷方法。一个行为良好的任务可以通过使用这两个运行器中的任何一个来启动任何逻辑。spring-doc.cadn.net.cn

任务的生命周期从在执行*Runner#run方法之前考虑,到所有方法都完成后为止。Spring Boot 让应用程序可以使用多个*Runner实现,Spring Cloud Task 也是如此。spring-doc.cadn.net.cn

任何不由 CommandLineRunnerApplicationRunner 启动(例如使用 InitializingBean#afterPropertiesSet)的处理机制,Spring Cloud Task 均不会记录。

运行示例

在这个阶段,我们的应用程序应该可以正常工作。由于这个应用程序是基于Spring Boot的, 我们可以从应用程序根目录使用$ ./mvnw spring-boot:run从命令行运行,如下面示例中所示(并包含其输出):spring-doc.cadn.net.cn

$ mvn clean spring-boot:run
....... . . .
....... . . . (Maven log output here)
....... . . .

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.1)

2024-01-04T10:07:01.102-06:00  INFO 18248 --- [helloWorld] [           main] i.s.d.helloworld.HelloworldApplication   : Starting HelloworldApplication using Java 21.0.1 with PID 18248 (/Users/dashaun/fun/dashaun/spring-cloud-task/helloworld/target/classes started by dashaun in /Users/dashaun/fun/dashaun/spring-cloud-task/helloworld)
2024-01-04T10:07:01.103-06:00  INFO 18248 --- [helloWorld] [           main] i.s.d.helloworld.HelloworldApplication   : No active profile set, falling back to 1 default profile: "default"
2024-01-04T10:07:01.526-06:00  INFO 18248 --- [helloWorld] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-01-04T10:07:01.626-06:00  INFO 18248 --- [helloWorld] [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:3ad913f8-59ce-4785-bf8e-d6335dff6856 user=SA
2024-01-04T10:07:01.627-06:00  INFO 18248 --- [helloWorld] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-01-04T10:07:01.633-06:00 DEBUG 18248 --- [helloWorld] [           main] o.s.c.t.c.SimpleTaskAutoConfiguration    : Using org.springframework.cloud.task.configuration.DefaultTaskConfigurer TaskConfigurer
2024-01-04T10:07:01.633-06:00 DEBUG 18248 --- [helloWorld] [           main] o.s.c.t.c.DefaultTaskConfigurer          : No EntityManager was found, using DataSourceTransactionManager
2024-01-04T10:07:01.639-06:00 DEBUG 18248 --- [helloWorld] [           main] o.s.c.t.r.s.TaskRepositoryInitializer    : Initializing task schema for h2 database
2024-01-04T10:07:01.772-06:00 DEBUG 18248 --- [helloWorld] [           main] o.s.c.t.r.support.SimpleTaskRepository   : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='helloWorld', startTime=2024-01-04T10:07:01.757268, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
2024-01-04T10:07:01.785-06:00  INFO 18248 --- [helloWorld] [           main] i.s.d.helloworld.HelloworldApplication   : Started HelloworldApplication in 0.853 seconds (process running for 1.029)
Hello, World!
2024-01-04T10:07:01.794-06:00 DEBUG 18248 --- [helloWorld] [           main] o.s.c.t.r.support.SimpleTaskRepository   : Updating: TaskExecution with executionId=1 with the following {exitCode=0, endTime=2024-01-04T10:07:01.787112, exitMessage='null', errorMessage='null'}
2024-01-04T10:07:01.799-06:00  INFO 18248 --- [helloWorld] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-01-04T10:07:01.806-06:00  INFO 18248 --- [helloWorld] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

....... . . .
....... . . . (Maven log output here)
....... . . .

前面的输出中有三条对我们在此处有重要意义的输出:spring-doc.cadn.net.cn

一个简单的任务应用程序可以在 Spring Cloud Task 项目中的 samples 模块找到 这里