Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to quickly get started with SpringBoot-ElasticJob Encapsulation

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to quickly get started with SpringBoot-ElasticJob packaging, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Introduction to elastic-job-spring-boot1

Elastic-Job is a distributed scheduling solution, which consists of two independent subprojects, Elastic-Job-Lite and Elastic-Job-Cloud. Elastic-Job-Lite is positioned as a lightweight decentralized solution that provides coordination services for distributed tasks in the form of jar packages. Based on the quartz timed task framework, so most of the functions of quartz use zookeeper for coordination, scheduling center, and more lightweight task slicing to support flexible expansion, which can be expanded horizontally. When the task runs again, it will check the current number of servers and re-shard. After the shard is finished, it will continue to perform task failure transfer and fault-tolerant processing. When a scheduling server is down or disconnected from zookeeper, it will stop the job immediately, and then find other idle scheduling servers to run the remaining tasks to provide an operation and maintenance interface to manage jobs and registries.

1.1 usage scenarios

Because the project is a micro-service, a single module may have more than two instances, and the timer will execute multiple instances at the same time. The general timer lacks an administrative interface and cannot monitor whether the timer is executed successfully. The common solution on the market is the operation of timer locking, or the use of third-party distributed timers. There are many schemes of distributed timer, such as ScheduledX inside Ali, Elastic job of Dangdang, xxl-job of personal open source and so on.

1.2 feature list

Distributed scheduling coordination

Elastic expansion and reduction

Failure transfer

Missed execution of job retrigger

Job fragmentation consistency to ensure that there is only one execution instance of the same shard in a distributed environment

Self-diagnose and fix problems caused by distributed instability

Support parallel scheduling

Support for job lifecycle operations

Rich job types

Spring integration and namespace provisioning

Operation and maintenance platform

1.3 concept

Fragmentation: the distributed execution of a task, which needs to split a task into multiple independent task items, and then one or more shard items are executed by the distributed server. For example, there is a job that traverses a table in the database and there are 2 servers. In order to execute the job quickly, each server should execute 50% of the job. To meet this requirement, the job can be divided into 2 slices, and each server executes 1 slice. The logic for job traversal data should be: server A traverses data that ends with an odd number for ID; server B traverses data that ends with an even number for ID. If the job is divided into 10 slices, the logic of the job traversal data should be as follows: the shard item assigned to each slice should be ID, and server An is assigned to the shard item 0jue 1, 2, and 4; server B is assigned to the shard item 5, 6, 7, and 8, 9, the direct result is that server A traverses the data that ends in ID with 0-4, and server B traverses the data ending in ID with 5-9.

Historical track: Elastic-Job provides event tracking function, which can handle important events in the scheduling process through event subscription for query, statistics and monitoring.

1.4Encapsulation elasticjob

As the Dangdang Elastic job is in the unupdated stage for one year, the relevant jar is in the usable stage and the function is not complete. Considering that the usage scenario is for multi-project use, it is easy to package elastic-job-lite-spring for use.

two。 Instructions: 2.1 add dependencies

Ps: please use the latest version of the actual version version

Com.purgeteam elasticjob-spring-boot-starter 0.1.1.RELEASE2.2 configuration

Ps: mysql,zookeeper support is required. Please build it in advance.

Configure bootstrap.yml or application.yml.

Add the following configuration:

Spring: elasticjob: datasource: # recording data source required by job url: jdbc:mysql://127.0.0.1:3306/batch_log?useUnicode=true&characterEncoding=utf-8&verifyServerCertificate=false&useSSL=false&requireSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: Rtqw123OpnmER regCenter: # recording Center serverList: 127.0.0.1 namespace: elasticJobDemo2.3 timer implementation method

Create a timer class (the only difference is to change @ Scheduled to implement the SimpleJob interface) the timer implementation method is written in the execute method.

@ Slf4j@Componentpublic class MySimpleJob implements SimpleJob {/ / @ Scheduled (cron = "00 ShardingContext shardingContext 1 *?") @ Override public void execute (ShardingContext shardingContext) {log.info ("Thread ID:% s, total job shards:% s," + "current shard item:% s. Current parameter:% s, "+" job name:% s. Job customization parameters:% s ", Thread.currentThread () .getId (), shardingContext.getShardingTotalCount (), shardingContext.getShardingItem (), shardingContext.getShardingParameter (), shardingContext.getJobName (), shardingContext.getJobParameter () / / the sharding is roughly as follows: execute the corresponding logical switch (context.getShardingItem ()) {case 0: / / do something by sharding item 0 break; case 1: / / do something by sharding item 1 break according to the configured sharding parameters. Case 2: / / do something by sharding item 2 break; / / case n:...}} log:Thread ID: 66, total number of job shards: 1, current shard item: 0. Current parameter: Beijing, job name: PropertiesSimpleJob. Job customization parameters: test2.4 configuration timer 2.4.1 create Configuration class

ZookeeperRegistryCenter and JobEventConfiguration are injected. Create JobScheduler @ Bean (initMethod = "init"). In the mySimpleJobScheduler method, you first get the LiteJobConfiguration object through ElasticJobUtils#getLiteJobConfiguration. Just create a SpringJobScheduler object and return it.

@ Configurationpublic class MyJobConfig {/ / job name private static final String JOB_NAME = "MySimpleJob"; / / timer cron parameter private static final String CRON = "0 _ 0 _ 1 *?"; / / timer sharding private static final int SHARDING_TOTAL_COUNT = 1; / / slicing parameter private static final String SHARDING_ITEM_PARAMETERS = "0 million Beijinglin 1: ShanghaiJing 2 fragments" / / Custom parameter private static final String JOB_PARAMETERS = "parameter"; @ Resource private ZookeeperRegistryCenter regCenter; @ Resource private JobEventConfiguration jobEventConfiguration; @ Bean (initMethod = "init") public JobScheduler mySimpleJobScheduler (final MySimpleJob mySimpleJob) {LiteJobConfiguration liteJobConfiguration = ElasticJobUtils .getLiteJobConfiguration (mySimpleJob.getClass (), JOB_NAME, CRON, SHARDING_TOTAL_COUNT, SHARDING_ITEM_PARAMETERS, JOB_PARAMETERS) / / Parameter: 1. Timer example, 2. Registry class, 3.LiteJobConfiguration, / / 3. History track (not needed) return new SpringJobScheduler (mySimpleJob, regCenter, liteJobConfiguration, jobEventConfiguration);}

Brief introduction of ElasticJobUtils#getLiteJobConfiguration parameters:

/ * * get {@ link LiteJobConfiguration} object * * @ param jobClass timer implementation class * @ param jobName timer name * @ param cron timing parameter * @ total number of param shardingTotalCount job shards * @ param shardingItemParameters the current parameter can be null * @ param jobParameters job custom parameter can be null * @ return {@ link LiteJobConfiguration} * / public static LiteJobConfiguration getLiteJobConfiguration (final Class

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report