Nacos同命名空间下不同组跨组相互调用基于Ribbon

Nacos同命名空间下不同组跨组相互调用基于Ribbon
Nacos同命名空间下不同组跨组相互调用基于Ribbon_第1张图片
yml文件
主要类Nacos同命名空间下不同组跨组相互调用基于Ribbon_第2张图片
消费者在INFO_GROUP
调用在TEST_GROUP组中的服务

@Slf4j
public class NacosRule extends AbstractLoadBalancerRule {
    @Autowired
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    @Override
    public Server choose(Object key) {
        try {
            String clusterName = this.nacosDiscoveryProperties.getClusterName();
            DynamicServerListLoadBalancer loadBalancer = (DynamicServerListLoadBalancer) getLoadBalancer();
            String name = loadBalancer.getName();

            NamingService namingService = this.nacosDiscoveryProperties.namingServiceInstance();

//            List instances = namingService.selectInstances(name, true);
            String group = nacosDiscoveryProperties.getGroup();
            log.info("当前group:{}", group);
            //获取指定组TEST_GROUP的信息
            List instances = namingService.selectInstances(name, "TEST_GROUP", true);
            log.info("choose()组:{}", instances);
            if (CollectionUtils.isEmpty(instances)) {
                log.warn("{}服务当前无任何实例", name);
                return null;
            }

            List instancesToChoose = instances;
            if (StringUtils.isNotBlank(clusterName)) {
                List sameClusterInstances = instances.stream()
                        .filter(instance -> Objects.equals(clusterName, instance.getClusterName()))
                        .collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(sameClusterInstances)) {
                    instancesToChoose = sameClusterInstances;
                } else {
                    log.warn("发生跨集群的调用,name = {}, clusterName = {}, instance = {}", name, clusterName, instances);
                }
            }

            Instance instance = ExtendBalancer.getHostByRandomWeight2(instancesToChoose);

            return new NacosServer(instance);
        } catch (Exception e) {
            log.warn("NacosRule发生异常", e);
            return null;
        }
    }

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {
    }
}

@Slf4j
public class NacosRibbonServerList extends AbstractServerList {

    private NacosDiscoveryProperties discoveryProperties;

    private String serviceId;

    public NacosRibbonServerList(NacosDiscoveryProperties discoveryProperties) {
        this.discoveryProperties = discoveryProperties;
    }

    @Override
    public List getInitialListOfServers() {
        return getServers();
    }

    @Override
    public List getUpdatedListOfServers() {
        return getServers();
    }

    private List getServers() {
        try {
//            Instance instance = discoveryProperties.namingServiceInstance().selectOneHealthyInstance(serviceId, true);
            String group = discoveryProperties.getGroup();
            List instance = discoveryProperties.namingServiceInstance().selectInstances(serviceId, "TEST_GROUP", true);

            log.debug("选择的instance = {}", instance);
            return instancesToServerList(
                    Lists.newArrayList(instance)
            );
        } catch (Exception e) {
            throw new IllegalStateException(
                    "Can not get service instances from nacos, serviceId=" + serviceId,
                    e);
        }
    }

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

    public String getServiceId() {
        return serviceId;
    }


    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {
        this.serviceId = iClientConfig.getClientName();
    }
}

依赖版本参考

   
            com.alibaba
            dubbo-registry-nacos
            0.0.1
        
        
        
            com.alibaba.spring
            spring-context-support
            1.0.2
        

			
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.1.RELEASE
                pom
                import
            
             
                org.springframework.boot
                spring-boot-dependencies
                2.2.2.RELEASE
                pom
                import
            

Nacos同命名空间下不同组跨组相互调用基于Ribbon_第3张图片参考周立老师的giteedemo
总结:
目前只使用Ribbon方式粗略实现还需要细雕琢,@Feign方式设计不兼容暂时没找到办法
https://gitee.com/itmuch/spring-cloud-study/blob/master/2019-Spring-Cloud-Alibaba/microservice-consumer-movie-ribbon-rule-with-nacos-2/src/main/resources/application.yml

你可能感兴趣的:(nacos)