对于最新稳定版本,请使用 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-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靴提供 一种方便且有序的方式通过其*跑步者接口 (命令线跑者应用运行者).一个行为良好的任务可以启动任何 逻辑是通过使用这两个运行者中的一个来实现的。spring-doc.cadn.net.cn

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

任何由命令线跑者应用运行者(通过使用InitializingBean#afterPropertiesSet例如)不是 由Spring Cloud Task录制。

运行示例

目前,我们的应用应该可以正常工作了。由于该应用基于 Spring Boot, 我们可以通过以下方式从命令行运行$ ./mvnw Spring靴: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的示例模块中可以找到一个简单的任务应用 任务项目在此