spring 网络客户端负载均衡

spring webclient load balance

以前从未使用过带有负载平衡的 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.

查看配置

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

  1. 同一个 @LoadBalanced WebClient.Builder 实例用于处理原始请求和发送健康检查请求,因此来自 HealthCheckServiceInstanceListSupplier 的调用是通过加载完成的平衡 Webclient 而不是非负载平衡的。由于在这个阶段正在使用真实主机,因此应该使用非负载平衡的 Webclient 实例。您可以通过在配置中实例化 2 个单独的 Webclient.Builder 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,并且请求在 th/actuator/health 端点发送。由于此端点未在测试使用的 httpbin 中配置,因此我们得到 404。更改属性中的运行状况检查路径将解决此问题:

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

我已经推送了一个具有固定配置的分支 here。如果您 运行 使用此设置进行测试,它会通过。