该版本仍在开发中,尚未被视为稳定。对于最新稳定版本,请使用 spring-cloud-task 5.0.0spring-doc.cadn.net.cn

开始

如果你刚开始使用春季云任务,应该阅读这一部分。 在这里,我们回答了基本的“什么?”、“怎么做?”和“为什么?”问题。我们从一个 温和的春云任务介绍。然后我们构建了一个 Spring Cloud 任务应用程序, 我们边讨论一些核心原则。spring-doc.cadn.net.cn

介绍春云任务

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

开发你的第一个春季云任务应用

一个好的起点是创建一个简单的“Hello, World!”应用程序,因此我们创建了 Spring Cloud Task相当于突出框架的功能。大多数IDE都包含 Apache Maven 支持良好,因此我们用它作为该项目的构建工具。spring-doc.cadn.net.cn

spring.io 网站上包含许多内容开始” 指南使用Spring Boot的。如果你需要解决具体问题,先去那里看看。 你可以通过进入 Spring Initializr 创建新项目来快捷完成以下步骤。正在这样做 它会自动生成新的项目结构,让你可以立即开始编码。 我们建议你先尝试一下Spring Initializr,熟悉它。

使用 Spring Initializr 创建 Spring 任务项目

现在我们可以创建并测试一个打印世界您好!去控制台。spring-doc.cadn.net.cn

具体做法:spring-doc.cadn.net.cn

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

    1. 创建一个新的 Maven 项目,组名为io.spring.demo以及一个名为HelloWorld.spring-doc.cadn.net.cn

    2. 在“依赖”文本框中,输入任务然后选择任务春云标签。spring-doc.cadn.net.cn

    3. 在“依赖”文本框中,输入H2然后选择H2SQL标签。spring-doc.cadn.net.cn

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

  2. 解压helloworld.zip文件,把项目导入你喜欢的IDE。spring-doc.cadn.net.cn

编写代码

为了完成我们的应用,我们需要更新生成的Helloworld应用以下列内容启动任务。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启动的具体信息,请参见 Spring Boot 参考文档spring-doc.cadn.net.cn

现在我们可以打开application.properties归档src/主/资源. 我们需要配置两个属性application.properties:spring-doc.cadn.net.cn

以下示例展示了如何同时实现这两种情况:spring-doc.cadn.net.cn

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

任务自动配置

当包含 Spring Cloud Task Starter 依赖时,任务会自动配置所有 BEANS 以启动其功能。该配置的一部分会注册任务仓库以及其使用的基础设施。spring-doc.cadn.net.cn

在我们的演示中,任务仓库使用嵌入式的H2数据库来记录任务的结果。这个H2嵌入式数据库并不适合生产环境,因为任务结束后,H2数据库就会消失。不过,为了快速开始体验,我们可以在示例中使用它,同时向日志中回应正在更新的内容在该仓库中。在配置部分(本文档后面)中,我们介绍了如何自定义由Spring Cloud Task提供的部分配置。spring-doc.cadn.net.cn

当我们的示例应用运行时,Spring Boot 会启动HelloWorldApplicationRunner并输出我们的“Hello, World!”消息以实现标准输出。 这任务生命周期监听器记录任务在仓库中的开始和结束。spring-doc.cadn.net.cn

主要方法

主方法作为任何 Java 应用的入口。我们的主方法委托给 Spring Boot 的 SpringApplication 类。spring-doc.cadn.net.cn

应用跑者

Spring 包含多种引导应用程序逻辑的方法。Spring Boot 提供了一种方便且有序的方法,通过其*跑步者接口 (命令线跑者应用运行者). 一个行为良好的任务可以通过使用这两个运行程序中的一个来启动任何逻辑。spring-doc.cadn.net.cn

任务的生命周期从之前考虑*跑者#跑方法被执行在所有方法完成后。Spring Boot 允许应用程序使用多个*跑步者实现,Spring Cloud Task也是如此。spring-doc.cadn.net.cn

任何由命令线跑者应用运行者(通过使用InitializingBean#afterPropertiesSet例如)并非由春云任务记录。

运行示例

此时,我们的应用程序应该可以运行了。由于这个应用程序基于 Spring Boot,我们可以通过以下方式从命令行运行它$ ./mvnw Spring靴:run从我们应用的根节点出发,如下示例所示(及其输出):spring-doc.cadn.net.cn

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

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

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的示例模块中找到。任务项目在这里