Hystrix selects a thread pool policy for resource isolation

This is the fourth day of my participation in Gwen Challenge

Hystrix selects thread pool policy to implement resource isolation (1) Hystrix selects thread pool policy to implement resource isolation Settings, thread pool key parameters, next use HystrixCommand to obtain a single user data

Train of thought

The operation of calling the member service is encapsulated in HystrixCommand and a key is specified, such as UserInfoCommandGroup. Each time the member service is called, only the resources in the thread pool are used, and no other thread resources are used

Get a single piece of data using HystrixCommand

public class UserInfoCommand extends HystrixCommand<UserOutpDTO> {

    private Long userId;

    public UserInfoCommand(Long userId){
        super(HystrixCommandGroupKey.Factory.asKey("UserInfoCommandGroup"));
        this.userId =userId;
    }

    @Override
    protected UserOutpDTO run(a) throws Exception {
        String url = "localhost:8080/user/info? userId="+userId;
        String result = HttpClientUtil.sendGetRequest(url);
        returnJSONObject.parseObject(result,UserOutpDTO.class); }}Copy the code

In the cache service interface, create a command based on the userId and execute it to get the user data

@Controller
@Slf4j
public class UserController {

    @GetMapping("getUserInfo")
    public String getUserInfo(Long userId){
        UserInfoCommand userInfoCommand = new UserInfoCommand(userId);
        UserOutpDTO userOutpDTO = userInfoCommand.execute();
        return "success"; }}Copy the code

Note: HystrixCommand executes the execute() method synchronously

Can HystrixCommand obtain data in batches?

No, but you can use HystrixObservableCommand to batch fetch data, to fetch user data, all bound to the same thread pool, and executed by HystrixObservableCommand’s thread pool core thread. In the thread pool, the core thread processes the userInfo fetch result set of multiple Userids in bulk

public class UserInfosCommand extends HystrixObservableCommand<UserOutpDTO> {

    private String[] userIds;

    public UserInfosCommand(String[] userIds){
        super(HystrixCommandGroupKey.Factory.asKey("UserInfosCommand"));
        this.userIds=userIds;
    }

    @Override
    protected Observable<UserOutpDTO> construct(a) {
        return Observable.unsafeCreate((Observable.OnSubscribe<UserOutpDTO>) subscriber->{
            for (String userId:userIds) {
                String url="localhost:8080/user/info? userId="+userId; String result = HttpClientUtil.sendGetRequest(url); UserOutpDTO userOutpDTO = JSONObject.parseObject(result, UserOutpDTO.class); subscriber.onNext(userOutpDTO); } subscriber.onCompleted(); }).subscribeOn(Schedulers.io()); }}Copy the code

According to the id from the list, such as with “, “separation of userId string, through HystrixObservableCommand, perform Hystrix some API method, obtain all user data

@GetMapping("getUserInfos")
    public String getUserInfos(String userIds){
        String[] userIdArr = userIds.split(",");
        UserInfosCommand userInfosCommand = new UserInfosCommand(userIdArr);
        Observable<UserOutpDTO> userInfos = userInfosCommand.observe();
        userInfos.subscribe(new Observer<UserOutpDTO>() {
            @Override
            public void onCompleted(a) {
                log.info("Complete obtaining all user information!");
            }

            @Override
            public void onError(Throwable throwable) {
                log.error(throwable.getMessage());
            }

            @Override
            public void onNext(UserOutpDTO userOutpDTO) {
                log.info("userOutpDTO:{}",userOutpDTO); }});return "success";
    }
Copy the code

conclusion

Hystrix chooses a thread pool policy to implement resource isolation. The default thread pool size is 10 threads, with a maximum of 10 threads calling the member service interface. Even if the member service interface fails, only 10 threads will block calling the member service interface, while other threads in Tomcat can call other services and perform other tasks to achieve the purpose of resource isolation.