31 Unstar Star 77 Fork 21

恒宇少年 / spring-boot-micro-job

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

Build StatusLicense Maven Central

Micro-job is a distributed task scheduling and execution framework, which accesses data internally through the Rest path shared by Jersey of each component.

Detailed development documentation Visit official website

Noun interpretation:

consumer -> Task Consumption Node

schedule -> Task Scheduler

provider -> Task Producer

registry -> Task Registry

Registry

Regisryserves as the task of registering nodes of each component in the whole ecosystem. The way to implement the task registry is diversified. At present, it includes memory', zookeeper', redis', consul', etc.

Create the SpringBoot'project through idea and eclipse tools and add the following dependencies to the pom.xml' file.

<dependency>
    <groupId>com.github.hengboy</groupId>
	<artifactId>spring-boot-starter-registry-memory</artifactId>
	<version>{lastVersion}</version>
</dependency>

Add the application.yml'configuration file to the resources' resource directory as follows:

server:
   port: 9000
hengboy:
  job:
    registry:
      # ask Registry Node Registration
      away: memory

Schedule

Each task is created through a dispatcher to allocate and execute. In the process of allocation, different tasks are consumed by different consumer nodes according to the load balancing strategy configuration of the consumer nodes. In production tasks, the scheduler nodes'that perform task scheduling are also filtered according to the load balancing strategy' of the scheduler. Create SpringBoot'project through idea' and eclipse'tools and add the following dependencies to the pom.xml' file.

<dependency>
    <groupId>com.github.hengboy</groupId>
    <artifactId>spring-boot-starter-schedule</artifactId>
    <version>{lastVersion}</version>
</dependency>

Add the application.yml'configuration file to the resources' resource directory as follows:

server:
   port: 8081
hengboy:
  job:
    registry:
      # Maintain consistency with task registry node registration
      away: memory
    schedule:
      # Memory Scheduler handles task queues and storage of task logs
      job-store-type: memory  

Consumer

Tasks are defined and reported by consumer'. When schedule'invokes a consumer to execute a task request, the corresponding task logic method is automatically executed according to jobKey'. Create SpringBoot'project through idea' and eclipse'tools and add the following dependencies to the `pom.xml' file.

<dependency>
	<groupId>com.github.hengboy</groupId>
	<artifactId>spring-boot-starter-consumer</artifactId>
	<version>{lastVersion}</version>
</dependency>

Add the application.yml'configuration file to the resources' resource directory as follows:

server:
   port: 8082
hengboy:
  job:
    registry:
      # Maintain consistency with task registry node registration
      away: memory

Example Of JOB Definition

Let's define a simple `Job', as follows:

@Job(jobExecuteAway = JobExecuteAwayEnum.ONCE)
public class TestJob implements MicroJob {
    /**
     * logger instance
     */
    static Logger logger = LoggerFactory.getLogger(TestJob.class);

    @Override
    public JobExecuteResult execute(JobExecuteParam jobExecuteParam) throws JobException {
        logger.info("Key:{},Param:{}", jobExecuteParam.getJobKey(), jobExecuteParam.getJsonParam());
        return JobExecuteResult.JOB_EXECUTE_SUCCESS;
    }
}

The Job', as defined above, corresponds to JobKey', which is `testJob'.

Provider

The business side adds dependencies and performs MicroJobProvider. newXxxJob'call creation tasks, such as Send mail' notification operation after creating an order'. Create SpringBoot'project through idea' and eclipse'tools and add the following dependencies to the pom.xml' file.

<dependency>
	<groupId>com.github.hengboy</groupId>
	<artifactId>spring-boot-starter-provider</artifactId>
	<version>{lastVersion}</version>
</dependency>

Add the application.yml'configuration file to the resources' resource directory as follows:

server:
  port: 8083
hengboy:
  job:
    registry:
      # Maintain consistency with task registry node registration
      away: memory

JOB Execution Example

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProviderTester {
    /**
     * Registered Task Provider
     */
    @Autowired
    private MicroJobProvider microJobProvider;

    @Test
    public void newJob() {
        // Created tasks are executed only once
        microJobProvider.newOnceJob(OnceJobWrapper.Context()
                // JobKey, which corresponds to tasks defined in consumer, defaults to lowercase class names
                .jobKey("testJob")
                // Customized task queue key, can accurately locate tasks and operate pause, delete and other operations
                .jobQueueKey(UUID.randomUUID().toString())
                // Parameters, parameters of any type, when consumer consumes, are converted to JSON strings
                .param(new HashMap() {
                    {
                        put("name", "admin");
                    }
                })
                .wrapper());
    }
}

Test flow

  1. Start Task Registry
  2. Start Task Scheduling Center
  3. Start Task Consumer Node
  4. Execute the Provider Tester # newJob unit test method

Folders

​```
.
├── micro-job-autoconfigure
├── micro-job-dependencies
├── micro-job-samples
│   ├── sample-consumer
│   ├── sample-provider
│   ├── sample-registry-consul
│   ├── sample-registry-memory
│   ├── sample-registry-redis
│   ├── sample-registry-zookeeper
│   ├── sample-schedule
│   ├── pom.xml
│   └── README.md
├── micro-job-starters
│   ├── spring-boot-starter
│   ├── spring-boot-starter-provider
│   ├── spring-boot-starter-registry-consul
│   ├── spring-boot-starter-registry-memory
│   ├── spring-boot-starter-registry-redis
│   ├── spring-boot-starter-registry-zookeeper
│   ├── spring-boot-starter-schedule
│   └── pom.xml
├── .travis.yml
├── LICENSE
├── pom.xml
└── README.md
​```

License

The Apache License

Comments ( 4 )

Sign in for post a comment

About

micro-job是一款分布式任务调度执行框架,用于集群环境负载均衡条件下的任务分发以及任务消费 任务消费者节点定义任务 & 任务自动上报 任务调度器自动根据负载均衡策略来选择不同的消费者节点进行任务消费 spread retract
Cancel

Releases (1)

All

Gitee Metrics

Contributors

All

Activities

load more
can not load any more
Java
1
https://gitee.com/hengboy/spring-boot-micro-job.git
git@gitee.com:hengboy/spring-boot-micro-job.git
hengboy
spring-boot-micro-job
spring-boot-micro-job
master

Search

132457 8cb2edc1 1899542 131848 70c8d3a4 1899542