zoukankan      html  css  js  c++  java
  • Hystrix超时测试

     1 package com.cookie.test;
     2 
     3 import com.netflix.hystrix.HystrixCommand;
     4 
     5 import com.netflix.hystrix.HystrixCommandGroupKey;
     6 
     7 import java.util.Random;
     8 
     9 /**
    10 
    11 * author : cxq
    12 
    13 * Date : 2019/6/28
    14 
    15 *
    16 
    17 * Hystrix超时测试
    18 
    19 */
    20 
    21 public class HystrixCommandTest extends HystrixCommand<String> {
    22 
    23   public String name ;
    24 
    25   public HystrixCommandTest( String name) {
    26 
    27   // 设置超时时间100ms
    28 
    29     super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),100);
    30 
    31     this.name = name;
    32 
    33   }
    34 
    35   @Override
    36 
    37   protected String run() throws InterruptedException {
    38 
    39     int excution = new Random().nextInt(200);
    40 
    41     System.out.println(" 执行时间 :"+excution + "ms");
    42 
    43     Thread.sleep(excution);
    44 
    45     return "Hello "+name ;
    46 
    47   }
    48 
    49   @Override
    50 
    51   protected String getFallback() {
    52 
    53   return "error ! 降级处理 ";
    54 
    55   }
    56 
    57   public static void main(String[] args) {
    58 
    59     HystrixCommandTest test = new HystrixCommandTest("Ketty");
    60 
    61     System.out.println(test.execute());
    62 
    63   }
    64 
    65 }

    输出结果展示:

    1、 执行时间 :177ms

    error ! 降级处理

    2、执行时间 :5ms

    Hello Ketty

  • 相关阅读:
    MVC 4 中 WEB API 选择 返回格式
    用XML配置菜单的一种思路,附一些不太准确的测试代码
    2020.11.15(每周总结)
    2020.11.19
    2020.11.22
    2020.11.21
    2020.11.14
    202.11.13
    2020.11.20
    2020.11.17
  • 原文地址:https://www.cnblogs.com/pretttyboy/p/11325705.html
Copyright © 2011-2022 走看看