spring webclient负载均衡

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

以前从未使用过带负载平衡的Web客户端,我放松了https://spring.io/guides/gs/spring-cloud-loadbalancer/并实现了Web客户端负载均衡器,现在我尝试使用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”你就会看到错误。您可以在https://github.com/ozkanpakdil/spring-examples/tree/master/web-client-loadbalancer查看配置。

java spring-boot spring-cloud-loadbalancer
1个回答
3
投票

感谢您提供样品。已经经历过了。有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
    ,并且请求在
    /actuator/health
    端点发送。由于测试使用的
    httpbin
    中未配置此端点,因此我们得到
    404
    。更改属性中的运行状况检查路径将解决该问题:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.