_ A little class (multi-shore College) Hystrix thread pool technology resources based on 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, I 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;
        // 调用商品服务接口
        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")
@ResponseBody
public String getProductInfo(Long productId) {
    HystrixCommand<ProductInfo> getProductInfoCommand = new GetProductInfoCommand(productId);
    
    // 通过command执行,获取最新商品数据
    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) {
        // 还是绑定在同一个线程池
        super(HystrixCommandGroupKey.Factory.asKey("GetProductInfoGroup"));
        this.productIds = productIds;
    }

    @Override
    protected Observable<ProductInfo> construct() {
        return Observable.unsafeCreate((Observable.OnSubscribe<ProductInfo>) subscriber -> {

            for (String productId : productIds) {
                // 批量获取商品数据
                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 the id, such is ,separated id string, through the above HystrixObservableCommand, some of the API method performs 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("获取完了所有的商品数据");
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        /**
         * 获取完一条数据,就回调一次这个方法
         * @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.
Here Insert Picture Description

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.

[Thank you for reading, if we can help you, trouble spots a praise ~]

More experience and technology come together to welcome the exchange of learning:
a little classroom - for the dream fight online learning platform http://www.yidiankt.com/

[Number of public attention, to receive free - java core knowledge [point]]
No public concern, to receive free - java core knowledge [point]
QQ discussion group: 616 683 098
QQ: 3,184,402,434
want in-depth study of students learning together can add my QQ discussion - there is a full set of resources to share, discuss experiences, so you oh !
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yidiankt/p/11458242.html