2016-09-27 98 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

相关问题