zoukankan      html  css  js  c++  java
  • Gateway Ribbon Nacos的那点事

    前言

    Gateway  Ribbon  Nacos分别属于不同组织开发的框架,但是能共同服务于微服务框架中,它们是如何配合的呢

    Gateway

    1.在自动配置类GatewayDiscoveryClientAutoConfiguration中有一段代码

        @Bean
            @ConditionalOnProperty(name = "spring.cloud.gateway.discovery.locator.enabled")
            public DiscoveryClientRouteDefinitionLocator discoveryClientRouteDefinitionLocator(
                    ReactiveDiscoveryClient discoveryClient,
                    DiscoveryLocatorProperties properties) {
                return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties);
            }

    当spring.cloud.gateway.discovery.locator.enabled为true,这段代码会从ReactiveDiscoveryClient的实现类中获取服务名称列表封装成路由定义,而Nacos中有对应的实现类为NacosReactiveDiscoveryClient,这是它们的联系点之一

    2.当外部请求进来时,例如https://host/服务名/xxx,会经过一个全局过滤器,为LoadBalancerClientFilter,这个过滤器的作用是将url的服务名称改成真正服务所在的ip,里面有段关键代码是

    protected ServiceInstance choose(ServerWebExchange exchange) {
            return loadBalancer.choose(
                    ((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost());
        }

    上面起作用的是loadBalancer,对应的类型是LoadBalancerClient,这个是gateway框架定义的接口,真正实现类是Ribbon的RibbonLoadBalancerClient,这是gateway和ribbon的关联点

    Ribbon

    再来看看RibbonLoadBalancerClient的choose方法

    public ServiceInstance choose(String serviceId, Object hint) {
            Server server = getServer(getLoadBalancer(serviceId), hint);
            if (server == null) {
                return null;
            }
            return new RibbonServer(serviceId, server, isSecure(server, serviceId),
                    serverIntrospector(serviceId).getMetadata(server));
        }

    上面关键代码是getLoadBalancer(serviceId),如下

    protected ILoadBalancer getLoadBalancer(String serviceId) {
            return this.clientFactory.getLoadBalancer(serviceId);
        }

    这段代码目的是取出ILoadBalancer的实现类,其为ZoneAwareLoadBalancer,而它又是继承DynamicServerListLoadBalancer,构造函数如下

    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);
        }

    上面的构造函数会自动注入类型ServerList<T>的serverList,而nacos的NacosServerList实现了其接口,这里就是ribbon和nacos的连接点.

    nacos

    再来看看nacos什么时候提供了NacosServerList,看下面配置类

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnRibbonNacos
    public class NacosRibbonClientConfiguration {
    
        @Autowired
        private PropertiesFactory propertiesFactory;
    
        @Bean
        @ConditionalOnMissingBean
        public ServerList<?> ribbonServerList(IClientConfig config,
                NacosDiscoveryProperties nacosDiscoveryProperties) {
            if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
                ServerList serverList = this.propertiesFactory.get(ServerList.class, config,
                        config.getClientName());
                return serverList;
            }
            NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
            serverList.initWithNiwsConfig(config);
            return serverList;
        }
    
        @Bean
        @ConditionalOnMissingBean
        public NacosServerIntrospector nacosServerIntrospector() {
            return new NacosServerIntrospector();
        }
    
    }

    在此配置类中,nacos提供了NacosServerList.另外,NacosServerList里面有方法getServers

    private List<NacosServer> getServers() {
            try {
                String group = discoveryProperties.getGroup();
                List<Instance> instances = discoveryProperties.namingServiceInstance()
                        .selectInstances(serviceId, group, true);
                return instancesToServerList(instances);
            }
            catch (Exception e) {
                throw new IllegalStateException(
                        "Can not get service instances from nacos, serviceId=" + serviceId,
                        e);
            }
        }

    selectInstances方法主要逻辑是向nacosserver发起http请求,获取服务的地址列表.

  • 相关阅读:
    myeclipse8.6注册码
    hibernate用注解(annotation)配置sequence
    MyEclipse注释配置
    JAVA 单选样式编写
    .Net中的几种异步模式
    C#并发编程——并发编程技术
    《CLR via C#》之线程处理——任务调度器
    《CLR via C#》之线程处理——线程池与任务
    《CLR via C#》之线程处理——协作式取消和超时
    《CLR via C#》之线程处理——线程基础
  • 原文地址:https://www.cnblogs.com/caizl/p/13973183.html
Copyright © 2011-2022 走看看