【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

相关推荐
sg_knight4 小时前
Spring 框架中的 SseEmitter 使用详解
java·spring boot·后端·spring·spring cloud·sse·sseemitter
郑州光合科技余经理6 小时前
同城系统海外版:一站式多语种O2O系统源码
java·开发语言·git·mysql·uni-app·go·phpstorm
一只乔哇噻6 小时前
java后端工程师+AI大模型开发进修ing(研一版‖day60)
java·开发语言·人工智能·学习·语言模型
Dolphin_Home6 小时前
笔记:SpringBoot静态类调用Bean的2种方案(小白友好版)
java·spring boot·笔记
MetaverseMan7 小时前
Java虚拟线程实战
java
刘一说7 小时前
Nacos 权限控制详解:从开源版 v2.2+ 到企业级安全实践
spring boot·安全·spring cloud·微服务·nacos·架构·开源
浪潮IT馆7 小时前
Tomcat运行war包的问题分析与解决步骤
java·tomcat
悟能不能悟8 小时前
Caused by: java.sql.SQLException: ORA-28000: the account is locked怎么处理
java·开发语言
_院长大人_8 小时前
MyBatis Plus 分批查询优化实战:优雅地解决 IN 参数过多问题(实操)
java·mybatis
IIIIIILLLLLLLLLLLLL8 小时前
Hadoop集群时间同步方法
大数据·hadoop·分布式