> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v5/di-15-zhang-chu-li-shi-bai-he-shi-yan/15.2-ding-yi-duan-lu-qi/15.2.1-huan-jie-shi-yan.md).

# 15.2.1 缓解时延

如果一个方法花费过长时间才返回结果，断路器也可以通过超时设置来减少延迟。在默认情况下，所有使用 `@HystrixCommand` 注解的方法，会在 1 秒后调用声明的降级方法。也就是说，如果由于某种原因，ingredient 服务响应迟缓，调用 `getAllIngredients()` 方法在 1 秒后超时，`getDefaultIngredients()` 将会被调用。

1 秒钟超时是一个合理的默认值，适合大多数情况。但是，您可以通过指定 Hystrix 命令属性来更改此值。命令属性是一个或多个 `@HystrixProperty` 注解数组，指定要设置的属性的名称和值。

为了调整断路器的超时设置，需要配置 Hystrix 命令属性 `execution.isolation.thread.timeoutInMilliseconds`。例如，将 `getAllIngredients()` 方法的超时时间设为半秒，可以将超时时间设置为 500，如下所示：

```java
@HystrixCommand(
    fallbackMethod="getDefaultIngredients",
    commandProperties={
        @HystrixProperty(
            name="execution.isolation.thread.timeoutInMilliseconds",
            value="500")
    })
public Iterable<Ingredient> getAllIngredients() {
  ...
}
```

给定的值以毫秒为单位。如果想放宽限制，可以将其设置为更高的值。或者，如果您认为不应该强制超时，那么可以通过设置 `execution.timeout.enabled` 为 false 来完全删除超时设置：

```java
@HystrixCommand(
    fallbackMethod="getDefaultIngredients",
    commandProperties={
        @HystrixProperty(
            name="execution.timeout.enabled",
            value="false")
    })
public Iterable<Ingredient> getAllIngredients() {
  ...
}
```

当 `execution.timeout.enabled` 属性设置为 false 时，没有延迟保护。在本例中，不管 `getAllingRedments()` 方法需要 1 秒，10 秒，还是 30 分钟，都不会超时。这可能会导致级联延迟效应，因此，在禁用执行超时设置时，应非常小心。
