其他分享
首页 > 其他分享> > elasticjob学习一:simplejob初识和springboot整合

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

作者:互联网

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

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

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

搭建springboot项目

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.1.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.kevin</groupId>
   <artifactId>es-job</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>es-job</name>
   <description>Demo project for Spring Boot</description>

   <properties>
       <java.version>1.8</java.version>
       <elastic-job.version>2.1.4</elastic-job.version>
   </properties>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>

       <!--  elastic-job dependency -->
       <dependency>
           <groupId>com.dangdang</groupId>
           <artifactId>elastic-job-lite-core</artifactId>
           <version>${elastic-job.version}</version>
       </dependency>
       <dependency>
           <groupId>com.dangdang</groupId>
           <artifactId>elastic-job-lite-spring</artifactId>
           <version>${elastic-job.version}</version>
       </dependency>

       <!-- spring boot dependency -->

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <optional>true</optional>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-jdbc</artifactId>
       </dependency>
       <!-- mysql驱动 -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-jpa</artifactId>
       </dependency>
   </dependencies>

   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>

</project>

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

    <!--  elastic-job dependency -->
       <dependency>
           <groupId>com.dangdang</groupId>
           <artifactId>elastic-job-lite-core</artifactId>
           <version>${elastic-job.version}</version>
       </dependency>
       <dependency>
           <groupId>com.dangdang</groupId>
           <artifactId>elastic-job-lite-spring</artifactId>
           <version>${elastic-job.version}</version>
       </dependency>

整合elasticjob

1.编写配置application.properties


# 注册中心
zookeeper.address=192.168.247.7:2181
zookeeper.namespace=elastic-job
zookeeper.connectionTimeout=10000
zookeeper.sessionTimeout=10000
zookeeper.maxRetries=3

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


# mysql设置
spring.datasource.url=jdbc:mysql://localhost:3306/elasticjob?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


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<? extends SimpleJob> 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下面的运行文件

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

标签:springboot,simpleJob,spring,simplejob,boot,Value,job,elasticjob,final
来源: https://www.cnblogs.com/zhenghengbin/p/12230300.html