开始
如果您刚刚开始使用 Spring Cloud Task,则应阅读本节。 在这里,我们回答了基本的 “what?”、“how?” 和 “why?” 问题。我们从 Spring Cloud Task 的简要介绍。然后,我们构建一个 Spring Cloud Task 应用程序 边走边讨论一些核心原则。
4. Spring Cloud Task 简介
Spring Cloud Task 使创建短期微服务变得容易。它提供 允许在生产中按需执行短期 JVM 进程的功能 环境。
5. 系统要求
您需要安装 Java(Java 8 或更高版本)。要构建,您需要拥有 Maven 也安装了。
5.1. 数据库要求
Spring Cloud Task 使用关系数据库来存储已执行任务的结果。 虽然您可以在没有数据库的情况下开始开发任务(任务的状态会记录下来 作为任务存储库更新的一部分),对于生产环境,您希望 使用支持的数据库。Spring Cloud Task 目前支持以下数据库:
-
DB2
-
H2 系列
-
HSQLDB 数据库
-
MySql 的
-
神谕
-
Postgres
-
SqlServer 服务器
6. 开发您的第一个 Spring Cloud 任务应用程序
一个好的起点是一个简单的 “Hello, World!” 应用程序,因此我们创建 Spring Cloud Task 等效于突出框架的功能。大多数 IDE 都有 对 Apache Maven 的良好支持,因此我们将其用作此项目的构建工具。
spring.io 网站包含许多“Getting Started ”
指南使用 Spring Boot 的 Boot。如果您需要解决特定问题,请先检查那里。
您可以通过转到 Spring Initializr 并创建一个新项目来简化以下步骤。这样做
自动生成新的项目结构,以便您可以立即开始编码。
我们建议尝试使用 Spring Initializr 来熟悉它。 |
6.1. 使用 Spring Initializr 创建 Spring 任务项目
现在,我们可以创建并测试一个打印Hello, World!
拖动到控制台。
为此,请执行以下作:
-
访问 Spring Initialzr 网站。
-
创建一个 Group name 为
io.spring.demo
和 Artifact 名称helloworld
. -
在 Dependencies (依赖项) 文本框中,键入
task
,然后选择Cloud Task
Dependency。 -
在 Dependencies (依赖项) 文本框中,键入
jdbc
,然后选择JDBC
Dependency。 -
在 Dependencies (依赖项) 文本框中,键入
h2
,然后选择H2
.(或您最喜欢的数据库) -
单击 Generate Project 按钮
-
-
解压缩 helloworld.zip 文件并将项目导入到您最喜欢的 IDE 中。
6.2. 编写代码
要完成我们的应用程序,我们需要更新生成的HelloworldApplication
替换为以下内容,以便启动 Task。
package io.spring.demo.helloworld;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableTask
public class HelloworldApplication {
@Bean
public CommandLineRunner commandLineRunner() {
return new HelloWorldCommandLineRunner();
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
public static class HelloWorldCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
System.out.println("Hello, World!");
}
}
}
虽然它可能看起来很小,但正在发生相当多的事情。有关 Spring 的更多信息 引导细节,请参阅 Spring Boot 参考文档。
现在我们可以打开application.properties
文件src/main/resources
.
我们需要在application.properties
:
-
application.name
:设置应用程序名称(转换为任务名称) -
logging.level
:要将 Spring Cloud 任务的日志记录设置为DEBUG
为了 了解正在发生的事情。
以下示例显示了如何执行这两项作:
logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloWorld
6.2.1. 任务自动配置
当包含 Spring Cloud Task Starter 依赖项时,Task auto 会配置所有 bean 以引导其功能。
此配置的一部分会注册TaskRepository
以及使用它的基础设施。
在我们的演示中,TaskRepository
使用嵌入式 H2 数据库记录结果
的任务。此 H2 嵌入式数据库不是生产环境的实用解决方案,因为
任务结束后,H2 DB 就会消失。但是,为了快速入门
经验,我们可以在示例中使用它,并将正在更新的内容回显到日志中
在那个存储库中。在 Configuration (配置) 部分(稍后部分)
文档),我们将介绍如何自定义
Spring Cloud 任务。
当我们的示例应用程序运行时, Spring Boot 会启动我们的HelloWorldCommandLineRunner
并将我们的 “Hello, World!” 消息输出到 standard out。这TaskLifecycleListener
在存储库中记录任务的开始和结束。
6.2.2. main 方法
main 方法用作任何 Java 应用程序的入口点。我们的主要方法 委托给 Spring Boot 的 SpringApplication 类。
6.2.3. CommandLineRunner
Spring 包含许多方法来引导应用程序的 logic。Spring Boot 提供
一种通过其*Runner
接口
(CommandLineRunner
或ApplicationRunner
).一个表现良好的任务可以引导任何
logic 使用这两个 runner 之一。
任务的生命周期被视为从*Runner#run
方法被执行
到它们全部完成之后。Spring Boot 允许应用程序使用多个*Runner
实现,Spring Cloud Task 也是如此。
从CommandLineRunner 或ApplicationRunner (通过使用InitializingBean#afterPropertiesSet 例如)不是
由 Spring Cloud Task 记录。 |
6.3. 运行示例
此时,我们的应用程序应该可以正常工作。由于此应用程序基于 Spring Boot,因此
我们可以使用$ mvn spring-boot:run
从根源
,如以下示例中所示(及其输出):
$ mvn clean spring-boot:run
....... . . .
....... . . . (Maven log output here)
....... . . .
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
2018-07-23 17:44:34.426 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : Starting HelloworldApplication on Glenns-MBP-2.attlocal.net with PID 1978 (/Users/glennrenfro/project/helloworld/target/classes started by glennrenfro in /Users/glennrenfro/project/helloworld)
2018-07-23 17:44:34.430 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
2018-07-23 17:44:34.472 INFO 1978 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1d24f32d: startup date [Mon Jul 23 17:44:34 EDT 2018]; root of context hierarchy
2018-07-23 17:44:35.280 INFO 1978 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-07-23 17:44:35.410 INFO 1978 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-07-23 17:44:35.419 DEBUG 1978 --- [ main] o.s.c.t.c.SimpleTaskConfiguration : Using org.springframework.cloud.task.configuration.DefaultTaskConfigurer TaskConfigurer
2018-07-23 17:44:35.420 DEBUG 1978 --- [ main] o.s.c.t.c.DefaultTaskConfigurer : No EntityManager was found, using DataSourceTransactionManager
2018-07-23 17:44:35.522 DEBUG 1978 --- [ main] o.s.c.t.r.s.TaskRepositoryInitializer : Initializing task schema for h2 database
2018-07-23 17:44:35.525 INFO 1978 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [org/springframework/cloud/task/schema-h2.sql]
2018-07-23 17:44:35.558 INFO 1978 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [org/springframework/cloud/task/schema-h2.sql] in 33 ms.
2018-07-23 17:44:35.728 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-23 17:44:35.730 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-07-23 17:44:35.733 INFO 1978 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-07-23 17:44:35.738 INFO 1978 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-07-23 17:44:35.762 DEBUG 1978 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Creating: TaskExecution{executionId=0, parentExecutionId=null, exitCode=null, taskName='application', startTime=Mon Jul 23 17:44:35 EDT 2018, endTime=null, exitMessage='null', externalExecutionId='null', errorMessage='null', arguments=[]}
2018-07-23 17:44:35.772 INFO 1978 --- [ main] i.s.d.helloworld.HelloworldApplication : Started HelloworldApplication in 1.625 seconds (JVM running for 4.764)
Hello, World!
2018-07-23 17:44:35.782 DEBUG 1978 --- [ main] o.s.c.t.r.support.SimpleTaskRepository : Updating: TaskExecution with executionId=1 with the following {exitCode=0, endTime=Mon Jul 23 17:44:35 EDT 2018, exitMessage='null', errorMessage='null'}
前面的输出有三行我们在这里感兴趣:
-
SimpleTaskRepository
在TaskRepository
. -
执行我们的
CommandLineRunner
,由 “Hello, World!” 输出演示。 -
SimpleTaskRepository
在TaskRepository
.
可以在 Spring Cloud 的 samples 模块中找到一个简单的任务应用程序 任务项目在这里。 |