为什么我的 Spring Consul Retry 配置不起作用

问题描述 投票:0回答:2

我正在尝试在 Spring Consul 中使用重试功能,以便应用程序在一定次数的重试后快速失败。 我正在使用以下 bootstrap.yml

spring:
  profiles:
    active: dev
  application:
    name: consultest
  cloud:
    consul:
      token: ${token}
      enabled: true
      host: ${host}
      port: 8500
      retry:
        initial-interval: 5000
        max-attempts: 6
        max-interval: 600000
        multiplier: 3
  profiles: dev

我的 pom.xml 也有以下依赖项

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>

我使用的是springboot版本2.2.2.RELEASE。

问题是,每当我启动应用程序并且它无法连接到consul时,它只尝试一次,然后应用程序失败

以下是日志,

2023-01-25 09:08:16.245 ERROR 1166405 --- [           main] o.s.c.c.c.ConsulPropertySourceLocator    : Fail fast is set and there was an error reading configuration from consul.
2023-01-25 09:08:16.252 ERROR 1166405 --- [           main] o.s.boot.SpringApplication               : Application run failed

如您所见,应用程序不会重试。 我是否缺少一些配置?谁能帮忙解决这个问题吗?

spring-boot consul spring-cloud-consul
2个回答
0
投票

看来您没有将fail-fast属性设置为false。因为这是真的,如果 consul 不可用,应用程序将抛出异常。 我请您参阅有关 consul 服务的 spring 文档,这里


0
投票

对于此配置 - 您需要通过设置

enable
retry
retry: enabled: true
- 像这样。

spring:
  cloud:
    consul:
      retry:
        enabled: true

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.