【Loadbalancer】解决withHints()配置不生效问题

解决办法

配置类

官方示例中提到,要使用hint可以采用下面的方式开启:

java 复制代码
public class CustomLoadBalancerConfiguration {

    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
            ConfigurableApplicationContext context) {
        return ServiceInstanceListSupplier.builder()
                    .withDiscoveryClient()
                    .withHints()
                    .withCaching()
                    .build(context);
    }
}

但是我按照上面的配置后,hint不会生效,debug时不会进入到filteredByHint(),在执行完cache相关的类后直接返回。

因此,关掉cache使hint生效,即:

java 复制代码
public class CustomLoadBalancerConfiguration {

    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
            ConfigurableApplicationContext context) {
        return ServiceInstanceListSupplier.builder()
                    .withDiscoveryClient()
                    .withHints()
                    .build(context);
    }
}

此外,如果同时使用了基于区间的过滤,需要注意二者的顺序:

java 复制代码
public class CustomLoadBalancerConfiguration {

    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
            ConfigurableApplicationContext context) {
        return ServiceInstanceListSupplier.builder()
                    .withDiscoveryClient()
                    .withZonePreference()
                    .withHints()
                    .build(context);
    }
}

原因

有个choose()方法,返回时,调用了ServiceInstanceListSupplier接口的default Flux<List<ServiceInstance>> get(Request request)方法。

default Flux<List<ServiceInstance>> get(Request request)方法里面调用了一个父类Supplierget()方法

java 复制代码
// ServiceInstanceListSupplier
default Flux<List<ServiceInstance>> get(Request request) {
	return get();
}

而这个接口有好几个实现类,其中包括ZonePreferenceServiceInstanceListSupplierCachingServiceInstanceListSupplierHintBasedServiceInstanceListSupplier这三个类:


withCaching()

会先进入CachingServiceInstanceListSupplier类中,执行它的get()方法,直接返回一个Flux<List<ServiceInstance>>,然后直接进行response。

java 复制代码
// CachingServiceInstanceListSupplier
@Override
public Flux<List<ServiceInstance>> get() {
	return serviceInstances;
}

如果没有withCaching()

则会调用HintBasedServiceInstanceListSupplierget()方法,在HintBasedServiceInstanceListSupplier中首先会调用ServiceInstanceListSupplierget(request)方法(上面的get()方法)

java 复制代码
// HintBasedServiceInstanceListSupplier
@Override
public Flux<List<ServiceInstance>> get(Request request) {
	return delegate.get(request).map(instances -> filteredByHint(instances, getHint(request.getContext())));
}

这次get()调用的实现类是ZonePreferenceServiceInstanceListSupplier,在里面进行基于zone过滤

java 复制代码
// ZonePreferenceServiceInstanceListSupplier
@Override
public Flux<List<ServiceInstance>> get() {
	return getDelegate().get().map(this::filteredByZone);
}

总结:

java 复制代码
choose(){
	HintBasedServiceInstanceListSupplier.get(){
		ZonePreferenceServiceInstanceListSupplier.getDelegate().get().map(this::filteredByZone);
	}
}

application.yml文件

消费者

yaml 复制代码
server:
  port: 8080

spring:
  application:
    name: consumer-01

  cloud:
    nacos:
      server-addr: localhost:8848

    loadbalancer:
      ribbon:
        enabled: false
      enabled: true
      configurations: zone-preference
      zone: myzone
      hint:
        default: myhint

生产者

yaml 复制代码
server:
  port: 8081

spring:
  application:
    name: provider-01
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        metadata:
          zone: myzone
          hint: myhint
    loadbalancer:
      ribbon:
        enabled: false
      enabled: true
      configurations: zone-preference

相关推荐
东北甜妹11 分钟前
Redis Cluster 操作命令
java·开发语言
消失的旧时光-194330 分钟前
Spring Boot 核心机制之 @Conditional:从原理到实战(一次讲透)
java·spring boot·后端
石榴树下的七彩鱼34 分钟前
智能抠图 API 接入实战:3 行代码实现图片自动去背景(Python / Java / PHP / JS)
java·图像处理·人工智能·python·php·api·抠图
知兀37 分钟前
【Result类】(使用/不使用<T> data的情况);自带静态方法、纯数据类;
java·开发语言
Seven9738 分钟前
【从0到1构建一个ClaudeAgent】协作-自主Agent
java
洋不写bug44 分钟前
Java线程(三):线程执行顺序问题、可重入锁、加锁操作解析,死锁解决
java·开发语言
lifallen1 小时前
Flink Source / Sink Exactly-Once 边界分析
java·大数据·flink
xyyaihxl1 小时前
将 vue3 项目打包后部署在 springboot 项目运行
java·spring boot·后端
行者-全栈开发2 小时前
拆解高可用CRM网站的容灾设计与云原生实践
微服务·云原生·异地多活·监控告警·高可用设计·crm架构·容灾演练
0xDevNull2 小时前
Spring Boot 3.x 整合 Nacos 全栈实战教程
java·spring boot·nacos