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
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.
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
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
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