前言

工作需要使用实时任务,虽然该框架目前属于无人维护的状态,但综合考量下还是使用LTS。编译过程参考LTS框架,不再过多叙述,本文版本v1.7.2。

启动流程

  • 基础环境:这里选用Zookeeper、MongoDB以及MySQL作为前提条件进行启动,具体不展开
  • 按照官方文档编译出dist,备份好原有conf(很重要)
  • 启动lts-admin(含monitor):
    • 修改conf文件下的lts-admin.cfg,改为自己的版本,这里给个参考
      port=7777
      
      // 后台的用户名密码
      console.username=admin
      console.password=admin
      
      # 注册中心地址,可以是zk,也可以是redis
      registryAddress=zookeeper://127.0.0.1:2181
      # registryAddress=redis://127.0.0.1:6379
      
      # 集群名称
      clusterName=test_cluster
      
      # zk客户端,可选值 zkclient, curator
      configs.zk.client=zkclient
      
      # ------ 这个是Admin存储数据的地方,也可以和JobQueue的地址一样 ------
      configs.jdbc.url=jdbc:mysql://127.0.0.1:3306/lts
      configs.jdbc.username=root
      configs.jdbc.password=123456
      
      # admin 数据使用mysql 默认 mysql, 可以自行扩展
      jdbc.datasource.provider=mysql
      
      # 使用 可选值  fastjson, jackson
      # configs.lts.json=fastjson
      
      # 是否在admin启动monitor服务, monitor服务也可以单独启动
      lts.monitorAgent.enable=true
      
      #======================以下相关配置是JobTracker的JobQueue和JobLogger的相关配置 要保持和JobTracker一样==========================
      ## (可选配置)jobT. 开头的, 因为JobTracker和Admin可能使用的数据库不是同一个
      # LTS业务日志, 可选值 mysql, mongo
      jobT.job.logger=mongo
      # ---------以下是任务队列配置-----------
      # 任务队列,可选值 mysql, mongo
      jobT.job.queue=mongo
      
      # ------ 1. 如果是mysql作为任务队列 (如果不配置,表示和Admin的在一个数据库)------
      # jobT.jdbc.url=jdbc:mysql://127.0.0.1:3306/lts
      # jobT.jdbc.username=root
      # jobT.jdbc.password=123456
      
      # ------ 2. 如果是mongo作为任务队列 ------
      jobT.mongo.addresses=127.0.0.1:27017
      jobT.mongo.database=lts
      # jobT.mongo.username=xxx #如果有的话
      # jobT.mongo.password=xxx #如果有的话
      
      # admin 数据使用mysql 默认 mysql, 可以自行扩展
      # jobT.jdbc.datasource.provider=mysql

       

    • 同时修改lts-monitor.cfg(若上面monitor配置没有设为true则不用修改)
      
      # 注册中心地址,可以是zk,也可以是redis
      registryAddress=zookeeper://127.0.0.1:2181
      # registryAddress=redis://127.0.0.1:6379
      
      # 集群名称
      clusterName=test_cluster
      
      # LTS业务日志, 可选值 mysql, mongo
      configs.job.logger=mongo
      
      # zk客户端,可选值 zkclient, curator
      configs.zk.client=zkclient
      
      # ---------以下是任务队列配置-----------
      # 任务队列,可选值 mysql, mongo
      configs.job.queue=mongo
      
      # ------ 1. 如果是mysql作为任务队列 ------
      configs.jdbc.url=jdbc:mysql://127.0.0.1:3306/lts
      configs.jdbc.username=root
      configs.jdbc.password=123456
      
      # ------ 2. 如果是mongo作为任务队列 ------
      configs.mongo.addresses=127.0.0.1:27017
      configs.mongo.database=lts
      # configs.mongo.username=xxx #如果有的话
      # configs.mongo.password=xxx #如果有的话
      
      # admin 数据使用mysql, h2 默认 h2 embedded
      jdbc.datasource.provider=mysql
      
      # 使用 可选值  fastjson, jackson
      # configs.lts.json=fastjson

       

    • 然后进入bin/lts-admin start启动,注意留意有无错误,一般错误发生在外部环境连接不上或者任务队列两个之一没有配好账号密码(虽然它只用一个队列,但两种方式都要配好,应该算是个bug)。成功后访问admin地址,看到存在MONITOR节点即算成功
  • 启动jobtracker
    • 修改conf/zoo/jobtracker.cfg,核心还是同上,两个队列都要写
      
      # 注册中心地址,可以是zk,也可以是redis
      registryAddress=zookeeper://127.0.0.1:2181
      
      # JobTracker的监听端口
      listenPort=3502
      
      # 集群名称
      clusterName=test_cluster
      
      # LTS业务日志, 可选值 console, mysql, mongo
      configs.job.logger=mongo
      
      # zk客户端,可选值 zkclient, curator
      configs.zk.client=zkclient
      
      # ---------以下是任务队列配置-----------
      # 任务队列,可选值 mysql, mongo
      configs.job.queue=mongo
      
      # ------ 1. 如果是mysql作为任务队列 ------
      configs.jdbc.url=jdbc:mysql://127.0.0.1:3306/lts
      configs.jdbc.username=root
      configs.jdbc.password=123456
      
      # ------ 2. 如果是mongo作为任务队列 ------
      configs.mongo.addresses=127.0.0.1:27017
      configs.mongo.database=lts
      # configs.mongo.username=xxx #如果有的话
      # configs.mongo.password=xxx #如果有的话

       

    • 启动bin/jobtracker zoo start,无错误的话刷新admin网页,就看到第二个节点JOB_TRACKER出现了
  • 自定义tasktracker启动
    • 参考POM
      <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
          </dependency>
          
          <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
      	<dependency>
      	    <groupId>org.springframework.boot</groupId>
      	    <artifactId>spring-boot-starter</artifactId>
          </dependency>
      
      	<!-- 热部署 -->
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
          </dependency>
      
      	<dependency>
      	    <groupId>com.github.ltsopensource</groupId>
      	    <artifactId>lts-spring</artifactId>
      	    <version>1.7.2-SNAPSHOT</version>
      	</dependency>
          
           <dependency>
      	    <groupId>com.github.ltsopensource</groupId>
      	    <artifactId>lts-core</artifactId>
      	    <version>1.7.2-SNAPSHOT</version>
      	</dependency>
      	
      	 <dependency>
      	    <groupId>com.github.ltsopensource</groupId>
      	    <artifactId>lts-tasktracker</artifactId>
      	    <version>1.7.2-SNAPSHOT</version>
      	</dependency>
      	
      	<dependency>
      	    <groupId>com.alibaba</groupId>
      	    <artifactId>fastjson</artifactId>
      	    <version>1.2.58</version>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
      	<dependency>
      	    <groupId>io.netty</groupId>
      	    <artifactId>netty-all</artifactId>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/org.iq80.leveldb/leveldb -->
      	<dependency>
      	    <groupId>org.iq80.leveldb</groupId>
      	    <artifactId>leveldb</artifactId>
      	    <version>0.11</version>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/org.fusesource.leveldbjni/leveldbjni-all -->
      	<dependency>
      	    <groupId>org.fusesource.leveldbjni</groupId>
      	    <artifactId>leveldbjni-all</artifactId>
      	    <version>1.8</version>
      	</dependency>
      
      	<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
      	<dependency>
      	    <groupId>com.101tec</groupId>
      	    <artifactId>zkclient</artifactId>
      	    <version>0.11</version>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
      	<dependency>
      	    <groupId>org.apache.zookeeper</groupId>
      	    <artifactId>zookeeper</artifactId>
      	    <version>3.4.14</version>
      	    <type>pom</type>
      	</dependency>
        </dependencies>
        
        <dependencyManagement>
          <dependencies>
            <!--支持Spring Boot 2.1.X-->
            <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-dependencies</artifactId>
              <version>2.1.4.RELEASE</version>
              <type>pom</type>
              <scope>import</scope>
            </dependency>
          </dependencies>
        </dependencyManagement>

       

    • 参考配置文件
      lts:
        tasktracker:
          cluster-name: test_cluster
          registry-address: zookeeper://127.0.0.1:2181
          work-threads: 32
          node-group: test_TaskTracker
          dispatch-runner:
            enable: true
            #shard-value: taskId

       

    • 如果在Application启动@EnableTaskTracker的话就会使用默认的TaskTracker,目前会导致使用带上@JobRunner4TaskTracker装饰的自定义JobRunner出现Can not find JobRunner by Shard Value的bug
    • 在这里使用自定义Config来注册一个TaskTracker,参考代码
      @Configuration
      public class Config implements ApplicationContextAware {
      	private ApplicationContext applicationContext;
      	
      	@Value("${lts.tasktracker.cluster-name}")
      	private String clusterName;
      	
      	@Value("${lts.tasktracker.node-group}")
      	private String nodeGroup;
      	
      	@Value("${lts.tasktracker.work-threads}")
      	private Integer workThreads;
      	
      	@Value("${lts.tasktracker.registry-address}")
      	private String address;
      
          @Override
          public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
              this.applicationContext = applicationContext;
          }
      
          @Bean(name = "taskTracker")
          public TaskTracker getTaskTracker() throws Exception {
              TaskTrackerAnnotationFactoryBean factoryBean = new TaskTrackerAnnotationFactoryBean();
              factoryBean.setApplicationContext(applicationContext);
              factoryBean.setClusterName(clusterName);
              factoryBean.setJobRunnerClass(JobRunnerDispatcher.class);
              factoryBean.setWorkThreads(workThreads);
              factoryBean.setShardField("taskId"); // 待定
              factoryBean.setNodeGroup(nodeGroup);
              factoryBean.setBizLoggerLevel("INFO");
              factoryBean.setRegistryAddress(address);
      
              factoryBean.afterPropertiesSet();
              factoryBean.start();
              return factoryBean.getObject();
          }
      }

       

    • 用Application启动,无错误的话,会在admin网页看到一个TASK_TRACKER节点上线
  • 启动JobClient
    • 参考POM
      <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
          </dependency>
          
          <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
      	<dependency>
      	    <groupId>org.springframework.boot</groupId>
      	    <artifactId>spring-boot-starter</artifactId>
          </dependency>
      
      	<!-- 热部署 -->
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
          </dependency>
          
          <dependency>
      	  <groupId>org.springframework.boot</groupId>
      	  <artifactId>spring-boot-starter-web</artifactId>
      	</dependency>
          
           <dependency>
      	    <groupId>com.github.ltsopensource</groupId>
      	    <artifactId>lts-core</artifactId>
      	    <version>1.7.2-SNAPSHOT</version>
      	</dependency>
      	
      	<dependency>
      	    <groupId>com.github.ltsopensource</groupId>
      	    <artifactId>lts-spring</artifactId>
      	    <version>1.7.2-SNAPSHOT</version>
      	</dependency>
      	
      	 <dependency>
      	    <groupId>com.github.ltsopensource</groupId>
      	    <artifactId>lts-jobclient</artifactId>
      	    <version>1.7.2-SNAPSHOT</version>
      	</dependency>
      	
      	<dependency>
      	    <groupId>com.alibaba</groupId>
      	    <artifactId>fastjson</artifactId>
      	    <version>1.2.58</version>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
      	<dependency>
      	    <groupId>io.netty</groupId>
      	    <artifactId>netty-all</artifactId>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/org.iq80.leveldb/leveldb -->
      	<dependency>
      	    <groupId>org.iq80.leveldb</groupId>
      	    <artifactId>leveldb</artifactId>
      	    <version>0.11</version>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/org.fusesource.leveldbjni/leveldbjni-all -->
      	<dependency>
      	    <groupId>org.fusesource.leveldbjni</groupId>
      	    <artifactId>leveldbjni-all</artifactId>
      	    <version>1.8</version>
      	</dependency>
      
      	<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
      	<dependency>
      	    <groupId>com.101tec</groupId>
      	    <artifactId>zkclient</artifactId>
      	    <version>0.11</version>
      	</dependency>
      	
      	<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
      	<dependency>
      	    <groupId>org.apache.zookeeper</groupId>
      	    <artifactId>zookeeper</artifactId>
      	    <version>3.4.14</version>
      	    <type>pom</type>
      	</dependency>
      	
        </dependencies>
      	
        <dependencyManagement>
          <dependencies>
            <!--支持Spring Boot 2.1.X-->
            <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-dependencies</artifactId>
              <version>2.1.4.RELEASE</version>
              <type>pom</type>
              <scope>import</scope>
            </dependency>
          </dependencies>
        </dependencyManagement>

       

    • 参考配置文件
      lts:
        jobclient:
          cluster-name: test_cluster
          registry-address: zookeeper://127.0.0.1:2181
          node-group: test_jobClient
          use-retry-client: true

       

    • Application加上@EnableJobClient装饰
    • (可选)任务完成处理代码
      @JobCompletedHandler4JobClient
      public class JobCompletedHandlerImpl implements JobCompletedHandler {
      
      	@Override
      	public void onComplete(List<JobResult> jobResults) {
      		 // 任务执行反馈结果处理
              if (CollectionUtils.isNotEmpty(jobResults)) {
                  for (JobResult jobResult : jobResults) {
                      System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 任务执行完成:" + jobResult);
                  }
              }
      	}
      
      }

       

    • 启动后无错误可以看到JOB_CLIENT节点上线

BUG

目前admin网页里,打开任务日志查询不到数据,但是在MongoDB里是可以看到日志数据的,待解决

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐