序文
タイミング タスク コンポーネントはワーク プロセスでよく使用されます。スタンドアロン ノードでは、Spring独自のタイミング タスク コンポーネントHubble-taskを使用することを直接選択できます。このタイミング タスクが確認され、タイミング トリガー戦略で固定化されると、動的にオン/オフを切り替えることはできないため、後にQuartzが登場しました。
Quartzは、 JAVAによって開発された時限タスク分野のオープンソース プロジェクトで、APIを通じて時限タスクの開始、停止、戦略をスケジュールでき、 JTAトランザクションやクラスターのサポートなどの強力な機能を備えています。
ただし、Quartz にはいくつかの欠点があります。
- Quartz はタイミングタスクを調整し、本質的にビジネスシステムから分離されていないAPIを通じてスケジュールする必要があります。
- Quartz は基礎となるデータ テーブルにデータを永続化する必要があり、ビジネス システムへのデータ侵入は比較的頻繁です。
- Quartz は分散スケジューリングの実行もサポートしておらず、個別に実行できるのは 1 つのタスクのみです
elastic -job は、Quartz をベースにした Dangdang の 2 番目のパッケージです。elastic-jobには 2 つのバージョンがあります。
- Elastic-Job-Cloud : マイクロサービスのデプロイ方法
- Elastic-Job-Lite : 登録センターとしての動物園の飼育員に基づくデプロイメント
これら 2 つのバージョンは、デプロイメント方法が異なることを除いて、APIでは同じです。Elastic -jobには、 Quartzと比較して多くの新機能が追加されています。
- サポート UI ページ。タイミング戦略を動的に調整し、Web ページ上で開始および停止できます。
- 分散スケジューリングとしての Zookeeper に基づいて、スケジューリングはタスクから切り離されます。
- 分散スケジューリングのシャーディングをサポートし、同じタスクを複数の部分に分割して実行できます。
- さまざまなジョブ タイプ、Simple、DataFLow データ フロー、Script スクリプトのサポート
- フェイルオーバー。オフラインマシンのタスクは実行のために再シャーディングされます。
- ジョブのシャーディングの一貫性。シャーディング後にタスクは繰り返されません。
- 実行を逃した場合のジョブ補償
インストール
Elastic-job-liteをインストールするには、事前にZookeeper をインストールする必要があります。インストール チュートリアルが必要な場合は、この記事を参照してください: Linux に Zookeeper Online をインストールする
Apache の elastic-job のアドレス: elasticjob
次に、Elastic-Job-Lite とビジネス コードを含む jar ファイルを実行する必要があります。jar や war の起動方法に限定されません。
送信元アドレス: elastic-job-lite
elastic-job-lite-console を開始します
バージョン 2.1.4 のソース コードをダウンロードします。
https://codeload.github.com/apache/shardingsphere-elasticjob/zip/refs/tags/2.1.4
ダウンロードが完了して解凍すると、次のディレクトリが表示されます。
elastic-job- lite ファイルの下にelastic-job- lite -consoleと入力します。このディレクトリにパッケージする、パッケージコマンド
mvn clean install -Dmaven.test.skip=true
パッケージ化後、jarパッケージを開始するか、ソース コードを直接開始し、コンソールモジュールの下でConsoleBootstrapクラスを見つけて開始します。
起動が完了したら、ip:8899にアクセスします。アカウントのパスワードはroot/rootです。
システムに入ったら、登録センターの設定を入力し、接続するために登録する必要がある動物園管理者のアドレスを入力します。
以下はLinuxでのインストール構成です。パッケージ化されたパッケージをLinuxに直接入れて実行することもできます。
コンソールをインストールする
elastic-job 3.0以降、コンソールモジュールはなくなり、より美しい UI コンソールが追加されました。
コンソール パッケージをここから直接ダウンロードします。
https://archive.apache.org/dist/shardingsphere/elasticjob-ui-3.0.0-RC1/apache-shardingsphere-elasticjob-3.0.0-RC1-lite-ui-bin.tar.gz
ダウンロード後、サーバーにアップロードして解凍します
tar -zxvf apache-shardingsphere-elasticjob-3.0.0-RC1-lite-ui-bin.tar.gz
bin ファイルに移動して開始します
./start.sh
正常に起動したら、IP アドレス 8088にアクセスし、デフォルトのアカウント パスワードはroot/rootです。
入力後、グローバル構成-登録センター構成に登録センターを追加する必要があります。
追加が完了すると、接続が確立され、エラスティック ジョブが初期設定されます。データ ソースをインポートする場合は、 conf ファイルの下のapplication.properties構成ファイルを変更する必要があります。
データベースとして mysql に変更したいので、手動でアップロードできる接続パッケージを lib ファイルに追加する必要があります。
vim application.properties
mysqlドライバと接続方法の変更
ファイルを保存してelastic-jobを再起動し、以下に示すようにイベント追跡データ ソース構成にデータ ソースを追加します。
クリックして接続を確立すると、スケジュールされたタスクの構成とログがテーブルに記録されます。
統合された
簡単な統合
pom 依存関係を導入する
<dependency>
<groupId>com.cxytiandi</groupId>
<artifactId>elastic-job-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.10.0</version>
</dependency>
構成ファイル application.properties に構成を追加します。
elasticJob.zk.serverLists=localhost:2181
elasticJob.zk.namespace=user-sync
次に、確認するジョブ クラスを直接定義できます。コードは次のとおりです。
@Component
@ElasticJobConf(name = "TestJob",cron="0 0 0 * * ?",shardingTotalCount=5) // 每天零点执行
public class TestJob extends SimpleJob{
@Override
public void execute(ShardingContext shardingContext) {
// 要执行的逻辑
}
}
このメソッドのタイミング戦略はElasticJobConfアノテーションに依存します。アノテーションの構成を調整するだけです。
一般的な統合
一般的な統合には 3 つのクラスがあります
- ElasticJobConfig : エラスティック ジョブ コンポーネントの構成 (Zookeeper 構成センターなど)
- ElasticJobHandler : ジョブタスクの特定の実行クラスをここで設定できます。
- ElasticJobListener : ジョブタスクの監視、開始と終了
- ElasticJobProperties : 構成ファイルから Zookeeper 構成を読み取ります
依存関係を導入する
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-common-core</artifactId>
<version>${elasticjob.version}</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>curator-framework</artifactId>
<groupId>org.apache.curator</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-core</artifactId>
<version>${elasticjob.version}</version>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring</artifactId>
<version>${elasticjob.version}</version>
</dependency>
<!--解决冲突 elasticjob-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.10.0</version>
</dependency>
ElasticJobConfig
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "elasticjob.enabled", havingValue = "true")
public class ElasticJobConfig {
private final ElasticJobProperties jobProperties;
public ElasticJobConfig(ElasticJobProperties jobProperties) {
this.jobProperties = jobProperties;
}
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter() {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(jobProperties.getServerLists(),
jobProperties.getNamespace()));
}
@Bean
public ElasticJobListener elasticJobListener() {
return new ElasticJobListener(100, 100);
}
}
エラスティックジョブハンドラー
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
@AutoConfigureAfter(com.kyf.pe.trade.dataprocess.assist.config.ElasticJobConfig.class)
@ConditionalOnBean(com.kyf.pe.trade.dataprocess.assist.config.ElasticJobConfig.class)
public class ElasticJobHandlerConfig {
private final ZookeeperRegistryCenter zookeeperRegistryCenter;
public ElasticJobHandlerConfig(ZookeeperRegistryCenter zookeeperRegistryCenter) {
this.zookeeperRegistryCenter = zookeeperRegistryCenter;
}
/**
* 配置任务详细信息
*
* @param jobClass 定时任务实现类
* @param cron 表达式
* @param shardingTotalCount 分片数
* @param shardingItemParameters 分片参数
* @return
*/
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters,
final String jobParameters,
final String description) {
// 定义作业核心配置
JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(jobClass.getSimpleName(), cron, shardingTotalCount).
shardingItemParameters(shardingItemParameters).jobParameter(jobParameters).description(description).build();
// 定义SIMPLE类型配置
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(simpleCoreConfig, jobClass.getCanonicalName());
// 定义Lite作业根配置
return LiteJobConfiguration.newBuilder(simpleJobConfig).build();
}
/**
* 具体任务
*/
@Bean(initMethod = "init")
public JobScheduler pushHrhbJobScheduler(final TestJob testjob,
@Value("${job.test.cron}") final String cron,
@Value("${job.test.shardingTotalCount}") final int shardingTotalCount,
@Value("${job.test.description}") final String description) {
return new SpringJobScheduler(testjob, zookeeperRegistryCenter, getLiteJobConfiguration(testjob.getClass(),
cron, shardingTotalCount, "", "", description));
}
}
エラスティックジョブリスナー
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.AbstractDistributeOnceElasticJobListener;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ElasticJobListener extends AbstractDistributeOnceElasticJobListener {
/**
* 设置间隔时间
*
* @param startedTimeoutMilliseconds
* @param completedTimeoutMilliseconds
*/
public ElasticJobListener(long startedTimeoutMilliseconds, long completedTimeoutMilliseconds) {
super(startedTimeoutMilliseconds, completedTimeoutMilliseconds);
}
@Override
public void doBeforeJobExecutedAtLastStarted(ShardingContexts shardingContexts) {
log.info("任务名:{}开始", shardingContexts.getJobParameter());
}
@Override
public void doAfterJobExecutedAtLastCompleted(ShardingContexts shardingContexts) {
log.info("任务名:{}结束", shardingContexts.getJobParameter());
}
}
弾性ジョブのプロパティ
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "elasticjob")
public class ElasticJobProperties {
private boolean enabled = true;
private String serverLists;
private String namespace;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getServerLists() {
return serverLists;
}
public void setServerLists(String serverLists) {
this.serverLists = serverLists;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}