Ribbon源码

news2024/9/21 22:46:26
学了feign源码之后感觉,这部分还是按运行流程分块学合适。核心组件什么的,当专业术语学妥了。

序章:认识真正のRibbon

但只用认识一点点

之前我们学习Ribbon的简单使用时,都是集成了Eureka-client或者Feign等组件,甚至在使用时感受不到ribbon的存在,顶多提一嘴他的负载均衡接口。而本文要专注于讲解微服务体系下,Ribbon部分的基本原理。

ribbon项目目前已经进入On Maintenance维护状态,大家可以学的轻松些。

摘自Github:

Ribbon is a client side IPC library that is battle-tested in cloud. It provides the following features

  • Load balancing 负载均衡
  • Fault tolerance 容错
  • Multiple protocol (HTTP, TCP, UDP) support in an asynchronous and reactive model
    • 多协议、异步、reactive模型
  • Caching and batching 缓存和批处理

Ribbon是一个久经沙场的IPC库。IPC(Inter-Process Communication)进程间通信,是指两个进程的数据之间产生交互。

Ribbon的核心类

ribbon-loadbalancer模块

Server

对服务实例的封装,里面封装了服务实例的ip、端口、是否可用等信息。

ServerList

ServerList是个接口,提供了两个方法,都是获取Server实例列表的。

eureka、nacos等注册中心都实现了这个接口,将注册中心的服务实例数据提供给Ribbon,供Ribbon来进行负载均衡。

public interface ServerList<T extends Server> {
    public List<T> getInitialListOfServers();        
    /**
    * Return updated list of servers. This is called say every 30 secs 
    * (configurable) by the Loadbalancer's Ping cycle     
    *
    */    
    public List<T> getUpdatedListOfServers();   
}
  • 是Eureka-client里实现了吗?那单独的ribbon有没有实现?能不能单独用Ribbon?
  • 这个get是说从注册中心拿,还是本地调用的时候从这里内存拿?
  • nacos怎么实现的?怎么和Feign结合的?

3、ServerListUpdater

用来更新服务注册列表的数据,他有唯一的实现类PollingServerListUpdater,该实现类有一个核心的方法start()。

start()方法首先通过CAS原子操作:isActive.compareAndSet(false, true)来获取锁(并发编程操作),然后将传入的参数updateAction的doUpdate()方法封装进了一个Runnable,然后包装好的Runnable扔到了定时调度的线程池,经过initialDelayMs(默认1s)时间后,会调用第一次,之后每隔refreshIntervalMs(默认30s)调用一次。

  • 参数updateAction哪里来,干什么?
@Override    
public synchronized void start(final UpdateAction updateAction) {   
    if (isActive.compareAndSet(false, true)) {           
        final Runnable wrapperRunnable = new Runnable() {     
            @Override            
            public void run() {    
                if (!isActive.get()) {      
                    if (scheduledFuture != null) {    
                        scheduledFuture.cancel(true);      
                    }
                    return;
                }
                try {
                    updateAction.doUpdate();   
                    lastUpdated = System.currentTimeMillis();   
                } catch (Exception e) {       
                    logger.warn("Failed one update cycle", e);     
                }
            }
        };
        scheduledFuture = getRefreshExecutor().scheduleWithFixedDelay(
            wrapperRunnable, 
            initialDelayMs,
            refreshIntervalMs, 
            TimeUnit.MILLISECONDS);  
    } else { 
        logger.info("Already active, no-op"); 
    }
}

4、IRule

public interface IRule{
    /*  
    * choose one alive server from lb.allServers or   
    * lb.upServers according to key     
    *
    * @return choosen Server object. NULL is returned if none    
    *  server is available 
    */
    public Server choose(Object key);        public void setLoadBalancer(ILoadBalancer lb);        public ILoadBalancer getLoadBalancer();    }

IRule是负载均衡的算法的统一接口,其实现类是各种负载均衡算法的具体实现。

比如说RandomRule类,随机轮询。

5、IClientConfig

配置接口,有个默认的实现DefaultClientConfigImpl,通过这个可以获取到一些配置Ribbon的一些配置。

6、ILoadBalancer

public interface ILoadBalancer {
    public void addServers(List<Server> newServers);   
    public Server chooseServer(Object key);   
    public void markServerDown(Server server);  
    @Deprecated 
    public List<Server> getServerList(boolean availableOnly);
    public List<Server> getReachableServers();
    public List<Server> getAllServers();
}

ILoadBalancer主要提供了获取服务实例列表和选择服务实例的功能。

虽然对外主要提供获取服务的功能,但是在实现的时候,主要是用来协调上面提到的各个核心组件的,使得他们能够协调工作

这个接口的实现有好几个实现类,但是我讲两个比较重要的。

BaseLoadBalancer

public class BaseLoadBalancer extends AbstractLoadBalancer implements
        PrimeConnections.PrimeConnectionListener, IClientConfigAware {
   
    private final static IRule DEFAULT_RULE = new RoundRobinRule();    
    protected IRule rule = DEFAULT_RULE;
    private IClientConfig config; 

    protected volatile List<Server> allServerList = Collections.synchronizedList(new ArrayList<Server>());
    protected volatile List<Server> upServerList = Collections.synchronizedList(new ArrayList<Server>());
    
    public BaseLoadBalancer(String name, IRule rule, LoadBalancerStats stats,
            IPing ping, IPingStrategy pingStrategy) {
  
        logger.debug("LoadBalancer [{}]:  initialized", name);
        
        this.name = name;
        this.ping = ping;
        this.pingStrategy = pingStrategy;
        setRule(rule);
        setupPingTask();
        lbStats = stats;
        init();
    }

    public BaseLoadBalancer(IClientConfig config) {
        initWithNiwsConfig(config);
    }
    public BaseLoadBalancer(IClientConfig config, IRule rule, IPing ping) {
        initWithConfig(config, rule, ping, createLoadBalancerStatsFromConfig(config));
    }

    void initWithConfig(IClientConfig clientConfig, IRule rule, IPing ping, LoadBalancerStats stats) {
        this.config = clientConfig;
        String clientName = clientConfig.getClientName();
        this.name = clientName;
        int pingIntervalTime = Integer.parseInt(""
                + clientConfig.getProperty(
                        CommonClientConfigKey.NFLoadBalancerPingInterval,
                        Integer.parseInt("30")));
        int maxTotalPingTime = Integer.parseInt(""
                + clientConfig.getProperty(
                        CommonClientConfigKey.NFLoadBalancerMaxTotalPingTime,
                        Integer.parseInt("2")));

        setPingInterval(pingIntervalTime);
        setMaxTotalPingTime(maxTotalPingTime);

        // cross associate with each other
        // i.e. Rule,Ping meet your container LB
        // LB, these are your Ping and Rule guys ...
        setRule(rule);
        setPing(ping);

        setLoadBalancerStats(stats);
        rule.setLoadBalancer(this);
        if (ping instanceof AbstractLoadBalancerPing) {
            ((AbstractLoadBalancerPing) ping).setLoadBalancer(this);
        }
        logger.info("Client: {} instantiated a LoadBalancer: {}", name, this);
        boolean enablePrimeConnections = clientConfig.get(
                CommonClientConfigKey.EnablePrimeConnections, DefaultClientConfigImpl.DEFAULT_ENABLE_PRIME_CONNECTIONS);

        if (enablePrimeConnections) {
            this.setEnablePrimingConnections(true);
            PrimeConnections primeConnections = new PrimeConnections(
                    this.getName(), clientConfig);
            this.setPrimeConnections(primeConnections);
        }
        init();

    }
    
    public void setRule(IRule rule) {
        if (rule != null) {
            this.rule = rule;
        } else {
            /* default rule */
            this.rule = new RoundRobinRule();
        }
        if (this.rule.getLoadBalancer() != this) {
            this.rule.setLoadBalancer(this);
        }
    }
    
     public Server chooseServer(Object key) {
        if (counter == null) {
            counter = createCounter();
        }
        counter.increment();
        if (rule == null) {
            return null;
        } else {
            try {
                return rule.choose(key);
            } catch (Exception e) {
                logger.warn("LoadBalancer [{}]:  Error choosing server for key {}", name, key, e);
                return null;
            }
        }
    }
   
}

核心属性

allServerList:缓存了所有的服务实例数据

upServerList:缓存了能够使用的服务实例数据。

rule:负载均衡算法组件,默认是RoundRobinRule

核心方法

setRule:这个方法是设置负载均衡算法的,并将当前这个ILoadBalancer对象设置给IRule,从这可以得出一个结论,IRule进行负载均衡的服务实例列表是通过ILoadBalancer获取的,也就是 IRule 和 ILoadBalancer相互引用。setRule(rule)一般是在构造对象的时候会调用。

chooseServer:就是选择一个服务实例,是委派给IRule的choose方法来实现服务实例的选择。

DynamicServerListLoadBalancer

public class DynamicServerListLoadBalancer<T extends Server> extends BaseLoadBalancer {    private static final Logger LOGGER = LoggerFactory.getLogger(DynamicServerListLoadBalancer.class);
    volatile ServerList<T> serverListImpl;    volatile ServerListFilter<T> filter;    protected final ServerListUpdater.UpdateAction updateAction = new ServerListUpdater.UpdateAction() {        @Override        public void doUpdate() {            updateListOfServers();        }    };    protected volatile ServerListUpdater serverListUpdater;        public DynamicServerListLoadBalancer(IClientConfig clientConfig, IRule rule, IPing ping,                                         ServerList<T> serverList, ServerListFilter<T> filter,                                         ServerListUpdater serverListUpdater) {        super(clientConfig, rule, ping);        this.serverListImpl = serverList;        this.filter = filter;        this.serverListUpdater = serverListUpdater;        if (filter instanceof AbstractServerListFilter) {            ((AbstractServerListFilter) filter).setLoadBalancerStats(getLoadBalancerStats());        }        restOfInit(clientConfig);    }            @Override    public void setServersList(List lsrv) {        super.setServersList(lsrv);        List<T> serverList = (List<T>) lsrv;        Map<String, List<Server>> serversInZones = new HashMap<String, List<Server>>();        for (Server server : serverList) {            // make sure ServerStats is created to avoid creating them on hot            // path            getLoadBalancerStats().getSingleServerStat(server);            String zone = server.getZone();            if (zone != null) {                zone = zone.toLowerCase();                List<Server> servers = serversInZones.get(zone);                if (servers == null) {                    servers = new ArrayList<Server>();                    serversInZones.put(zone, servers);                }                servers.add(server);            }        }        setServerListForZones(serversInZones);    }
    protected void setServerListForZones(            Map<String, List<Server>> zoneServersMap) {        LOGGER.debug("Setting server list for zones: {}", zoneServersMap);        getLoadBalancerStats().updateZoneServerMapping(zoneServersMap);    }
    @VisibleForTesting    public void updateListOfServers() {        List<T> servers = new ArrayList<T>();        if (serverListImpl != null) {            servers = serverListImpl.getUpdatedListOfServers();            LOGGER.debug("List of Servers for {} obtained from Discovery client: {}",                    getIdentifier(), servers);
            if (filter != null) {                servers = filter.getFilteredListOfServers(servers);                LOGGER.debug("Filtered List of Servers for {} obtained from Discovery client: {}",                        getIdentifier(), servers);            }        }        updateAllServerList(servers);    }
    /**     * Update the AllServer list in the LoadBalancer if necessary and enabled     *      * @param ls     */    protected void updateAllServerList(List<T> ls) {        // other threads might be doing this - in which case, we pass        if (serverListUpdateInProgress.compareAndSet(false, true)) {            try {                for (T s : ls) {                    s.setAlive(true); // set so that clients can start using these                                      // servers right away instead                                      // of having to wait out the ping cycle.                }                setServersList(ls);                super.forceQuickPing();            } finally {                serverListUpdateInProgress.set(false);            }        }    }}

DynamicServerListLoadBalancer继承自BaseLoadBalancer, 进行了一些扩展。

成员变量

serverListImpl:上面说过,通过这个接口获取服务列表

filter:起到过滤的作用

updateAction:是个匿名内部类,实现了doUpdate方法,会调用updateListOfServers方法

serverListUpdater:上面说到过,默认就是唯一的实现类PollingServerListUpdater,也就是每个30s就会调用传入的updateAction的doUpdate方法。

这不是巧了么,serverListUpdater的start方法需要一个updateAction,刚刚好成员变量有个updateAction的匿名内部类的实现,所以serverListUpdater的start方法传入的updateAction的实现其实就是这个匿名内部类。
那么哪里调用了serverListUpdater的start方法传入了updateAction呢?是在构造的时候调用的,具体的调用链路是调用 restOfInit -> enableAndInitLearnNewServersFeature(),这里就不贴源码了

所以,其实DynamicServerListLoadBalancer在构造完成之后,默认每隔30s中,就会调用updateAction的匿名内部类的doUpdate方法,从而会调用updateListOfServers。所以我们来看一看 updateListOfServers 方法干了什么。

public void updateListOfServers() {
        List<T> servers = new ArrayList<T>();
        if (serverListImpl != null) {
            servers = serverListImpl.getUpdatedListOfServers();
            LOGGER.debug("List of Servers for {} obtained from Discovery client: {}",
                    getIdentifier(), servers);

            if (filter != null) {
                servers = filter.getFilteredListOfServers(servers);
                LOGGER.debug("Filtered List of Servers for {} obtained from Discovery client: {}",
                        getIdentifier(), servers);
            }
        }
        updateAllServerList(servers);
   }

这个方法实现很简单,就是通过调用 ServerList 的getUpdatedListOfServers获取到一批服务实例数据,然后过滤一下,最后调用updateAllServerList方法。

查看updateAllServerList()源码:

  protected void updateAllServerList(List<T> ls) {
        // other threads might be doing this - in which case, we pass
        if (serverListUpdateInProgress.compareAndSet(false, true)) {
            try {
                for (T s : ls) {
                    s.setAlive(true); // set so that clients can start using these
                                      // servers right away instead
                                      // of having to wait out the ping cycle.
                }
                setServersList(ls);
                super.forceQuickPing();
            } finally {
                serverListUpdateInProgress.set(false);
            }
        }
    }

该方法调用每个服务实例的setAlive()方法,将isAliveFlag设置成true,然后调用setServersList()将服务实例更新到内部的缓存中,也就是上面提到的allServerList和upServerList中,这里就不贴源码了。

其实分析完updateListOfServers方法之后,再结合上面源码的分析,我们可以清楚的得出一个结论,那就是默认每隔30s都会重新通过ServerList组件获取到服务实例数据,然后更新到BaseLoadBalancer缓存中,IRule的负载均衡所需的服务实例数据,就是这个内部缓存。

从DynamicServerListLoadBalancer的命名也可以看出,他相对于父类BaseLoadBalancer而言,提供了动态更新内部服务实例列表的功能。

图片

说完一些核心的组件,以及他们跟ILoadBalancer的关系之后,接下来就来分析一下,ILoadBalancer是在ribbon中是如何使用的。

8、AbstractLoadBalancerAwareClient

ILoadBalancer是一个可以获取到服务实例数据的组件,AbstractLoadBalancerAwareClient,这个是用来执行请求的,我们来看一下这个类的构造。

   public AbstractLoadBalancerAwareClient(ILoadBalancer lb) {      
       super(lb); 
   } 
/**
* Delegate to {@link #initWithNiwsConfig(IClientConfig)} 
* @param clientConfig
*/
public AbstractLoadBalancerAwareClient(ILoadBalancer lb, IClientConfig clientConfig) {    
    super(lb, clientConfig);   
}

通过上面可以看出,在构造的时候需要传入一个ILoadBalancer。

AbstractLoadBalancerAwareClient中有一个方法executeWithLoadBalancer,这个是用来执行传入的请求,以负载均衡的方式。

   public T executeWithLoadBalancer(final S request, final IClientConfig requestConfig) throws ClientException {        LoadBalancerCommand<T> command = buildLoadBalancerCommand(request, requestConfig);
        try {            return command.submit(                new ServerOperation<T>() {                    @Override                    public Observable<T> call(Server server) {                        URI finalUri = reconstructURIWithServer(server, request.getUri());                        S requestForServer = (S) request.replaceUri(finalUri);                        try {                            return Observable.just(AbstractLoadBalancerAwareClient.this.execute(requestForServer, requestConfig));                        }                         catch (Exception e) {                            return Observable.error(e);                        }                    }                })                .toBlocking()                .single();        } catch (Exception e) {            Throwable t = e.getCause();            if (t instanceof ClientException) {                throw (ClientException) t;            } else {                throw new ClientException(e);            }        }            }

这个方法构建了一个LoadBalancerCommand,随后调用了submit方法,传入了一个匿名内部类,这个匿名内部类中有这么一行代码很重要。

URI finalUri = reconstructURIWithServer(server, request.getUri());

这行代码是根据给定的一个Server重构了URI,reconstructURIWithServer干的一件事就是将ServerA服务名替换成真正的服务所在的机器的ip和端口,这样就能发送http请求到ServerA服务所对应的一台服务器了。

之后根据新的地址,调用这个类中的execute方法来执行请求,execute方法是个抽象方法,也就是交给子类实现,子类就可以通过实现这个方法,来发送http请求,实现rpc调用。

那么这台Server是从获取的呢?肯定是通过ILoadBalancer获取的。直接贴出submit方法中核心的一部分代码

Observable<T> o = (server == null ? selectServer() : Observable.just(server))

就是通过selectServer来选择一个Server的,selectServer我就不翻源码了,其实最终还是调用ILoadBalancer的方法chooseServer方法来获取一个服务,之后就会调用上面的说的匿名内部类的方法,重构URI,然后再交由子类的execut方法来实现发送http请求。

所以,通过对AbstractLoadBalancerAwareClient的executeWithLoadBalancer方法,我们可以知道,这个抽象类的主要作用就是通过负载均衡算法,找到一个合适的Server,然后将你传入的请求路径http://ServerA/api/sayHello重新构建成类似http://192.168.1.101:8088/api/sayHello这样,之后调用子类实现的execut方法,来发送http请求,就是这么简单。

到这里其实Ribbon核心组件和执行原理我就已经说的差不多了,再来画一张图总结一下

图片

SpringCloud中使用的核心组件的实现都有哪些

说完了Ribbon的一些核心组件和执行原理之后,我们再来看一下在SpringCloud环境下,这些组件到底是用的哪些实现

Ribbon, 启动!(原理)

启动阶段的任务

  • 你们微服务源码流程够有共性的,一起总结下?

Ribbon的自动装配类为RibbonAutoConfiguration,此处粘贴核心源码:

@Configuration
@RibbonClients
public class RibbonAutoConfiguration {

  @Autowired(required = false)
  private List<RibbonClientSpecification> configurations = new ArrayList<>();
  @Bean
  public SpringClientFactory springClientFactory() {
    SpringClientFactory factory = new SpringClientFactory();
    factory.setConfigurations(this.configurations);
    return factory;
  }
}

RibbonAutoConfiguration配置类上有个@RibbonClients注解,接下来讲解一下这个注解的作用

@Import(RibbonClientConfigurationRegistrar.class)
public @interface RibbonClients {
  RibbonClient[] value() default {};
  Class<?>[] defaultConfiguration() default {};
}

OpenFeign的小伙伴肯定知道,要使用Feign,得需要使用@EnableFeignClients,@EnableFeignClients的作用可以扫描指定包路径下的@FeignClient注解,也可以声明配置类;

RibbonClients的作用也是可以声明配置类,同样也使用了@Import注解来实现,RibbonClientConfigurationRegistrar这个配置类的作用就是往spring容器中注入每个服务的Ribbon组件(@RibbonClient里面可以声明每个服务对应的配置)的配置类和默认配置类,将配置类封装为RibbonClientSpecification注入到spring容器中。

RibbonAutoConfiguration的主要作用就是注入了一堆RibbonClientSpecification,就是每个服务对应的配置类,然后声明了SpringClientFactory这个bean,将配置类放入到里面。

SpringClientFactory跟OpenFeign中的FeignContext很像,SpringClientFactory也继承了NamedContextFactory,实现了配置隔离,同时也在构造方法中传入了每个容器默认的配置类RibbonClientConfiguration。至于什么是配置隔离,我在OpenFeign那篇文章说过,不清楚的小伙伴可以后台回复feign01即可获得文章链接。

配置优先级问题

优先级最高的是springboot启动的时候的容器,因为这个容器是每个服务的容器的父容器,而在配置类声明bean的时候,都有@ConditionalOnMissingBean注解,一旦父容器有这个bean,那么子容器就不会初始化。

优先级第二高的是每个客户端声明的配置类,也就是通过@FeignClient和@RibbonClient的configuration属性声明的配置类

优先级第三高的是@EnableFeignClients和@RibbonClients注解中configuration属性声明的配置类

优先级最低的就是FeignContext和SpringClientFactory构造时传入的配置类

至于优先级怎么来的,其实是在NamedContextFactory中createContext方法中构建AnnotationConfigApplicationContext时按照配置的优先级一个一个传进去的。

RibbonClientConfiguration提供的默认的bean

接下来我们看一下RibbonClientConfiguration都提供了哪些默认的bean

  @Bean
  @ConditionalOnMissingBean
  public IClientConfig ribbonClientConfig() {
    DefaultClientConfigImpl config = new DefaultClientConfigImpl();
    config.loadProperties(this.name);
    config.set(CommonClientConfigKey.ConnectTimeout, DEFAULT_CONNECT_TIMEOUT);
    config.set(CommonClientConfigKey.ReadTimeout, DEFAULT_READ_TIMEOUT);
    config.set(CommonClientConfigKey.GZipPayload, DEFAULT_GZIP_PAYLOAD);
    return config;
  }

配置类对应的bean,这里设置了ConnectTimeout和ReadTimeout都是1s中。

  @Bean  @ConditionalOnMissingBean  public IRule ribbonRule(IClientConfig config) {    if (this.propertiesFactory.isSet(IRule.class, name)) {      return this.propertiesFactory.get(IRule.class, config, name);    }    ZoneAvoidanceRule rule = new ZoneAvoidanceRule();    rule.initWithNiwsConfig(config);    return rule;  }

IRule,默认是ZoneAvoidanceRule,这个Rule带有过滤的功能,过滤哪些不可用的分区的服务,过滤成功之后,继续采用线性轮询的方式从过滤结果中选择一个出来。至于这个propertiesFactory,可以不用管,这个是默认读配置文件的中的配置,一般不设置,后面看到都不用care。

@Bean
@ConditionalOnMissingBean
@SuppressWarnings("unchecked")
public ServerList<Server> ribbonServerList(IClientConfig config) {
    if (this.propertiesFactory.isSet(ServerList.class, name)) {
      return this.propertiesFactory.get(ServerList.class, config, name);
    }
    ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
    serverList.initWithNiwsConfig(config);
    return serverList;
}

默认是ConfigurationBasedServerList,也就是基于配置来提供服务实例列表。但是在SpringCloud环境中,服务信息是注册在注册中心的。因此注册中心实现了优先级更高的ServerList。比如Nacos的实现NacosServerList,这里我贴出NacosServerList的bean的声明,在配置类NacosRibbonClientConfiguration中

  @Bean  
  @ConditionalOnMissingBean
  public ServerList<?> ribbonServerList(IClientConfig config, NacosDiscoveryProperties nacosDiscoveryProperties) {
  	NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);    
  	serverList.initWithNiwsConfig(config);    
  	return serverList;
  }

至于为什么容器选择NacosServerList而不是ConfigurationBasedServerList,主要是因为NacosRibbonClientConfiguration这个配置类是通过@RibbonClients导入的,比SpringClientFactory导入的RibbonClientConfiguration配置类优先级高。

  • @ConditionalOnMissingBean?这类注解还是不要在Spring里总结了,大而全,还是需要的时候临时现场介绍下
  • 是说单独使用ribbon的时候用ConfigurationBasedServerList从配置里读取服务列表,使用注册中心的时候,利用优先级覆盖关系,使用注册中心的列表?多么天才的发明!
  • 服务中心怎么通信自己的?怎么 拉取的?nacos也默认集成ribbon吗?
@Bean  
@ConditionalOnMissingBean
public ServerListUpdater ribbonServerListUpdater(IClientConfig config) {    
    return new PollingServerListUpdater(config); 
}

ServerListUpdater,就是我们剖析的PollingServerListUpdater,默认30s更新一次BaseLoadBalancer内部服务的缓存。

@Bean
@ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
      ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
      IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
    if (this.propertiesFactory.isSet(ILoadBalancer.class, name)) {
      return this.propertiesFactory.get(ILoadBalancer.class, config, name);
    }
    return new ZoneAwareLoadBalancer<>(config, rule, ping, serverList,
        serverListFilter, serverListUpdater);
  }

ILoadBalancer,默认是ZoneAwareLoadBalancer,构造的时候也传入了上面声明的的bean,ZoneAwareLoadBalancer这个类继承了DynamicServerListLoadBalancer。

到这里,Ribbon在SpringCloud的配置我们就讲完了,主要就是声明了很多核心组件的bean,最后都设置到ZoneAwareLoadBalancer中。但是,AbstractLoadBalancerAwareClient这个对象的声明我们并没有在配置类中找到,主要是因为这个对象是OpenFeign整合Ribbon的一个入口,至于是如何整合的,这个坑就留给下篇文章吧。

那么在springcloud中,上图就可以加上注册中心。

图片

附录

可能一辈子也没必要看,但如果你真的想看的Ribbon源码地址

在不结合其他微服务组件的情况下,我们也可以单独使用ribbon

<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon</artifactId>
    <version>2.2.2</version>
</dependency>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/811900.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

better scoll右 联左

这是先拿一个数组装进我们所有 获取到的dom节点的 高度 因为算的 都是 到最上面的 高度&#xff0c;所以我们 要减去他的 高度 就得到自身的高度 然后给右边加一个滚动事件&#xff0c;得到每一次滑动的高度&#xff0c;在循环上面的数组&#xff0c;就是我们右边的 y就在算出…

如何排查 IDEA 自身报错?| 以 IntelliJ IDEA 2023.1.4 无法刷新项目 Maven 模块的问题为例

这个问题是 2023 年 7 月 26 日遇到的&#xff0c;当时还是 IDEA 2023.1.4&#xff0c;结果文章还没写完&#xff0c;7 月 27 日自动给更新了 IDEA 2023.2。问题估计解决了。 所以&#xff0c;本文就简单提一下 IDEA 自身报错的排查方法。 规避/解决方式 先说问题怎么处理&am…

重生之我要学C++第五天

这篇文章主要内容是构造函数的初始化列表以及运算符重载在顺序表中的简单应用&#xff0c;运算符重载实现自定义类型的流插入流提取。希望对大家有所帮助&#xff0c;点赞收藏评论&#xff0c;支持一下吧&#xff01; 目录 构造函数进阶理解 1.内置类型成员在参数列表中的定义 …

糟了,数据库主从延迟了!

前言 在实际的生产环境中&#xff0c;由单台MySQL作为独立的数据库是完全不能满足实际需求的&#xff0c;无论是在安全性&#xff0c;高可用性以及高并发等各个方面 因此&#xff0c;一般来说都是通过集群主从复制&#xff08;Master-Slave&#xff09;的方式来同步数据&…

【点云处理教程】05-Python 中的点云分割

一、说明 这是我的“点云处理”教程的第 5 篇文章。“点云处理”教程对初学者友好&#xff0c;我们将在其中简单地介绍从数据准备到数据分割和分类的点云处理管道。 在上一教程中&#xff0c;我们看到了如何过滤点云以减少噪声或其密度。在本教程中&#xff0c;我们将应用一些聚…

LeetCode_11. 盛最多水的容器

题目描述 11. 盛最多水的容器 - 力扣&#xff08;LeetCode&#xff09;https://leetcode.cn/problems/container-with-most-water/ 思路分析 这题就是典型的是一道很经典的面试题&#xff0c;最优的解法是双指针&#xff0c;但很多人在第一次看到这题的时候很难想到用双指针来…

*CTF 2023 Misc

一、 snippingTools Alice在参加某个CTF比赛&#xff0c;她成功的解出了一道题&#xff0c;拿到了flag。她很开心&#xff0c;迫不及待地想要向Bob分享她的喜悦。于是按下了快捷键ShiftWinS使用了Windows 11的截图工具&#xff0c;截取了整个屏幕&#xff0c;并且保存为文件1.p…

无涯教程-jQuery - Spinner组件函数

Widget Spinner 函数可与JqueryUI中的窗口小部件一起使用。Spinner提供了一种从一组中选择一个值的快速方法。 Spinner - 语法 $( "#menu" ).selectmenu(); Spinner - 示例 以下是显示Spinner用法的简单示例- <!doctype html> <html lang"en"…

(树) 剑指 Offer 27. 二叉树的镜像 ——【Leetcode每日一题】

❓剑指 Offer 27. 二叉树的镜像 难度&#xff1a;简单 请完成一个函数&#xff0c;输入一个二叉树&#xff0c;该函数输出它的镜像。 例如输入&#xff1a; 4/ \2 7/ \ / \1 3 6 9镜像输出&#xff1a; 4/ \7 2/ \ / \9 6 3 1示例 1&#xff1a; 输…

给你一个项目,你将如何开展性能测试工作?

一、性能三连问 1、何时进行性能测试&#xff1f; 性能测试的工作是基于系统功能已经完备或者已经趋于完备之上的&#xff0c;在功能还不够完备的情况下没有多大的意义。因为后期功能完善上会对系统的性能有影响&#xff0c;过早进入性能测试会出现测试结果不准确、浪费测试资…

We are the Lights - 思维

分析&#xff1a; 每次操作会把上一次的状态覆盖&#xff0c;但是从后往前操作可以保留最后一次覆盖&#xff0c;每一个位置最后的覆盖状态一定是最终状态&#xff0c;因此可以存下来从后往前记录第一次覆盖的状态并计数。 代码&#xff1a; #include <bits/stdc.h>usi…

玩转LaTeX(一)【源文件基本结构、中文处理方法、中英文的字体字号设置、文档基本结构】

latex源文件基本结构&#xff1a; 【在latex中一般分为两个区&#xff0c;一个是导言区&#xff0c;一个是正文区&#xff08;文稿区&#xff09;】 %导言区(主要进行全局设置)%一个latex文件&#xff0c;只能有且只有一个document环境\documentclass{article} %除article类外…

OpenHarmony开源鸿蒙学习入门 - 基于3.2Release 应用开发环境安装

OpenHarmony开源鸿蒙学习入门 - 基于3.2Release 应用开发环境安装 基于目前官方master主支&#xff0c;最新文档版本3.2Release&#xff0c;更新应用开发环境安装文档。 一、安装IDE&#xff1a; 1.IDE安装的系统要求 2.IDE下载官网链接&#xff08;IDE下载链接&#xff09; …

【论文精读】Self-Attentive Assocative Memory,2020

目录 1 引言2 Outer product attention (OPA)3 Self-attentive Associative Memory (SAM)4 SAM-based Two-Memory Model (STM)4.1 M i M^i Mi写操作4.2 M r M^r Mr读操作4.3 M i M^i Mi读操作和 M r M^r Mr写操作过程4.4 用 M r M^r Mr实现item转移4.5 模型输出 o t o_t ot​…

C语言手撕顺序表

目录 一、概念 1、静态顺序表&#xff1a;使用定长数组存储元素。 2、动态顺序表&#xff1a;使用动态开辟的数组存储 二、接口实现 1、对顺序表的初始化 2、对数据的销毁 3、对数据的打印 4、检查是否需要扩容 5、尾插 6、头插 7、尾删 8、头删 9、在pos位置插入x …

数据集【NO.7】无人机航拍数据集——VisDrone2019数据集

写在前面&#xff1a;数据集对应应用场景&#xff0c;不同的应用场景有不同的检测难点以及对应改进方法&#xff0c;本系列整理汇总领域内的数据集&#xff0c;方便大家下载数据集&#xff0c;若无法下载可关注后私信领取。关注免费领取整理好的数据集资料&#xff01;本文数据…

【前端知识】React 基础巩固(四十)——Navigate导航

React 基础巩固(四十)——Navigate导航 一、Navigate的基本使用 新建Login页面&#xff0c;在Login中引入Navigate&#xff0c;实现点击登陆按钮跳转至/home路径下&#xff1a; import React, { PureComponent } from "react"; import { Navigate } from "reac…

MySQL数据库之JDBC编程(从认识到操作)

目录 前言 一.JDBC的认识 1.1JDBC的来源 1.2JDBC的概念 二.JDBC的导包 三.JDBC的四步操作 三.JDBC常用的类和方法 3.1常用类 3.2常见方法 &#x1f381;个人主页&#xff1a;tq02的博客_CSDN博客-C语言,Java,Java数据结构领域博主 &#x1f3a5; 本文由 tq02 原创&…

芯片制造详解.光刻技术与基本流程.学习笔记(四)

本篇文章是看了以下视频后的笔记提炼&#xff0c;欢迎各位观看原视频&#xff0c;这里附上地址 芯片制造详解04&#xff1a;光刻技术与基本流程&#xff5c;国产之路不容易 芯片制造详解.光刻技术与基本流程.学习笔记 四 一、引子二、光刻(1).光掩膜(2).光刻机(3).光刻胶(4).挖…

matplotlib实现动态显示图片

plt.ion()打开交互开关 plt.ioff()关闭交互开关 plt.pause(0.1)暂停0.1秒 plt.clf()#清除当前的Figure图像 plt.cla()#清除当前的Axex图像 import matplotlib.pyplot as plt import numpy as np import time from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg…