Microservice protection--thread isolation (bulkhead mode)

1. How to implement thread isolation

Thread isolation can be implemented in two ways:

  • Thread pool isolation

  • Semaphore isolation (used by Sentinel by default)

As shown in the picture:

Thread pool isolation: Allocate a thread pool to each service call business, and use the thread pool itself to achieve isolation effect

Semaphore isolation: Does not create a thread pool, but uses counter mode to record the number of threads used by the business. When the upper limit of the semaphore is reached, new requests are prohibited.

Advantages and disadvantages of both:

2. Thread isolation of sentinel

Usage explanation

When adding a current limiting rule, you can choose from two threshold types:

  • QPS: It is the number of requests per second, which has been demonstrated in the quick start

  • Number of threads: It is the maximum number of tomcat threads that can be used by this resource. That is, by limiting the number of threads, thread isolation is achieved (bulkhead mode).

Case requirement: Set flow control rules for the query user interface of UserClient in the order-service service. The number of threads cannot exceed 2. Then use jemeter to test.

1) Configure isolation rules

Select the flow control button behind the feign interface:

Fill out the form:

2) Jmeter test

Select "Threshold Type - Number of Threads <2":

If 10 requests occur at one time, there is a high probability that the number of concurrent threads will exceed 2, and the exceeded requests will follow the previously defined failure and degradation logic.

View the running results:

If you like it, please give it a follow!

Guess you like

Origin blog.csdn.net/qq_45672041/article/details/135021666