著作権:〜に再現してくださいマーク、ありがとう - 侵害が私にはプライベートの手紙を喜ばせるならば、私はすぐに削除させていただきます〜https://blog.csdn.net/baidu_26954625/article/details/90636073
本系列内容转载自git项目advancejava
TimeoutMilliseconds
コマンドは、長いセットの持続時間よりを実行する場合Hystrixでは、我々は、手動でタイムアウト時間を設定することができ、フォールバックのダウングレードロジックを実行しながら、タイムアウトとして識別し、Hystrixコマンドをタイムアウトしていると考えられます。
TimeoutMillisecondsのデフォルトは1000ミリ秒である、1000年です。
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int)
TimeoutEnabled
このパラメータはタイムアウトメカニズムを開くかどうかを制御するために使用され、デフォルト値はtrueです。
HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean)
例デモ
我々は、コマンド、タイムアウト時間が500ミリ秒に設定され、睡眠時間1秒を設定run()メソッドは、そのような要求、直接睡眠1S上に、結果があるために行わタイムアウト論理を低下します。
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;
}
}
テストクラスでは、我々は直接要求を行います。
@SpringBootTest
@RunWith(SpringRunner.class)
public class TimeoutTest {
@Test
public void testTimeout() {
HttpClientUtils.sendGetRequest("http://localhost:8080/getProductInfo?productId=1");
}
}
結果は降格商品に関連する情報をプリントアウトし、見ることができます。
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"}