To provide security for the service interface to call a timeout based on timeout mechanism

Copyright: ~ reproduced please marked, thank you - if infringement please private letter to me, I will immediately delete ~ https://blog.csdn.net/baidu_26954625/article/details/90636073

本系列内容转载自git项目advancejava

TimeoutMilliseconds

In Hystrix, we can set the timeout duration manually, if a command runs longer than the duration of the set, then it is considered to be timeout, then Hystrix command identified as a timeout, while performing fallback downgrade logic.
TimeoutMilliseconds default is 1000, which is 1000ms.

HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int)

TimeoutEnabled

This parameter is used to control whether or not to open the timeout mechanism, the default value is true.

HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean)

Examples Demo

We command, the timeout time is set to 500ms, and the run () method, set sleep time 1s, over such a request, direct sleep 1s, the result will be degraded because of timeout logic performed.

public class GetProductInfoCommand extends HystrixCommand<ProductInfo> {
private Long productId;
private static final HystrixCommandKey KEY = HystrixCommandKey.Factory.asKey("GetProductInfoCommand");
public GetProductInfoCommand(Long productId) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ProductInfoService"))
                .andCommandKey(KEY)
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                        .withCoreSize(8)
                        .withMaxQueueSize(10)
                        .withQueueSizeRejectionThreshold(8))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerEnabled(true)
                        .withCircuitBreakerRequestVolumeThreshold(20)
                        .withCircuitBreakerErrorThresholdPercentage(40)
                        .withCircuitBreakerSleepWindowInMilliseconds(3000)
                        // 设置是否打开超时,默认是true
                        .withExecutionTimeoutEnabled(true)
                        // 设置超时时间,默认1000(ms)
                        .withExecutionTimeoutInMilliseconds(500)
                        .withFallbackIsolationSemaphoreMaxConcurrentRequests(30)));
        this.productId = productId;
    }
@Override
    protected ProductInfo run() throws Exception {
        System.out.println("调用接口查询商品数据,productId=" + productId);
// 休眠1s
        TimeUtils.sleep(1);
String url = "http://localhost:8081/getProductInfo?productId=" + productId;
        String response = HttpClientUtils.sendGetRequest(url);
        System.out.println(response);
        return JSONObject.parseObject(response, ProductInfo.class);
    }
@Override
    protected ProductInfo getFallback() {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setName("降级商品");
        return productInfo;
    }
}

In the test class, we make requests directly.

@SpringBootTest
@RunWith(SpringRunner.class)
public class TimeoutTest {
@Test
    public void testTimeout() {
        HttpClientUtils.sendGetRequest("http://localhost:8080/getProductInfo?productId=1");
    }
}

The results can be seen, print out the relegation commodity-related information.

ProductInfo(id=null, name=降级商品, price=null, pictureList=null, specification=null, service=null, color=null, size=null, shopId=null, modifiedTime=null, cityId=null, cityName=null, brandId=null, brandName=null)
{"id": 1, "name": "iphone7手机", "price": 5599, "pictureList":"a.jpg,b.jpg", "specification": "iphone7的规格", "service": "ipho"}

Guess you like

Origin blog.csdn.net/baidu_26954625/article/details/90636073