3

以前从未使用过带有负载平衡的 webclient,我休养了https://spring.io/guides/gs/spring-cloud-loadbalancer/并实现了 webclient 负载平衡器,现在我正在尝试使用 helthchecks 并遇到问题。

    @Bean
    @Primary
    ServiceInstanceListSupplier serviceInstanceListSupplier(ConfigurableApplicationContext ctx) {
        return ServiceInstanceListSupplier
                .builder()
                .withRetryAwareness()
                .withHealthChecks()
                .withBase(new RestCaller("restCaller"))
                .build(ctx);
    }

我得到了下面的错误

2021-06-27 17:32:01.562  WARN 12252 --- [     parallel-4] o.s.c.l.core.RoundRobinLoadBalancer      : No servers available for service: httpbin.org
2021-06-27 17:32:01.564  WARN 12252 --- [     parallel-4] eactorLoadBalancerExchangeFilterFunction : LoadBalancer does not contain an instance for the service httpbin.org
2021-06-27 17:32:01.606  WARN 12252 --- [     parallel-4] o.s.c.l.core.RoundRobinLoadBalancer      : No servers available for service: httpbin.org
2021-06-27 17:32:01.606  WARN 12252 --- [     parallel-4] eactorLoadBalancerExchangeFilterFunction : LoadBalancer does not contain an instance for the service httpbin.org
2021-06-27 17:32:01.607  WARN 12252 --- [     parallel-4] o.s.c.l.core.RoundRobinLoadBalancer      : No servers available for service: httpbin.org
2021-06-27 17:32:01.607  WARN 12252 --- [     parallel-4] eactorLoadBalancerExchangeFilterFunction : LoadBalancer does not contain an instance for the service httpbin.org
2021-06-27 17:32:01.607  WARN 12252 --- [     parallel-4] o.s.c.l.core.RoundRobinLoadBalancer      : No servers available for service: restCaller
2021-06-27 17:32:01.608  WARN 12252 --- [     parallel-4] eactorLoadBalancerExchangeFilterFunction : LoadBalancer does not contain an instance for the service restCaller

当我评论“withHealthChecks()”时,一切都按预期工作。我的主要目标是禁用“DefaultServiceInstance”以防它失败(意味着 http 状态 503 或 404 或任何错误)。

我在https://github.com/ozkanpakdil/spring-examples/tree/master/web-client-loadbalancer准备了一个复制器,只需运行“mvn test”你就会看到错误。您可以在 fhttps://github.com/ozkanpakdil/spring-examples/tree/master/web-client-loadbalancer 查看配置。

4

1 回答 1

1

感谢您提供样品。经历过。有2个问题:

  1. 同一个@LoadBalanced WebClient.Builder实例用于处理原始请求和发送运行状况检查请求,因此来自的调用HealthCheckServiceInstanceListSupplier是通过负载平衡Webclient而不是非负载平衡来完成的。由于在此阶段使用的是真实主机,因此应使用非负载平衡Webclient实例。您可以通过Webclient.Builder在配置中实例化 2 个单独的 bean 并使用限定符将非负载平衡的 bean 传递给来实现它HealthCheckServiceInstanceListSupplier,如下所示:

     @Configuration
     @LoadBalancerClient(name = "restCaller", configuration = RestCallerConfiguration.class)
     public class WebClientConfig {
    
     @LoadBalanced
     @Bean
     @Qualifier("loadBalancedWebClientBuilder")
     WebClient.Builder loadBalancedWebClientBuilder() {
         return WebClient.builder();
     }
    
     @Bean
     @Qualifier("webClientBuilder")
     WebClient.Builder webClientBuilder() {
         return WebClient.builder();
     }
    }
    
     @Configuration
     public class RestCallerConfiguration {
    
     @Autowired
     @Qualifier("webClientBuilder")
     WebClient.Builder webClientBuilder;
    
     @Bean
     @Primary
     ServiceInstanceListSupplier     serviceInstanceListSupplier(ConfigurableApplicationContext ctx) {
         return ServiceInstanceListSupplier
                 .builder()
                 .withRetryAwareness()
                 .withHealthChecks(webClientBuilder.build())
                 .withBase(new RestCaller("restCaller"))
                 .build(ctx);
     }
    
  2. HealthCheckServiceInstanceListSupplier运行状况检查 URL 发送请求以验证服务实例是否处于活动状态。默认情况下,我们假设协作服务具有spring-boot-starter-actuator它们的依赖关系,并且请求是在/actuator/health端点发送的。由于此端点未在httpbin测试使用的 中配置,因此我们得到一个404. 更改属性中的运行状况检查路径将解决该问题:

     spring.cloud.loadbalancer.health-check.path.default=/
    

我在这里推送了一个具有固定配置的分支。如果您使用此设置运行测试,它将通过。

于 2021-09-08T13:25:31.957 回答