undefinedfix
Sign in

How to do load balancing when multiple microservices do scheduled tasks

Alishafranklin edited in Sun, 18 Jul 2021

When deploying multiple microservices, the business logic is the same, that is, to do timing tasks. How to make each micro service do timing tasks evenly in spring cloud without conflict

6 Replies
piyushkantm
commented on Sun, 18 Jul 2021

One way is to use the task queue to publish the task to the queue, and then let the idle micro service automatically collect it. So there's no conflict.

meibd
commented on Sun, 18 Jul 2021

https://github.com/lukas-krec...

Lapis
commented on Sun, 18 Jul 2021

Is it necessary for each node of load balancing to run scheduled tasks, or to ensure that only one node performs tasks? If it is the former, a single thread in the VM will be OK

Olli
commented on Sun, 18 Jul 2021
  1. Send delay message
  2. Set mutex, for example, set an NX key to redis
  3. Zookeeper's election mechanism is used for timing tasks. In spring cloud, Eureka should have a similar mechanism
Rad0o95
commented on Mon, 19 Jul 2021

If it is not complicated, it can be implemented simply by using database. For example, MySQL has a built-in program lock function: get_ Lock() / / get lock release_ Lock() / / release the lock

For specific functions, you can check the functions of MySQL. There is a note to use this method to control the switching of distributed tasks. The acquisition and release of the lock is related to the database connection. The lock created by a connection can only be released by the connection itself, which needs to be changed in the database connection pool. However, the simplest way is to obtain the database connection directly from the native JDBC without causing the database connection to fail Use the connection pool to get the connection, get the lock, and execute the task of the microservice. After the task is completed, use the connection to release the lock. It's OK to switch a small number of distributed tasks, but a large number needs to consider a complex switching framework

openHBP
commented on Mon, 19 Jul 2021

We can try to make the scheduled task into a micro service, and then use the external scheduling framework such as elastic job to call the micro service regularly

lock This question has been locked and the reply function has been disabled.