转载

聊聊NacosRibbonClientConfiguration

本文主要研究一下NacosRibbonClientConfiguration

NacosRibbonClientConfiguration

spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfiguration.java

@Configuration
@ConditionalOnRibbonNacos
public class NacosRibbonClientConfiguration {

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

NacosServerList

spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java

public class NacosServerList extends AbstractServerList<NacosServer> {

    private NacosDiscoveryProperties discoveryProperties;

    private String serviceId;

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

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

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

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

    private List<NacosServer> instancesToServerList(List<Instance> instances) {
        List<NacosServer> 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();
    }
}
  • NacosServerList继承了com.netflix.loadbalancer.AbstractServerList,其getInitialListOfServers及getUpdatedListOfServers方法都是调用了getServers;getServers方法则是通过NamingService.selectInstances来获取服务实例信息

NacosServer

spring-cloud-alibaba-0.9.0.RELEASE/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServer.java

public class NacosServer extends Server {

    private final MetaInfo metaInfo;
    private final Instance instance;
    private final Map<String, String> metadata;

    public NacosServer(final Instance instance) {
        super(instance.getIp(), instance.getPort());
        this.instance = instance;
        this.metaInfo = new MetaInfo() {
            @Override
            public String getAppName() {
                return instance.getServiceName();
            }

            @Override
            public String getServerGroup() {
                return null;
            }

            @Override
            public String getServiceIdForDiscovery() {
                return null;
            }

            @Override
            public String getInstanceId() {
                return instance.getInstanceId();
            }
        };
        this.metadata = instance.getMetadata();
    }

    @Override
    public MetaInfo getMetaInfo() {
        return metaInfo;
    }

    public Instance getInstance() {
        return instance;
    }

    public Map<String, String> getMetadata() {
        return metadata;
    }
}
  • NacosServer继承了com.netflix.loadbalancer.Server;它主要有metaInfo、instance、metadata三个属性

小结

NacosRibbonClientConfiguration会创建NacosServerList;NacosServerList继承了com.netflix.loadbalancer.AbstractServerList,其getInitialListOfServers及getUpdatedListOfServers方法都是调用了getServers;getServers方法则是通过NamingService.selectInstances来获取服务实例信息

doc

  • NacosRibbonClientConfiguration
原文  https://segmentfault.com/a/1190000019924705
正文到此结束
Loading...