參考github
-
克隆程式碼
git clone github.com/ltsopensour…
-
執行根目錄下的sh build.sh或build.cmd指令碼,會在dist目錄下生成 lts-{version}-bin zip包,解壓資料夾
下面是其目錄結構,其中bin目錄主要是JobTracker和LTS-Admin的啟動指令碼。
jobtracker 中是 JobTracker的配置檔案和需要使用到的jar包,
lts-admin是LTS-Admin相關的war包和配置檔案。 lts-{version}-bin的檔案結構
-- lts-${version}-bin
|-- bin
| |-- jobtracker.cmd
| |-- jobtracker.sh
| |-- lts-admin.cmd
| |-- lts-admin.sh
| |-- lts-monitor.cmd
| |-- lts-monitor.sh
| |-- tasktracker.sh
|-- conf
| |-- log4j.properties
| |-- lts-admin.cfg
| |-- lts-monitor.cfg
| |-- readme.txt
| |-- tasktracker.cfg
| |-- zoo
| |-- jobtracker.cfg
| |-- log4j.properties
| |-- lts-monitor.cfg
|-- lib
| |-- *.jar
|-- war
|-- jetty
| |-- lib
| |-- *.jar
|-- lts-admin.war
複製程式碼
- JobTracker啟動
如果你想啟動一個節點,直接修改下conf/zoo下的配置檔案,然後執行 sh jobtracker.sh zoo start即可,如果你想啟動兩個JobTracker節點, 那麼你需要拷貝一份zoo,譬如命名為zoo2,修改下zoo2下的配置檔案, 然後執行sh jobtracker.sh zoo2 start即可。logs資料夾下生成 jobtracker-zoo.out日誌
1.修改配置檔案 lts-${version}-SNAPSHOT-bin/conf/zoo目錄下的: jobtracker.cfg 、 lts-monitor.cfg ,主要修改zookeeper、mysql的配置資訊
修改了registryAddress為redis, mysql配置
2.同上修改點
3.執行JobTracker
cd bin/ #進入lts-${version}-SNAPSHOT-bin/bin
sh jobtracker.sh zoo start
- LTS-Admin啟動
修改conf/lts-monitor.cfg和conf/lts-admin.cfg下的配置, 然後執行bin下的sh lts-admin.sh或lts-admin.cmd指令碼即可。 logs資料夾下會生成lts-admin.out日誌,啟動成功在日誌中會列印出訪問地址, 使用者可以通過這個訪問地址訪問了
1.修改配置檔案lts-1.7.1-SNAPSHOT-bin/conf目錄下的:lts-admin.cfg 、 lts-monitor.cfg ,主要修改zookeeper、mysql的配置資訊
2.執行LTS-Admin後臺管理Web
cd bin/ #進入lts-${version}-SNAPSHOT-bin/bin
sh lts-admin.sh start
3.訪問LTS-Admin後臺管理Web
訪問:http://172.20.48.141:8081/index.htm
1.啟動TaskTracker和JobClient
- 配置
lts:
taskTrackerNodeGroup: testtaskTracker
remoting: mina
tasktracker:
cluster-name: test_cluster
registry-address: redis://127.0.0.1:6379
node-group: testtaskTracker
configs:
job:
fail:
store: mapdb
jobclient:
cluster-name: test_cluster
registry-address: redis://127.0.0.1:6379
node-group: testjobClient
use-retry-client: true
configs:
job:
fail:
store: mapdb
複製程式碼
- 註冊定時Job
package com.example.lts.job.client;
import com.example.lts.job.LtsConfig;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.jobclient.JobClient;
import com.github.ltsopensource.jobclient.domain.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class JobRegister implements InitializingBean {
@Autowired
private LtsConfig ltsConfig;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private JobClient jobClient;
@Override
public void afterPropertiesSet() throws Exception {
for (JobSchedule jobSchedule: JobSchedule.values()) {
Job job = new Job();
job.setTaskId(jobSchedule.getTaskId());
job.setTaskTrackerNodeGroup(ltsConfig.getTaskTrackerNodeGroup());
job.setNeedFeedback(jobSchedule.isNeedFeedback());
job.setReplaceOnExist(jobSchedule.isReplaceOnExist());
if (jobSchedule.isCronJob()) {
job.setCronExpression(jobSchedule.getCronExpression());
}
Map<String, String> params = jobSchedule.getParams();
if (params != null) {
for (Map.Entry<String, String> param: params.entrySet()) {
job.setParam(param.getKey(), param.getValue());
}
}
Response response = jobClient.submitJob(job);
}
}
}
複製程式碼
讀取配置(這裡是用的列舉)
package com.example.lts.job.client;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
@Getter
public enum JobSchedule {
/** 測試Job */
TEST_JOB("TEST_JOB", buildParams(new String[] { "key1" }, new String[] { "value1" }), false, true, true,
"0 0/1 * * * ?");
private String taskId;
private Map<String, String> params = new HashMap<>();
private boolean needFeedback;
private boolean replaceOnExist;
private boolean isCronJob;
private String cronExpression;
JobSchedule(String taskId, Map<String, String> params, boolean needFeedback, boolean replaceOnExist,
boolean isCronJob, String cronExpression) {
this.taskId = taskId;
this.params = params;
this.needFeedback = needFeedback;
this.replaceOnExist = replaceOnExist;
this.isCronJob = isCronJob;
this.cronExpression = cronExpression;
}
private static Map<String, String> buildParams(String[] keys, String[] values) {
Map<String, String> params = new HashMap<>();
if (keys.length != values.length) {
throw new RuntimeException("job引數鍵的個數和值的個數不一樣");
}
for (int i = 0; i < keys.length; i++) {
params.put(keys[i], values[i]);
}
return params;
}
}
複製程式碼
2.執行Job
- 新增執行方法(繼承JobRunner)
在JobDistribute中run方法執行Job
package com.example.lts.job.task;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.core.logger.Logger;
import com.github.ltsopensource.core.logger.LoggerFactory;
import com.github.ltsopensource.spring.boot.annotation.JobRunner4TaskTracker;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.logger.BizLogger;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
@JobRunner4TaskTracker
public class JobDistribute implements JobRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(JobDistribute.class);
@Override
public Result run(JobContext jobContext) throws Throwable {
try {
BizLogger bizLogger = jobContext.getBizLogger();
LOGGER.info("jobContext:" + jobContext);
final Job job = jobContext.getJob();
// 會傳送到 LTS (JobTracker上)
bizLogger.info("測試,業務日誌");
runJob(job);
} catch (Exception e) {
LOGGER.info("JOb_EXECUTE_FAILED", e);
return new Result(Action.EXECUTE_FAILED, e.getMessage());
}
return new Result(Action.EXECUTE_SUCCESS, "EXECUTE_SUCCESS");
}
private void runJob(Job job) {
final String taskId = job.getTaskId();
if (JobInfo.valuesOfTaskId(taskId) != null) {
final JobInfo jobInfo = JobInfo.valueOf(taskId);
final String className = jobInfo.getClassName();
try {
final Class<?> jobClass = Class.forName(className);
final Object jobInstance = jobClass.newInstance();
SchedulerJob schedulerJob = (SchedulerJob) jobInstance;
schedulerJob.perform(job);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
LOGGER.error(e);
}
} else {
LOGGER.error("找不到task_id對應的job實現, taskId: " + taskId);
}
}
}
複製程式碼
- 設定TaskTracker執行配置
package com.example.lts.job.task;
import com.google.common.collect.ImmutableMap;
import lombok.Getter;
@Getter
public enum JobInfo {
/** 測試Job */
TEST_JOB("TEST_JOB", "com.example.lts.job.task.jobs.TestJob");
private static final ImmutableMap<String, JobInfo> CACHE;
static {
final ImmutableMap.Builder<String, JobInfo> builder = ImmutableMap.builder();
for (JobInfo jobInfo: values()) {
builder.put(jobInfo.getTaskId(), jobInfo);
}
CACHE = builder.build();
}
private String taskId;
private String className;
JobInfo(String taskId, String className) {
this.taskId = taskId;
this.className = className;
}
public static JobInfo valuesOfTaskId(String taskId) {
return CACHE.get(taskId);
}
}
複製程式碼
- 手動提交Job(前提:Job已經提前在JobInfo中配置,無論是及時還定時)
package com.example.lts.job.client;
import com.example.lts.job.LtsConfig;
import com.github.ltsopensource.jobclient.JobClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class JobSubmiter {
@Autowired
private LtsConfig ltsConfig;
@Autowired
private JobClient jobClient;
// public Response submitJob(JobVO jobVO) {
// Job job = new Job();
// job.setTaskId(jobVO.getTaskId());
// job.setTaskTrackerNodeGroup(ltsConfig.getTaskTrackerNodeGroup());
// job.setTriggerTime(new Date().getTime() + 1000);
// final Map<String, String> params = jobVO.getParams();
// if (params != null) {
// for (Map.Entry<String, String> param: params.entrySet()) {
// job.setParam(param.getKey(), param.getValue());
// }
// }
// return jobClient.submitJob(job);
// }
}
複製程式碼