栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpingBoot集成ElaticJob定时器

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpingBoot集成ElaticJob定时器

POM文件配置

    4.0.0

    com.example
    demojob
    0.0.1-SNAPSHOT
    jar

    demojob
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        

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


        
        
            elastic-job-common-core
            com.dangdang
            2.1.5
        
        
            elastic-job-lite-core
            com.dangdang
            2.1.5
        
        
            elastic-job-lite-spring
            com.dangdang
            2.1.5
        
        
            elastic-job-cloud-executor
            com.dangdang
            2.1.5
        


        
        
            org.mariadb.jdbc
            mariadb-java-client
            1.5.4
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        
        
        
            com.baomidou
            mybatisplus-spring-boot-starter
            1.0.5
        
        
            com.baomidou
            mybatis-plus
            2.1.9
        


    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
yaml文件配置(也可以用application.properties一样的)
# 配置配置数据源spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.mariadb.jdbc.Driver
    name: elastic-job-event    url: jdbc:mariadb://127.0.0.1:3306/elasticjob
    username: root    password: 123456
    druid:
      validationQuery: SELECT 1
      initialSize: 10
      minIdle: 10
      maxActive: 200
      minEvictableIdleTimeMillis: 180000
      testOnBorrow: false
      testWhileIdle: true
      removeAbandoned: true
      removeAbandonedTimeout: 1800
      logAbandoned: true
      poolPreparedStatements: true
      maxOpenPreparedStatements: 100# 配置ZookeeperregCenter:
  serverList: localhost:2181
  namespace: hulk_order_task# 配置定时器规则simpleJob:
  cron: 0/5 * * * * ?  shardingTotalCount: 1
  shardingItemParameters: 0=1
开始写代码

RegistryCenterConfig

package com.example.demojob.config;import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.condition.ConditionalOnexpression;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@ConditionalOnexpression("'${regCenter.serverList}'.length() > 0")
public class RegistryCenterConfig {
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList,
                                             @Value("${regCenter.namespace}") final String namespace) {        return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
    }
}

JobEventConfig

package com.example.demojob.config;import com.dangdang.ddframe.job.event.JobEventConfiguration;import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.annotation.Resource;import javax.sql.DataSource;@Configurationpublic class JobEventConfig {
    @Resource
    private DataSource dataSource;

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

SimpleJobConfig

package com.example.demojob.config;import com.dangdang.ddframe.job.config.JobCoreConfiguration;import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;import com.dangdang.ddframe.job.event.JobEventConfiguration;import com.dangdang.ddframe.job.lite.api.JobScheduler;import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;import com.example.demojob.job.TestSimpleJob;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.annotation.Resource;@Configurationpublic class SimpleJobConfig {    
    @Resource
    private ZookeeperRegistryCenter regCenter;    
    @Resource
    private JobEventConfiguration jobEventConfiguration;    
    @Resource
    private TestSimpleJob simpleJob;    
    @Bean(initMethod = "init")    public JobScheduler simpleJobScheduler(@Value("${simpleJob.cron}") final String cron,
                                           @Value("${simpleJob.shardingTotalCount}") final int shardingTotalCount,
                                           @Value("${simpleJob.shardingItemParameters}") final String shardingItemParameters) {        return new SpringJobScheduler(simpleJob, regCenter,
                getLiteJobConfiguration(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters),
                jobEventConfiguration);
    }    
    private LiteJobConfiguration getLiteJobConfiguration(final Class jobClass, final String cron,                                                         final int shardingTotalCount, final String shardingItemParameters) {        return LiteJobConfiguration
                .newBuilder(                        new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount)
                                .shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName()))
                .overwrite(true).build();
    }
}

TestSimpleJob,定时器任务本身

package com.example.demojob.job;import com.dangdang.ddframe.job.api.ShardingContext;import com.dangdang.ddframe.job.api.simple.SimpleJob;import org.springframework.stereotype.Component;@Componentpublic class TestSimpleJob implements SimpleJob {    private int count;    //任务就是每5秒执行一次控制台输出1,2,3……
    @Override
    public void execute(ShardingContext shardingContext) {
        count++;
        System.out.println("task " + count);
    }
}
最后在Docker下安装 Zookeeper

安装脚本compose文件如下

version: '2'services:
  zookeeper01:
    image: zookeeper
    restart: always
    hostname: zookeeper01
    ports:
      - 2181:2181
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zookeeper02:2888:3888 server.3=zookeeper03:2888:3888
  zookeeper02:
    image: zookeeper
    restart: always
    hostname: zookeeper02
    ports:
      - 2182:2181
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zookeeper01:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zookeeper03:2888:3888
  zookeeper03:
    image: zookeeper
    restart: always
    hostname: zookeeper03
    ports:
      - 2183:2181
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zookeeper01:2888:3888 server.2=zookeeper02:2888:3888 server.3=0.0.0.0:2888:3888

         


作者:面皮大师
链接:https://www.jianshu.com/p/62ebedc764b7

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235901.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号