Search in sources :

Example 1 with AddressWithWeight

use of rpc.turbo.config.AddressWithWeight in project turbo-rpc by hank-whu.

the class ZooKeeperRegister method register.

@Override
public void register(String group, String app, Protocol protocol, HostPort serverAddress, int serverWeight) {
    Objects.requireNonNull(client, "call init first");
    final String path = "/turbo/" + group + "/" + app + "/" + protocol + "/" + HexUtils.toHex(serverAddress.toString().getBytes(StandardCharsets.UTF_8));
    byte[] data = new AddressWithWeight(serverAddress, serverWeight).toBytes();
    try {
        if (client.checkExists().forPath(path) != null) {
            if (logger.isInfoEnabled()) {
                logger.info("删除zk已存在节点: " + path + ", " + serverAddress);
            }
            client.delete().forPath(path);
        }
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("zk已存在节点删除失败, " + path + ", " + serverAddress, e);
        }
    }
    try {
        // 
        client.create().creatingParentsIfNeeded().withMode(// 
        CreateMode.EPHEMERAL).forPath(path, data);
        if (logger.isInfoEnabled()) {
            logger.info("zk注册成功, " + path + ", " + serverAddress + "@" + serverWeight);
        }
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("zk注册失败, " + path + ", " + serverAddress + "@" + serverWeight, e);
        }
    }
    if (!watcherMap.containsKey(path)) {
        addRegisterWatcher(group, app, protocol, serverAddress, serverWeight);
    }
}
Also used : AddressWithWeight(rpc.turbo.config.AddressWithWeight) IOException(java.io.IOException)

Example 2 with AddressWithWeight

use of rpc.turbo.config.AddressWithWeight in project turbo-rpc by hank-whu.

the class ZooKeeperDiscover method addListener.

@Override
public void addListener(String group, String app, Protocol protocol, final DiscoverListener listener) {
    Objects.requireNonNull(listener, "listener is null");
    Objects.requireNonNull(client, "call init first");
    final String path = "/turbo/" + group + "/" + app + "/" + protocol;
    final PathChildrenCache watcher = new PathChildrenCache(client, path, true);
    PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {

        private final ConcurrentMap<HostPort, Integer> serverWithWeight = new ConcurrentHashMap<>();

        private volatile boolean waitForInitializedEvent = true;

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            if (logger.isInfoEnabled()) {
                logger.info("zk监控列表发生变化, " + path + ", " + event.getType());
            }
            boolean isChanged = true;
            switch(event.getType()) {
                case INITIALIZED:
                    waitForInitializedEvent = false;
                    if (logger.isInfoEnabled()) {
                        logger.info("完成初始化: " + path);
                    }
                    break;
                case CHILD_ADDED:
                    {
                        AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
                        serverWithWeight.put(kv.address, kv.weight);
                        if (logger.isInfoEnabled()) {
                            logger.info("新增节点: " + kv);
                        }
                        break;
                    }
                case CHILD_REMOVED:
                    {
                        AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
                        serverWithWeight.remove(kv.address);
                        if (logger.isInfoEnabled()) {
                            logger.info("删除节点: " + kv);
                        }
                        break;
                    }
                case CHILD_UPDATED:
                    {
                        AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
                        serverWithWeight.put(kv.address, kv.weight);
                        if (logger.isInfoEnabled()) {
                            logger.info("更新节点: " + kv);
                        }
                        break;
                    }
                default:
                    isChanged = false;
                    if (logger.isInfoEnabled()) {
                        logger.info("忽略, " + path + ", " + event.getType());
                    }
            }
            if (!waitForInitializedEvent && isChanged) {
                try {
                    listener.onChange(serverWithWeight);
                } catch (Throwable t) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Discover监听处理失败", t);
                    }
                }
            }
        }
    };
    watcher.getListenable().addListener(pathChildrenCacheListener);
    try {
        watcher.start(StartMode.POST_INITIALIZED_EVENT);
        watchers.add(watcher);
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("zk监听失败, " + path, e);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ConcurrentMap(java.util.concurrent.ConcurrentMap) AddressWithWeight(rpc.turbo.config.AddressWithWeight) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 AddressWithWeight (rpc.turbo.config.AddressWithWeight)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)1 PathChildrenCacheEvent (org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent)1 PathChildrenCacheListener (org.apache.curator.framework.recipes.cache.PathChildrenCacheListener)1