elasticjob学习一:simplejob初识和springboot整合

Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成。
Elastic-Job-Lite定位为轻量级无中心化解决方案,使用jar包的形式提供分布式任务的协调服务; --摘自官网

具体的详细介绍,大家可以去官网查阅

这篇文章主要是整合springboot 的简单例子。通过一步一步实现,来逐步的熟悉elastic-job 这个组件,首要条件就是需要你有个运行的zookeeper

搭建springboot项目

这里不在赘述如果搭建,这里我只贴出我的pom文件



   4.0.0
   
       org.springframework.boot
       spring-boot-starter-parent
       2.0.1.RELEASE
        
   
   com.kevin
   es-job
   0.0.1-SNAPSHOT
   es-job
   Demo project for Spring Boot

   
       1.8
       2.1.4
   

   
       
           org.springframework.boot
           spring-boot-starter
       

       
           org.springframework.boot
           spring-boot-starter-test
           test
       

       
       
           com.dangdang
           elastic-job-lite-core
           ${elastic-job.version}
       
       
           com.dangdang
           elastic-job-lite-spring
           ${elastic-job.version}
       

       

       
           org.springframework.boot
           spring-boot-starter-web
       
       
           org.springframework.boot
           spring-boot-starter-actuator
       
       
           org.springframework.boot
           spring-boot-configuration-processor
           true
       

       
           org.springframework.boot
           spring-boot-starter-jdbc
       
       
       
           mysql
           mysql-connector-java
       
       
           org.springframework.boot
           spring-boot-starter-data-jpa
       
   

   
       
           
               org.springframework.boot
               spring-boot-maven-plugin
           
       
   



主要的就是加载这2个依赖

    
       
           com.dangdang
           elastic-job-lite-core
           ${elastic-job.version}
       
       
           com.dangdang
           elastic-job-lite-spring
           ${elastic-job.version}
       

整合elasticjob

1.编写配置application.yml



zookeeper:
 address: 192.168.247.7:2181
 namespace: elastic-job
 connectionTimeout: 10000
 sessionTimeout: 10000
 maxRetries: 3

# simplejob配置
simpleJob:
   cron: 0/5 * * * * ?
   shardingTotalCount: 5
   shardingItemParameters: 0=java,1=php,2=erlang,3=angular,4=vue
   jobParameter: source1=public,source2=private
   failover: true
   monitorExecution: true
   monitorPort: 8889
   maxTimeDiffSeconds: -1
   jobShardingStrategyClass: com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy

dataflowJob:
   cron: 0/10 * * * * ?
   shardingTotalCount: 2
   shardingItemParameters: 0=jinan,1=qingdao




############################################################
#
# 配置数据源信息
#
############################################################
spring:
 datasource: # 数据源的相关配置
     type: com.zaxxer.hikari.HikariDataSource          # 数据源类型:HikariCP
     driver-class-name: com.mysql.jdbc.Driver          # mysql驱动
     url: jdbc:mysql://localhost:3306/elasticjob?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
     username: root
     password: root
     hikari:
       connection-timeout: 30000       # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒
       minimum-idle: 5                 # 最小连接数
       maximum-pool-size: 20           # 最大连接数
       auto-commit: true               # 自动提交
       idle-timeout: 600000            # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
       pool-name: DateSourceHikariCP     # 连接池名字
       max-lifetime: 1800000           # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms
       connection-test-query: SELECT 1






2.注册中心加载到spring 容器中

/**
* @author: kevin
* @Date: 2020/1/22
*/
@Configuration
@ConditionalOnExpression("'${zookeeper.address}'.length() > 0")
public class RegistryCenterConfig {

   /**
    * 	把注册中心加载到spring 容器中
    * @return
    */
   @Bean(initMethod = "init")
   public ZookeeperRegistryCenter registryCenter(@Value("${zookeeper.address}") final String serverLists,
                                                 @Value("${zookeeper.namespace}") final String namespace,
                                                 @Value("${zookeeper.connectionTimeout}") final int connectionTimeout,
                                                 @Value("${zookeeper.sessionTimeout}") final int sessionTimeout,
                                                 @Value("${zookeeper.maxRetries}") final int maxRetries) {
       ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverLists, namespace);
       zookeeperConfiguration.setConnectionTimeoutMilliseconds(connectionTimeout);
       zookeeperConfiguration.setSessionTimeoutMilliseconds(sessionTimeout);
       zookeeperConfiguration.setMaxRetries(maxRetries);

       return new ZookeeperRegistryCenter(zookeeperConfiguration);

   }
}

3.配置JobEventConfig事件追踪

/**
* @author: kevin
* @Date: 2020/1/22
*/
@Configuration
public class JobEventConfig {

   @Autowired
   private DataSource dataSource;

   @Bean
   public JobEventConfiguration jobEventConfiguration() {
       return new JobEventRdbConfiguration(dataSource);
   }

}

4.编写自己的job并完成配置

4.1 定义自己的job

/**
* @author: kevin
* @Date: 2020/1/22
*/

@Component
public class MySimpleJob implements SimpleJob {
   @Override
   public void execute(ShardingContext shardingContext) {
       System.out.println(shardingContext.getShardingParameter());
   }
}


4.2 编写job配置

@Configuration
public class MySimpleJobConfig {
   @Autowired
   private ZookeeperRegistryCenter registryCenter;

   @Autowired
   private JobEventConfiguration jobEventConfiguration;

   /**
    * 	具体真正的定时任务执行逻辑
    * @return
    */
   @Bean
   public SimpleJob simpleJob() {
       return new MySimpleJob();
   }

   /**
    * @param simpleJob
    * @return
    */
   @Bean(initMethod = "init")
   public JobScheduler simpleJobScheduler(final SimpleJob simpleJob,
                                          @Value("${simpleJob.cron}") final String cron,
                                          @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount,
                                          @Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters,
                                          @Value("${simpleJob.jobParameter}") final String jobParameter,
                                          @Value("${simpleJob.failover}") final boolean failover,
                                          @Value("${simpleJob.monitorExecution}") final boolean monitorExecution,
                                          @Value("${simpleJob.monitorPort}") final int monitorPort,
                                          @Value("${simpleJob.maxTimeDiffSeconds}") final int maxTimeDiffSeconds,
                                          @Value("${simpleJob.jobShardingStrategyClass}") final String jobShardingStrategyClass) {

       return new SpringJobScheduler(simpleJob,
               registryCenter,
               getLiteJobConfiguration(simpleJob.getClass(),
                       cron,
                       shardingTotalCount,
                       shardingItemParameters,
                       jobParameter,
                       failover,
                       monitorExecution,
                       monitorPort,
                       maxTimeDiffSeconds,
                       jobShardingStrategyClass),
               jobEventConfiguration,
               new SimpleJobListener());

   }


   private LiteJobConfiguration getLiteJobConfiguration(Class jobClass, String cron,
                                                        int shardingTotalCount, String shardingItemParameters, String jobParameter, boolean failover,
                                                        boolean monitorExecution, int monitorPort, int maxTimeDiffSeconds, String jobShardingStrategyClass) {

       //定义作业核心配置
       JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration
               .newBuilder(jobClass.getName(), cron, shardingTotalCount)
               .misfire(true)
               .failover(failover)
               .jobParameter(jobParameter)
               .shardingItemParameters(shardingItemParameters)
               .build();

       //定义SIMPLE类型配置
       SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName());

       //定义Lite作业根配置
       LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration)
               .jobShardingStrategyClass(jobShardingStrategyClass)
               .monitorExecution(monitorExecution)
               .monitorPort(monitorPort)
               .maxTimeDiffSeconds(maxTimeDiffSeconds)
               .overwrite(true)
               .build();

       return liteJobConfiguration;
   }

运行

这里直接启动后,可以看到控制台打印

java
php
erlang
angular
vue

运维平台

elastic-job也提供运维平台,大家可以去官网下载源码,然后本地install下。找到elastic-job-lite-console-3.0.0.M1-SNAPSHOT.tar 解压后,直接运行bin下面的运行文件

如图所示,添加自己的配置

elasticjob学习一:simplejob初识和springboot整合_第1张图片

elasticjob学习一:simplejob初识和springboot整合_第2张图片

elasticjob学习一:simplejob初识和springboot整合_第3张图片

你可能感兴趣的:(elasticjob学习一:simplejob初识和springboot整合)