SpringCloud-番外篇-临时任务

SpringCloud文章系列

  1. SpringCloud
  2. SpringCloud-注册中心
  3. SpringCloud-配置中心
  4. SpringCloud-链路跟踪
  5. SpringCloud-消息总线
  6. SpringCloud-API网关
  7. SpringCloud-异步消息
  8. SpringCloud-同步调用
  9. SpringCloud-断路降级
  10. SpringCloud-监控管理
  11. 【当前文章】SpringCloud-番外篇-临时任务
  12. SpringCloud-番外篇-文档生成
  13. SpringCloud-番外篇-源码解析

接入说明

  1. 目前采用最新的1.xRelease版本:1.5.15.RELEASE
  2. SpringCloud项目可以在start.spring.io下载,不过更方便的应该是通过idea新建项目,建立Spring Initializr项目
  3. 我一般习惯将具体实现服务用具体的服务名,而公共组件用service-xxx来命名,当然还有一些比较固定名字的公共组件

Task

1. 新建项目

新建artifactId为service-task的服务

2. 导入依赖

依赖说明

  1. actuator用于暴露监控的接口
  2. eureka-server是Eureka服务器的依赖
  3. config是Config客户端,从配置中心拉取配置
  4. task用于短命服务的依赖
  5. jpa与mysql的依赖用于存储task相关的数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

3. 编写启动类

打开ServiceTaskApplication,在class上加入@EnableTask注解

4. 修改配置

1
2
3
4
5
6
7
8
9
10
11
12
13
server.port=8060
spring.application.name=service-task
#private
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud
spring.datasource.username=root
spring.datasource.password=xxx
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

主要配置jpa与datasource链接到mysql数据库,如果使用内存存储则不需要任何数据库配置

5. 编写方法

在main类中编写CommandLineRunner相关接口task会全部执行完

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@EnableTask
@SpringBootApplication
public class ServiceTaskApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceTaskApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner() {
return strings -> System.out.println("Hello World!");
}
@Bean
public CommandLineRunner commandLineRunner2() {
return strings -> System.out.println("Hello 2!");
}
}

5. 启动main

6. 验证

main方法运行后会直接退出,同时控制台会打印”Hello World!”与”Hello 2!”

疑问

这个干嘛用的?
官方叫做短命任务,就是执行一次就结束的任务,用于一些临时处理的代码场景,每次执行都会记录在数据库中执行的结果,正常,异常信息

------ 本文结束 ------

版权声明

dawell's Notes by Dawell is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Dawell创作并维护的dawell's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于dawell's Notes 博客( http://dawell.cc ),版权所有,侵权必究。

坚持原创技术分享,您的支持将鼓励我继续创作!