栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud Nacos 集成Ribbon

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud Nacos 集成Ribbon

1、注册
@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();
   }

}

在这里注册了ServerList,和NacosServerIntrospector。其中,用NacosServerList来覆盖了ribbonServerList的默认实现,在ribbon的DynamicServerListLoadBalancer中提供服务。

在com.netflix.loadbalancer.DynamicServerListLoadBalancer#updateListOfServers方法中有一个servers = serverListImpl.getUpdatedListOfServers(),这里替换成了NacosServerList的方法,用来获取服务列表。

2、更新服务列表
//com.alibaba.cloud.nacos.ribbon.NacosServerList#getUpdatedListOfServers
public List getUpdatedListOfServers() {
   return getServers();
}
//com.alibaba.cloud.nacos.ribbon.NacosServerList#getServers
private List getServers() {
   try {
       //获取group
      String group = discoveryProperties.getGroup();
       //掉用NamingService获取服务列表
      List 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);
   }
}
public NamingService namingServiceInstance() {
   return nacosServiceManager.getNamingService(this.getNacosProperties());
}
public List selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException {
    return selectInstances(serviceName, groupName, healthy, true);
}
public List selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe)
        throws NacosException {
    return selectInstances(serviceName, groupName, new ArrayList(), healthy, subscribe);
}
public List selectInstances(String serviceName, String groupName, List clusters, boolean healthy,
        boolean subscribe) throws NacosException {
    
    ServiceInfo serviceInfo;
    String clusterString = StringUtils.join(clusters, ",");
    if (subscribe) {
        //直接获取服务列表
        serviceInfo = serviceInfoHolder.getServiceInfo(serviceName, groupName, clusterString);
        if (null == serviceInfo) {
            //获取服务信息,参见上一篇
            serviceInfo = clientProxy.subscribe(serviceName, groupName, clusterString);
        }
    } else {
        //获取服务信息,参见上一篇
        serviceInfo = clientProxy.queryInstancesOfService(serviceName, groupName, clusterString, 0, false);
    }
    //过滤
    return selectInstances(serviceInfo, healthy);
}
public ServiceInfo getServiceInfo(final String serviceName, final String groupName, final String clusters) {
    NAMING_LOGGER.debug("failover-mode: " + failoverReactor.isFailoverSwitch());
    String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
    String key = ServiceInfo.getKey(groupedServiceName, clusters);
    if (failoverReactor.isFailoverSwitch()) {
        return failoverReactor.getService(key);
    }
    return serviceInfoMap.get(key);
}

过滤服务列表,将不可用的服务移除

private List selectInstances(ServiceInfo serviceInfo, boolean healthy) {
    List list;
    if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) {
        return new ArrayList();
    }
    
    Iterator iterator = list.iterator();
    while (iterator.hasNext()) {
        Instance instance = iterator.next();
        if (healthy != instance.isHealthy() || !instance.isEnabled() || instance.getWeight() <= 0) {
            iterator.remove();
        }
    }
    
    return list;
}

将服务包装成NacosServer返回

NacosServer继承了Server

private List instancesToServerList(List instances) {
   List result = new ArrayList<>();
   if (CollectionUtils.isEmpty(instances)) {
      return result;
   }
   for (Instance instance : instances) {
      result.add(new NacosServer(instance));
   }

   return result;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号