Hystrix semaphore mechanism to achieve resource isolation

Isolation of resources based on Hystrix semaphore mechanism

Inside the core Hystrix a feature, in fact, the so-called resource isolation , to solve the most central problem is that the more dependent on service calls were isolated to pool their resources. Said one of its dependencies avoid service calls, since the delay or failure depend on the service interface calls, leading to service all the threads of all the resources spent on this interface to invoke the service. Once the said thread resources for a service all exhausted, then it could lead to the breakdown of services, even said that this failure will continue to spread.

Hystrix isolation of resources, there are two main technologies:

  • Thread Pool
  • signal

By default, Hystrix use thread pool mode.

As already mentioned a thread pool, this section is said on the semaphore mechanism to achieve resource isolation, and the difference between these two scenarios with specific technologies.

Semaphore mechanism

Resource isolation semaphore only acts as a switch, for example, service A semaphore size of 10, then it also means that there are only 10 A tomcat threads to access the service, other requests are denied, so as to achieve the role of isolation and protection of the resource limiting.

hystrix-semphore

Thread pool and the amount of the difference signal

Thread pool isolation technology, not to say this to control similar tomcat web container threads. On a more strict sense, Hystrix thread pool isolation technology, control of the implementation of tomcat threads. After Hystrix thread pool is full, it will ensure that say, tomcat threads will not depend on the service interface calls delay or failure is hang live, tomcat other threads will not get stuck, you can quickly go back and support other things.

Thread pool isolation technology, with Hystrix own thread to make the call; and semaphore isolation technology, is directly dependent on tomcat thread to invoke the service. Semaphore isolation, just a checkpoint, the number of semaphores, on the number of tomcat threads allows it, then to do it.

hystrix-semphore-thread-pool

Applicable scene :

  • Thread pool , for the vast majority of scenarios, for example, we call and request access to network-dependent services, it is necessary to control the timeout call (capture timeout timeout exception).
  • Semaphore technology for saying your access is not dependent on external access, but some of the more complex business logic of access to the internal, and internal system code, in fact, does not involve any network requests, as long as doing semaphore ordinary current limit on it, because no timeout to capture a similar problem.

Semaphore simple Demo

Business background, the semaphore is more suitable for what scene it?

For example, we generally speaking, caching service, it might turn some of the amounts are extremely rare, and especially frequent data access, on their own pure memory.

For chestnuts. After we get to the general product data, we have to go to get goods belongs to which location, provincial, municipal, sellers, etc., may be pure in their memory, for example on a Map to acquire. For this direct access to local logic memory, more suitable for use semaphore to do some simple isolation.

Advantage is that it do not have to manage the thread pool without care timeout timeout it does not require context switching threads friends. Semaphore isolation do so, the performance will be relatively higher.

If this is the local cache, we can cityId, get cityName.

public class LocationCache {
    private static Map<Long, String> cityMap = new HashMap<>();

    static {
        cityMap.put(1L, "北京");
    }

    /**
     * Get cityName by cityId
     *
     * @Param cityId city id
     * @Return City
     */
    public static String getCityName(Long cityId) {
        return cityMap.get(cityId);
    }
}

Write a GetCityNameCommand, policy is set to semaphore . run () method of obtaining the local cache. Our aim is to get the local cache code resource isolation.

public class GetCityNameCommand extends HystrixCommand<String> {

    private Long cityId;

    public GetCityNameCommand(Long cityId) {
        // set the semaphore isolation policy
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GetCityNameGroup"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)));

        this.cityId = cityId;
    }

    @Override
    protected String run() {
        // need to isolate code semaphore
        return LocationCache.getCityName(cityId);
    }
}

In the interface layer, by creating GetCityNameCommand, incoming cityId, execute execute () method, then get local cityName code will be cached resource isolation semaphore.

@RequestMapping("/getProductInfo")
@ResponseBody
public String getProductInfo(Long productId) {
    HystrixCommand<ProductInfo> getProductInfoCommand = new GetProductInfoCommand(productId);

    // By command execution, get the latest trade data
    ProductInfo productInfo = getProductInfoCommand.execute();

    Long cityId = productInfo.getCityId();

    GetCityNameCommand getCityNameCommand = new GetCityNameCommand(cityId);
    // Get local memory (cityName) code will be isolated semaphore resource
    String cityName = getCityNameCommand.execute();

    productInfo.setCityName(cityName);

    System.out.println(productInfo);
    return "success";
}

Guess you like

Origin blog.csdn.net/u014513171/article/details/93462169