spring cloud之spring cloud task
简介SpringCloud任务允许用户使用SpringCloud开发和运行短期的微服务,在本地 、云环境中、甚至在SpringCloud Data Flow上运行它们。仅添加注解@EnableTask便可将app作为spring boot app运行。spring cloud task让创建短期的微服务变得很容易。它提供的功能允许在生产环境中按需执行短期的JVM进程。环境要求Java8级...
1. 简介
SpringCloud任务允许用户使用SpringCloud开发和运行短期的微服务,在本地 、云环境中、甚至在SpringCloud Data Flow上运行它们。仅添加注解@EnableTask便可将app作为spring boot app运行。
spring cloud task让创建短期的微服务变得很容易。它提供的功能允许在生产环境中按需执行短期的JVM进程。
2. 环境要求
Java8级更高版本
spring cloud task使用RDB来存储任务运行的结果,当然开发的时候也可以不适用数据库,任务的状态作为taskrepository更新的一部分进行记录。目前支持的RDB有:
- DB2
- H2
- HSQLDB
- MySql
- Oracle
- Postgres
- SqlServer
3. Spring Cloud Task之Hello World
Hello World
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>
启动类
@SpringBootApplication(scanBasePackages = {"com.yuyan"})
@ComponentScan({"com.yuyan"})
@EnableTask
public class StarterApplication {
private static final Logger log = LoggerFactory.getLogger(StarterApplication.class);
public static void main(String[] args) {
SpringApplication.run(StarterApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner() {
return new HelloWorldCommandLineRunner();
}
public static class HelloWorldCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
log.info("running a task!!!!");
}
}
}
application.properties
spring.application.name=task1
logging.level.org.springframework.cloud.task=DEBUG
CommandLineRunner
@EnableTask
这个类级注解用来开启Spring Cloud Task的功能。默认情况下,会引入一个附加的配置类SimpleTaskConfiguration,这个附加的配置会注册taskrepository和基础资源以供使用。
TaskRepository使用内存的Map来保存Task执行的结果。这个Map在生产环境中并不是有效的解决方案,因为这个Map会随着任务的结束而销毁。
CommandLineRunner
SimpleTaskRepository
默认任务仓库
4. 生命周期
实现接口TaskExecutionListener监听任务的状态
@Component
public class MyTaskListener implements TaskExecutionListener {
private static final Logger log = LoggerFactory.getLogger(MyTaskListener.class);
@Override
public void onTaskStartup(TaskExecution taskExecution) {
log.info("starting task:" + taskExecution.getTaskName());
}
@Override
public void onTaskEnd(TaskExecution taskExecution) {
log.info("ending task:" + taskExecution.getTaskName());
}
@Override
public void onTaskFailed(TaskExecution taskExecution, Throwable throwable) {
log.info("task failed:" + taskExecution.getTaskName());
log.error("error msg:", throwable);
}
}
5. 为任务配置数据源
引入jpa的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
修改application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jackson.serialization.indent-output=false
为TaskConfigurer配置数据源
@Autowired
private DataSource dataSource;
@Bean
public DefaultTaskConfigurer getTaskConfigurer() {
return new DefaultTaskConfigurer(dataSource);
}
运行task之后数据库中增加如下四张表
task_execution记录了刚刚任务执行的起始时间、结果等数据
6. 集成Spring Batch
引入spring batch依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-batch</artifactId>
</dependency>
application.properties中增加
spring.batch.initialize-schema=always
JobConfiguration
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Job job1() {
return this.jobBuilderFactory.get("job1")
.start(this.stepBuilderFactory.get("job1step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
logger.info("Job1 was run");
return RepeatStatus.FINISHED;
}
})
.build())
.build();
}
Spring Batch的东西远不止这些。。。
7. 任务实例个数的限制
引入依赖
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jdbc</artifactId>
</dependency>
spring.cloud.task.single-instance-enabled为true表示同一个时刻只能有一个叫这个名字的task执行,否则会抛出Task with name “application” is already running.
#限制同一个时刻只能有一个同名任务执行
spring.cloud.task.single-instance-enabled=true
更多推荐
所有评论(0)