前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊skywalking的cluster-nacos-plugin

聊聊skywalking的cluster-nacos-plugin

作者头像
code4it
发布2020-03-31 11:11:30
3700
发布2020-03-31 11:11:30
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下skywalking的cluster-nacos-plugin

ClusterModuleNacosConfig

skywalking-6.6.0/oap-server/server-cluster-plugin/cluster-nacos-plugin/src/main/java/org/apache/skywalking/oap/server/cluster/plugin/nacos/ClusterModuleNacosConfig.java

代码语言:javascript
复制
public class ClusterModuleNacosConfig extends ModuleConfig {
    @Setter @Getter private String serviceName;
    @Setter @Getter private String hostPort;
    @Setter @Getter private String namespace = "public";
}
  • ClusterModuleNacosConfig定义了serviceName、hostPort、namespace属性

ClusterModuleNacosProvider

skywalking-6.6.0/oap-server/server-cluster-plugin/cluster-nacos-plugin/src/main/java/org/apache/skywalking/oap/server/cluster/plugin/nacos/ClusterModuleNacosProvider.java

代码语言:javascript
复制
public class ClusterModuleNacosProvider extends ModuleProvider {

    private final ClusterModuleNacosConfig config;
    private NamingService namingService;

    public ClusterModuleNacosProvider() {
        super();
        this.config = new ClusterModuleNacosConfig();
    }

    @Override
    public String name() {
        return "nacos";
    }

    @Override
    public Class<? extends ModuleDefine> module() {
        return ClusterModule.class;
    }

    @Override
    public ModuleConfig createConfigBeanIfAbsent() {
        return config;
    }

    @Override
    public void prepare() throws ServiceNotProvidedException, ModuleStartException {
        try {
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, config.getHostPort());
            properties.put(PropertyKeyConst.NAMESPACE, config.getNamespace());
            namingService = NamingFactory.createNamingService(properties);
        } catch (Exception e) {
            throw new ModuleStartException(e.getMessage(), e);
        }
        NacosCoordinator coordinator = new NacosCoordinator(namingService, config);
        this.registerServiceImplementation(ClusterRegister.class, coordinator);
        this.registerServiceImplementation(ClusterNodesQuery.class, coordinator);
    }

    @Override
    public void start() throws ServiceNotProvidedException {

    }

    @Override
    public void notifyAfterCompleted() throws ServiceNotProvidedException {

    }

    @Override
    public String[] requiredModules() {
        return new String[]{CoreModule.NAME};
    }
}
  • ClusterModuleNacosProvider继承了ModuleProvider,其prepare方法创建NamingService及NacosCoordinator,然后将NacosCoordinator注册为ClusterRegister及ClusterNodesQuery的实现

NacosCoordinator

skywalking-6.6.0/oap-server/server-cluster-plugin/cluster-nacos-plugin/src/main/java/org/apache/skywalking/oap/server/cluster/plugin/nacos/NacosCoordinator.java

代码语言:javascript
复制
public class NacosCoordinator implements ClusterRegister, ClusterNodesQuery {

    private final NamingService namingService;
    private final ClusterModuleNacosConfig config;
    private volatile Address selfAddress;

    public NacosCoordinator(NamingService namingService, ClusterModuleNacosConfig config) {
        this.namingService = namingService;
        this.config = config;
    }

    @Override
    public List<RemoteInstance> queryRemoteNodes() {
        List<RemoteInstance> result = new ArrayList<>();
        try {
            List<Instance> instances = namingService.selectInstances(config.getServiceName(), true);
            if (CollectionUtils.isNotEmpty(instances)) {
                instances.forEach(instance -> {
                    Address address = new Address(instance.getIp(), instance.getPort(), false);
                    if (address.equals(selfAddress)) {
                        address.setSelf(true);
                    }
                    result.add(new RemoteInstance(address));
                });
            }
        } catch (NacosException e) {
            throw new ServiceQueryException(e.getErrMsg());
        }
        return result;
    }

    @Override
    public void registerRemote(RemoteInstance remoteInstance) throws ServiceRegisterException {
        String host = remoteInstance.getAddress().getHost();
        int port = remoteInstance.getAddress().getPort();
        try {
            namingService.registerInstance(config.getServiceName(), host, port);
        } catch (Exception e) {
            throw new ServiceRegisterException(e.getMessage());
        }
        this.selfAddress = remoteInstance.getAddress();
        TelemetryRelatedContext.INSTANCE.setId(selfAddress.toString());
    }
}
  • NacosCoordinator实现了ClusterRegister、ClusterNodesQuery接口,其queryRemoteNodes方法通过namingService.selectInstances(config.getServiceName(), true)获取instances;其registerRemote方法则通过namingService.registerInstance(config.getServiceName(), host, port)进行注册

小结

ClusterModuleNacosProvider继承了ModuleProvider,其prepare方法创建NamingService及NacosCoordinator,然后将NacosCoordinator注册为ClusterRegister及ClusterNodesQuery的实现

doc

  • ClusterModuleNacosProvider
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ClusterModuleNacosConfig
  • ClusterModuleNacosProvider
  • NacosCoordinator
  • 小结
  • doc
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档