Hystrix thread pool resources to achieve isolation

Isolation of resources based on Hystrix thread pool

Mentioned in the last lecture, if you start from the Nginx, caches are ineffective, Nginx will get the latest product data directly through caching service calls Goods (to be discussed in our project-based electricity supplier), it is possible to call a delay occurs while the caching service resource depletion situation. Here, we are concerned that, how to achieve resource isolation by Hystrix thread pool.

Resource isolation, that is to say, if you want to call to request one for all dependent services, all in the same isolate a resource pool, do not go with other resources, and this is called resource isolation. Even if this dependent services, such as goods and services, while now has been initiated by the amount of calls to 1000, but the thread pool to 10 threads, up to 10 will only use this thread to perform, will not say, requests for goods and services because the interface calls delay, all the threads inside the tomcat exhausted all resources.

Hystrix resource isolation, in fact, provides an abstraction called command. This is the most basic resources Hystrix isolation technology.

Using a single data acquisition HystrixCommand

We will call the packaged goods and services operating in HystrixCommand in defining a key, such as the following  GetProductInfoCommandGroup , where we can simply think that this is a thread pool, each invocation of goods and services, will only use the thread pool resources , will not go with the other thread resources.

public class GetProductInfoCommand extends HystrixCommand<ProductInfo> {

    private Long productId;

    public GetProductInfoCommand(Long productId) {

        super(HystrixCommandGroupKey.Factory.asKey("GetProductInfoCommandGroup"));

        this.productId = productId;

    }

    @Override

    protected ProductInfo run() {

        String url = "http://localhost:8081/getProductInfo?productId=" + productId;

        // Call Goods Services Interface

        String response = HttpClientUtils.sendGetRequest(url);

        return JSONObject.parseObject(response, ProductInfo.class);

    }

}

We caching service interface, according to productId command to create and perform, to get product data.

@RequestMapping("/getProductInfo")@ResponseBodypublic String getProductInfo(Long productId) {

    HystrixCommand<ProductInfo> getProductInfoCommand = new GetProductInfoCommand(productId);

    // By command execution, get the latest trade data

    ProductInfo productInfo = getProductInfoCommand.execute();

    System.out.println(productInfo);

    return "success";

}

The implementation of the above execute () method, in fact, are synchronized. You can also call queue () method of the command, it is only the command into a queue thread pool, to return immediately to get a Future object behind can continue to do some other things, and then some time to get calls Future () method to get the data. This is asynchronous.

Batch data acquired using the HystrixObservableCommand

As long as the product data acquisition, all bound to the same thread pool to go inside, we went through a thread HystrixObservableCommand execution, and in this thread there, the bulk of the more productId productInfo pull back.

public class GetProductInfosCommand extends HystrixObservableCommand<ProductInfo> {

    private String[] productIds;

    public GetProductInfosCommand(String[] productIds) {

        // or binding in the same thread pool

        super(HystrixCommandGroupKey.Factory.asKey("GetProductInfoGroup"));

        this.productIds = productIds;

    }

    @Override

    protected Observable<ProductInfo> construct() {

        return Observable.unsafeCreate((Observable.OnSubscribe<ProductInfo>) subscriber -> {

            for (String productId : productIds) {

                // get the bulk product data

                String url = "http://localhost:8081/getProductInfo?productId=" + productId;

                String response = HttpClientUtils.sendGetRequest(url);

                ProductInfo productInfo = JSONObject.parseObject(response, ProductInfo.class);

                subscriber.onNext(productInfo);

            }

            subscriber.onCompleted();

        }).subscribeOn(Schedulers.io());

    }

}

In the cache service interface, based on the list came id, such as is  ,  separated id string, through the above HystrixObservableCommand, perform some API methods Hystrix, access to all product data.

public String getProductInfos(String productIds) {

    String[] productIdArray = productIds.split(",");

    HystrixObservableCommand<ProductInfo> getProductInfosCommand = new GetProductInfosCommand(productIdArray);

    Observable<ProductInfo> observable = getProductInfosCommand.observe();

    observable.subscribe(new Observer<ProductInfo>() {

        @Override

        public void onCompleted() {

            System . OUT . Println ( "get over all the product data" );

        }

        @Override

        public void onError(Throwable e) {

            e . printStackTrace ();

        }

        / ** * get a complete data, once the callback method * @param  productInfo * /

        @Override

        public void onNext(ProductInfo productInfo) {

            System.out.println(productInfo);

        }

    });

    return "success";

}

We look back and see how Hystrix thread pool resources to achieve isolation.

Nginx from the start, the cache are invalid, then Nginx to call Goods by caching services. Cache Service default thread size is 10, only a maximum of 10 threads to call interface of goods and services. Even Goods Services interface failure, and only a maximum of 10 threads will hang dead on the road calling Goods Services interface within tomcat caching service other threads can still be used to call other services, do other things.

 

Guess you like

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