Home  >  Article  >  Java  >  How to use Spring Cloud to develop a task scheduling system under a microservice architecture

How to use Spring Cloud to develop a task scheduling system under a microservice architecture

WBOY
WBOYOriginal
2023-06-22 17:54:541274browse

With the development of the Internet, more and more companies are beginning to adopt microservice architecture to build distributed systems to improve application reliability, scalability, maintainability and other capabilities. Under the microservice architecture, a task scheduling system is a very important component. It can be used to regularly execute some asynchronous tasks, call other microservice interfaces, etc.

Spring Cloud is an open source microservice framework that provides some very powerful tools and frameworks, such as Spring Cloud Netflix, Spring Cloud Config, Spring Cloud Stream, Spring Cloud Security, etc. Among them, Spring Cloud Netflix is ​​a very popular microservice framework. It provides some core components and extensions, such as service registration and discovery, client load balancing, configuration management, circuit breakers, API gateways, etc.

In this article, we will introduce how to use Spring Cloud to develop a task scheduling system, which mainly includes the following content:

  1. Requirements analysis of task scheduling system
  2. Usage Spring Scheduler implements task scheduling
  3. Put the task scheduling system into the Spring Cloud microservice architecture

1. Requirements analysis of the task scheduling system

In the microservice architecture Under the circumstances, a task scheduling system needs to support the following functions:

  1. can trigger the execution of a task at a specified time point or moment.
  2. Supports triggering the execution of a task at fixed time intervals or periodic time intervals.
  3. Support retry after task execution failure.
  4. Supports asynchronous execution of tasks and does not affect the running of the main program.
  5. Supports dynamic addition and deletion of tasks.

2. Use Spring Scheduler to implement task scheduling

Spring Scheduler is a module of the Spring framework. It provides a lightweight task scheduling framework that can easily implement task scheduling. function.

  1. Add Spring Scheduler dependency

Add Spring Scheduler dependency in the pom.xml file of the Spring Boot project:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
     <version>2.4.3</version>
 </dependency>
  1. Create task Execution class

Create a task execution class inherited from the Runnable interface to implement specific task logic.

@Component
public class JobTask implements Runnable {
 
     @Override
     public void run() {
         System.out.println("running job...");
     }
 }
  1. Add task scheduling configuration

Add a task scheduling configuration class to the Spring Boot project to configure the specific task scheduling strategy and executed tasks.

@Configuration
@EnableScheduling
public class ScheduleConfig {
 
     @Autowired
     private JobTask jobTask;
 
     //在每分钟的3秒和6秒执行一次
     @Scheduled(cron = "3-6 * * * * ?")
     public void scheduleJob1() {
         jobTask.run();
     }
 
     //在间隔5秒后执行第一次,之后每隔10秒执行一次
     @Scheduled(initialDelay = 5000, fixedRate = 10000)
     public void scheduleJob2() {
         jobTask.run();
     }
 }
  1. Test task scheduling function

After starting the Spring Boot application, you can view the console output and the task will be executed according to the specified time interval and periodicity. If you want to modify the task execution strategy, you only need to modify the configuration class.

3. Put the task scheduling system into the Spring Cloud microservice architecture

  1. Register the task scheduling system to the service registration center

In task scheduling In the system application, service registration and discovery components such as Spring Cloud's Eureka or Consul are used to register the task scheduling system to the service registration center.

spring:
     application:
          name: task-scheduler
 
 eureka:
     instance:
          hostname: localhost
     client:
          service-url:
               defaultZone: http://localhost:8761/eureka/
  1. Set routing rules in the gateway

Use API gateways such as Spring Cloud Gateway or Zuul to expose the task scheduling system to other microservices.

spring:
     application:
          name: api-gateway
          
 server:
     port: 8080
 
 eureka:
     instance:
          hostname: localhost
     client:
          service-url:
               defaultZone: http://localhost:8761/eureka/
 
 gateway:
     routes:
          - id: task-scheduler
               uri: lb://task-scheduler
               predicates:
                    - Path=/schedule/**
  1. Call the task scheduling system in microservices

In other microservices, use tools such as Feign or RestTemplate to call the RESTful API exposed by the task scheduling system. Execute asynchronous tasks or trigger the execution of tasks.

@Service
 public class OrderService {
 
     @Autowired
     private TaskSchedulerClient taskSchedulerClient;
     
     public void createOrder(Order order) {
         //... 创建订单逻辑
         taskSchedulerClient.scheduleJob();
     }
 }

4. Summary

This article introduces how to use Spring Cloud to develop a task scheduling system under a microservice architecture, mainly including using Spring Scheduler to implement task scheduling and putting the task scheduling system into Spring Cloud In microservice architecture. I hope it will be helpful to developers who are adopting microservice architecture, so that they can more easily build highly reliable and highly scalable distributed systems.

The above is the detailed content of How to use Spring Cloud to develop a task scheduling system under a microservice architecture. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn