最近打算研究一下elastic-job,同事都沒有這方面的使用經驗,自己打算看官方文檔跟蹤源碼一步步完善一系列的使用博客,并打算使用到工作中去。算是自己調研,自己推一個框架,希望完成。
簡介
Elastic-Job是一個分布式調度解決方案,由兩個相互獨立的子項目Elastic-Job-Lite和Elastic-Job-Cloud組成。
Elastic-Job-Lite定位為輕量級無中心化解決方案,使用jar包的形式提供分布式任務的協調服務。
官方主頁
Elastic-Job-Lite官方文檔
Elastic-Job-Cloud官方文檔
使用的是官方最新的release版本2.1.5,下載最新的源碼,
git clone https://github.com/dangdangdotcom/elastic-job.git
下載之后的文件目錄,
elastic-job-example目錄中包括Elastic-Job-Lite和Elastic-Job-Cloud的幾個demo。本篇主要講解elastic-job-example-lite的快速入門,elastic-job-example-lite-java項目。
快速入門
關于Elastic-Job-Lite的一些簡介,可以看官網文檔
Elastic-Job-Lite定位為輕量級無中心化解決方案,使用jar包的形式提供最輕量級的分布式任務的協調服務,外部依賴僅Zookeeper。
環境準備
maven(不會的自己百度)
Zookeeper(最好是3.4.6版本以上,網上的安裝教程太多)
jdk1.7+
將elastic-job-example導入到idea中,
修改JavaMain的部分代碼:
public final class JavaMain {
private static final int EMBED_ZOOKEEPER_PORT = 2181;
private static final String ZOOKEEPER_CONNECTION_STRING = "127.0.0.1:" + EMBED_ZOOKEEPER_PORT;
private static final String JOB_NAMESPACE = "elastic-job-example-lite-java";
// switch to MySQL by yourself
private static final String EVENT_RDB_STORAGE_DRIVER = "com.mysql.jdbc.Driver";
private static final String EVENT_RDB_STORAGE_URL = "jdbc:mysql://localhost:3306/elastic_job_log";
private static final String EVENT_RDB_STORAGE_USERNAME = "root";
private static final String EVENT_RDB_STORAGE_PASSWORD = "root";
//h2數據庫
// private static final String EVENT_RDB_STORAGE_DRIVER = "org.h2.Driver";
//
// private static final String EVENT_RDB_STORAGE_URL = "jdbc:h2:mem:job_event_storage";
//
// private static final String EVENT_RDB_STORAGE_USERNAME = "sa";
//
// private static final String EVENT_RDB_STORAGE_PASSWORD = "";
// CHECKSTYLE:OFF
public static void main(final String[] args) throws IOException {
// CHECKSTYLE:ON
EmbedZookeeperServer.start(EMBED_ZOOKEEPER_PORT);
CoordinatorRegistryCenter regCenter = setUpRegistryCenter();
JobEventConfiguration jobEventConfig = new JobEventRdbConfiguration(setUpEventTraceDataSource());
setUpSimpleJob(regCenter, jobEventConfig);
setUpDataflowJob(regCenter, jobEventConfig);
setUpScriptJob(regCenter, jobEventConfig);
}
private static CoordinatorRegistryCenter setUpRegistryCenter() {
ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(ZOOKEEPER_CONNECTION_STRING, JOB_NAMESPACE);
CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
result.init();
return result;
}
private static DataSource setUpEventTraceDataSource() {
BasicDataSource result = new BasicDataSource();
result.setDriverClassName(EVENT_RDB_STORAGE_DRIVER);
result.setUrl(EVENT_RDB_STORAGE_URL);
result.setUsername(EVENT_RDB_STORAGE_USERNAME);
result.setPassword(EVENT_RDB_STORAGE_PASSWORD);
return result;
}
private static DataSource setUpEventTraceDataSource() {
BasicDataSource result = new BasicDataSource();
result.setDriverClassName(EVENT_RDB_STORAGE_DRIVER);
result.setUrl(EVENT_RDB_STORAGE_URL);
result.setUsername(EVENT_RDB_STORAGE_USERNAME);
result.setPassword(EVENT_RDB_STORAGE_PASSWORD);
return result;
}
//Simple類型作業,需要實現SimpleJob接口。該接口僅提供單一方法用于覆蓋,此方法將定時執行。與Quartz原生接口相似,但提供了彈性擴縮容和分片等功能。
private static void setUpSimpleJob(final CoordinatorRegistryCenter regCenter, final JobEventConfiguration jobEventConfig) {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder("javaSimpleJob", "0/5 * * * * ?", 3).shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou").build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, JavaSimpleJob.class.getCanonicalName());
new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build(), jobEventConfig).init();
}
//Dataflow類型作業,需實現DataflowJob接口。該接口提供2個方法可供覆蓋,分別用于抓取(fetchData)和處理(processData)數據。
private static void setUpDataflowJob(final CoordinatorRegistryCenter regCenter, final JobEventConfiguration jobEventConfig) {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder("javaDataflowElasticJob", "0/10 * * * * ?", 3).shardingItemParameters("0=Beijing,1=Shanghai,2=Guangzhou").build();
DataflowJobConfiguration dataflowJobConfig = new DataflowJobConfiguration(coreConfig, JavaDataflowJob.class.getCanonicalName(), true);
new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(dataflowJobConfig).build(), jobEventConfig).init();
}
//Script類型作業,支持shell,python,perl等所有類型腳本。只需通過控制臺或代碼配置scriptCommandLine即可,
private static void setUpScriptJob(final CoordinatorRegistryCenter regCenter, final JobEventConfiguration jobEventConfig) throws IOException {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder("scriptElasticJob", "0/15 * * * * ?", 3).build();
ScriptJobConfiguration scriptJobConfig = new ScriptJobConfiguration(coreConfig, buildScriptCommandLine());
new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(scriptJobConfig).build(), jobEventConfig).init();
}
private static String buildScriptCommandLine() throws IOException {
if (System.getProperties().getProperty("os.name").contains("Windows")) {
return Paths.get(JavaMain.class.getResource("/script/demo.bat").getPath().substring(1)).toString();
}
Path result = Paths.get(JavaMain.class.getResource("/script/demo.sh").getPath());
Files.setPosixFilePermissions(result, PosixFilePermissions.fromString("rwxr-xr-x"));
return result.toString();
}
}
官網默認選用的是h2數據庫(內存數據庫),切換到mysql(自己新建elastic_job_log數據庫)。
編譯elastic-job-lite控制臺
到下載的源碼路徑下elastic-job/elastic-job-lite-console編譯,
mvn install
編譯之后在target下面的到elastic-job-lite-console-2.1.5.tar.gz包,解壓運行,
tar -zxvf elastic-job-lite-console-2.1.5.tar.gz
cd elastic-job-lite-console-2.1.5/bin
? ll
total 16
-rwxr-xr-x 1 naeshihiroshi staff 459B 7 25 21:19 start.bat
-rwxr-xr-x 1 naeshihiroshi staff 582B 7 25 21:19 start.sh
? ./start.sh
[INFO ] 2017-07-26 10:55:32,306 --main-- [com.dangdang.ddframe.job.restful.RestfulServer] Elastic Job: Start RESTful server
[INFO ] 2017-07-26 10:55:32,330 --main-- [org.eclipse.jetty.server.Server] jetty-8.1.19.v20160209
[INFO ] 2017-07-26 10:55:32,652 --main-- [org.eclipse.jetty.server.AbstractConnector] Started SelectChannelConnector@0.0.0.0:8899
根據不同的環境執行不同的腳本,
配置注冊中心:
執行上面的JavaMain方法,啟動了三個作業,可以在控制臺上顯示,
JavaMain方法中執行了三個定時任務,分別是
a.Simple類型作業
意為簡單實現,未經任何封裝的類型。需實現SimpleJob接口。該接口僅提供單一方法用于覆蓋,此方法將定時執行。與Quartz原生接口相似,但提供了彈性擴縮容和分片等功能。
b.Dataflow類型作業
Dataflow類型用于處理數據流,需實現DataflowJob接口。該接口提供2個方法可供覆蓋,分別用于抓取(fetchData)和處理(processData)數據。
c. Script類型作業
Script類型作業意為腳本類型作業,支持shell,python,perl等所有類型腳本。只需通過控制臺或代碼配置scriptCommandLine即可,無需編碼。執行腳本路徑可包含參數,參數傳遞完畢后,作業框架會自動追加最后一個參數為作業運行時信息。
這一篇博客先講到這里。