【问题标题】:Spring Cloud Consul client does not ignore critical servicesSpring Cloud Consul 客户端不会忽略关键服务
【发布时间】:2021-08-08 10:43:25
【问题描述】:

我已经实现了一个基于 Spring Cloud 的应用程序,它具有以下主要架构。 GatewayService 代表HelloService 的多个实例。我使用 Consul 进行服务发现。 GatewayService的实现方式如下:

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {

  public static void main(String[] args) {
    SpringApplication.run(ApiGatewayApplication.class, args);
  }

  @Bean
  @LoadBalanced
  public WebClient.Builder loadBalancedWebClientBuilder() {
    return WebClient.builder();
  }

  @RestController
  static class GateWayController {

    private final WebClient.Builder webClientBuilder;

    @Data
    @NoArgsConstructor
    private static class Response {
      private String message;
      private String appName;
      private int port;
    }

    @Autowired
    public GateWayController(WebClient.Builder webClientBuilder) {
      this.webClientBuilder = webClientBuilder;
    }

    @GetMapping("/lbhello")
    public Mono<Response> hello() {
      return webClientBuilder.build()
                             .get()
                             .uri("lb://hello-service/hello")
                             .retrieve()
                             .bodyToMono(Response.class);
    }
  }
}

spring-cloud-starter-consul-discoveryspring-cloud-starter-loadbalancer 作为依赖项包含在 pom.xml 中。在application.yaml 中我只设置了application.name 并禁用Ribbon,以便使用Spring Cloud Loadbalancer。当我启动GatewayServiceHelloService 的两个实例时,一切都按预期工作。对lbhello 端点的服务调用交替委托给两个HelloService 实例。

HelloService 的一个实例停止时,Consul 检测到该实例缺少将其标记为关键。因此,我希望负载均衡器至少在一段时间后停止将请求转发到不存在的服务。但这永远不会发生。每隔一个服务调用就会委托给这个服务实例,因此会失败。当使用 Eureka 作为发现服务时,注册表会被调整,并且只有剩余的 HelloService 会被考虑用于服务调用。

这是 Consul 的预期行为吗?如果答案是肯定的,是否有 Spring Cloud 设置来适应这种行为?

【问题讨论】:

标签: java spring-boot spring-cloud spring-cloud-consul


【解决方案1】:

Spring Cloud ConsulDiscoveryClient 默认加载所有服务实例,不管它们的健康状态如何。可以通过以下配置设置更改此行为:

spring.cloud.consul.discovery.query-passing = true

使用此设置,请求不会在延迟一段时间后委托给非健康实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多