【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)方法里面调用了一个父类Supplier的get()方法

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

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


有withCaching()

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

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

如果没有withCaching()

则会调用HintBasedServiceInstanceListSupplier的get()方法,在HintBasedServiceInstanceListSupplier中首先会调用ServiceInstanceListSupplier的get(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

​

相关推荐
Helix2504 分钟前
JDK 与 Maven 下载获取与构建配置实战:release 参数与 toolchains 编译版本对齐指南
java·jdk·maven·配置·toolchains·java构建·release参数
CoderYanger15 分钟前
Java EE 进阶:11. SpringBoot 统一功能处理(第三弹):统一数据返回格式+统一异常处理
java·数据库·spring boot·后端·spring·java-ee·mybatis
Helix25023 分钟前
‌Tomcat 下载安装配置完整教程
java·环境变量配置·tomcat配置·tomcat 11·java web服务器·jdk配置·web服务器部署
软件技术NINI1 小时前
基于SpringBoot的滑雪场设备租赁系统
java·spring boot·后端
考虑考虑9 小时前
JDK27结构化并发
java·后端·java ee
金銀銅鐵10 小时前
带您看懂字节码指令(五):控制流里有什么
java·jvm·后端
Cosolar12 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
真上帝的左手19 小时前
31. 占位-14
分布式·spring cloud·架构·系统架构
TiDB 社区干货传送门20 小时前
当向量走向百亿规模:平凯数据库对下一代分布式向量索引的探索
数据库·分布式
会编程的吕洞宾21 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring