2016-09-27 102 views
3

我正在使用無功觀測功能研究啓用Hystrix的Spring Boot-app,並試圖找出超時如何工作。我假設如果在執行期間發生超時,Hystrix會立即返回響應(回退或異常)。現在,這似乎不是我的代碼如下所示。而是撥打myService.getString()阻止5秒。當它最終返回時,可執行的lambda被執行。超時如何在Hystrix中使用Observable?

我的假設是錯誤的還是下面的其他錯誤?

@SpringBootApplication 
@EnableCircuitBreaker 
public class App { 
    public static void main(String[] args) { 
    ApplicationContext ctx = SpringApplication.run(App.class, args); 
    } 
} 

@RestController 
public class MyController { 

    @Autowired 
    private MyService myService; 

    @RequestMapping("/") 
    public DeferredResult index() { 

    DeferredResult<String> result = new DeferredResult<>(); 
    Observable<String> res = myService.getString(); 

    res.subscribe(s -> { 
       result.setResult("Got a " + s); 
      }, 
      throwable -> { 
       result.setErrorResult("Got a " + throwable.getMessage()); 
      }); 

    return result; 
    } 
} 


@Service 
public class MyService { 

    @HystrixCommand() // Default timeout is 1 sec 
    public Observable<String> getString() { 
    return Observable.create(subscriber -> { 
     if (!subscriber.isUnsubscribed()) { 

      try { 
       Thread.sleep(5000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      subscriber.onNext("regular response"); 
      subscriber.onCompleted(); 
     } 
    }); 
    } 
} 

的pom.xml: http://maven.apache.org/xsd/maven-4.0.0.xsd「> 4.0.0

<groupId>dag</groupId> 
    <artifactId>dag</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.1.RELEASE</version> 
    </parent> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.springframework.cloud</groupId> 
       <artifactId>spring-cloud-dependencies</artifactId> 
       <version>Camden.RELEASE</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-hystrix</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.netflix.hystrix</groupId> 
      <artifactId>hystrix-javanica</artifactId> 
      <version>1.5.5</version> 
     </dependency> 
    </dependencies> 

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 


    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

感謝您的幫助!

回答

1

請澄清什麼是你的紅椎命令的隔離和你用什麼椎版本?

「超時現在適用於信號隔離的命令以及 線程隔離的命令。在1.4.x之前,信號隔離命令 不能超時。他們現在在另一個 (HystrixTimer)線程上註冊了一個超時,它會觸發超時流程。如果您使用 信號隔離命令,他們將看到超時」

反正嘗試:

  1. 切換到線程有自己的線程池隔離在週期
  2. 睡眠與小週期。
+0

感謝您的幫助。默認是THREAD隔離級別,所以我使用這個。 – DagR

+0

好的...默認似乎是SEMAPHORE,而不是THREAD,如文檔所述:https://github.com/Netflix/Hystrix/wiki/Configuration#execution – DagR

0

設置execution.isolation.strategyTHREAD解決了我的問題。

@HystrixCommand(
    commandProperties = { 
    @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD") 
    } 
) 

該文檔指出HystrixCommand默認使用THREAD,但對於javanica @HystrixCommand有點不同,它根據註釋方法的返回類型創建不同的類型。 HystrixCommandHystrixObservableCommand。並且HystrixObservableCommand具有SEMAPHORE作爲默認隔離策略。

有關更多詳細信息,請參閱https://github.com/Netflix/Hystrix/issues/1383

相關問題