Search in sources :

Example 56 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ZookeeperRegistry method unRegister.

@Override
public void unRegister(ProviderConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isRegister()) {
        // 注册中心不注册
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return;
    }
    // 反注册服务端节点
    if (config.isRegister()) {
        try {
            List<String> urls = providerUrls.remove(config);
            if (CommonUtils.isNotEmpty(urls)) {
                String providerPath = buildProviderPath(rootPath, config);
                for (String url : urls) {
                    url = URLEncoder.encode(url, "UTF-8");
                    getAndCheckZkClient().delete().forPath(providerPath + CONTEXT_SEP + url);
                }
                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_UNPUB, providerPath, "1"));
                }
            }
        } catch (Exception e) {
            if (!RpcRunningState.isShuttingDown()) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNREG_PROVIDER, EXT_NAME), e);
            }
        }
    }
    // 反订阅配置节点
    if (config.isSubscribe()) {
        try {
            if (null != configObserver) {
                configObserver.removeConfigListener(config);
            }
            if (null != overrideObserver) {
                overrideObserver.removeConfigListener(config);
            }
        } catch (Exception e) {
            if (!RpcRunningState.isShuttingDown()) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUB_PROVIDER_CONFIG, EXT_NAME), e);
            }
        }
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 57 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ZookeeperRegistry method registerProviderUrls.

/**
 * 注册 服务信息
 * @param config
 * @return
 * @throws Exception
 */
protected void registerProviderUrls(ProviderConfig config) {
    String appName = config.getAppName();
    // 注册服务端节点
    try {
        // 避免重复计算
        List<String> urls;
        if (providerUrls.containsKey(config)) {
            urls = providerUrls.get(config);
        } else {
            urls = ZookeeperRegistryHelper.convertProviderToUrls(config);
            providerUrls.put(config, urls);
        }
        if (CommonUtils.isNotEmpty(urls)) {
            String providerPath = buildProviderPath(rootPath, config);
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_START, providerPath));
            }
            for (String url : urls) {
                url = URLEncoder.encode(url, "UTF-8");
                String providerUrl = providerPath + CONTEXT_SEP + url;
                try {
                    getAndCheckZkClient().create().creatingParentContainersIfNeeded().withMode(// 是否永久节点
                    ephemeralNode ? CreateMode.EPHEMERAL : CreateMode.PERSISTENT).forPath(providerUrl, // 是否默认上下线
                    config.isDynamic() ? PROVIDER_ONLINE : PROVIDER_OFFLINE);
                    if (LOGGER.isInfoEnabled(appName)) {
                        LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB, providerUrl));
                    }
                } catch (KeeperException.NodeExistsException nodeExistsException) {
                    if (LOGGER.isWarnEnabled(appName)) {
                        LOGGER.warnWithApp(appName, "provider has exists in zookeeper, provider=" + providerUrl);
                    }
                }
            }
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_OVER, providerPath));
            }
        }
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_REG_PROVIDER, "zookeeperRegistry", config.buildKey()), e);
    }
    if (EventBus.isEnable(ProviderPubEvent.class)) {
        ProviderPubEvent event = new ProviderPubEvent(config);
        EventBus.post(event);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderPubEvent(com.alipay.sofa.rpc.event.ProviderPubEvent) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 58 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ZookeeperRegistry method subscribeConfig.

/**
 * 订阅接口级配置
 *
 * @param config   provider/consumer config
 * @param listener config listener
 */
protected void subscribeConfig(final AbstractInterfaceConfig config, ConfigListener listener) {
    try {
        if (configObserver == null) {
            // 初始化
            configObserver = new ZookeeperConfigObserver();
        }
        configObserver.addConfigListener(config, listener);
        final String configPath = buildConfigPath(rootPath, config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, configPath, true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                if (LOGGER.isDebugEnabled(config.getAppName())) {
                    LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
                }
                switch(event.getType()) {
                    case // 新增接口级配置
                    CHILD_ADDED:
                        configObserver.addConfig(config, configPath, event.getData());
                        break;
                    case // 删除接口级配置
                    CHILD_REMOVED:
                        configObserver.removeConfig(config, configPath, event.getData());
                        break;
                    case // 更新接口级配置
                    CHILD_UPDATED:
                        configObserver.updateConfig(config, configPath, event.getData());
                        break;
                    default:
                        break;
                }
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_CONFIG_CACHE.put(configPath, pathChildrenCache);
        configObserver.updateConfigAll(config, configPath, pathChildrenCache.getCurrentData());
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER_CONFIG, EXT_NAME), 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) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 59 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ZookeeperRegistry method unSubscribe.

@Override
public void unSubscribe(ConsumerConfig config) {
    // 反注册服务端节点
    if (config.isRegister()) {
        try {
            String url = consumerUrls.remove(config);
            if (url != null) {
                String consumerPath = buildConsumerPath(rootPath, config);
                url = URLEncoder.encode(url, "UTF-8");
                getAndCheckZkClient().delete().forPath(consumerPath + CONTEXT_SEP + url);
            }
        } catch (Exception e) {
            if (!RpcRunningState.isShuttingDown()) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNREG_CONSUMER_CONFIG, EXT_NAME), e);
            }
        }
    }
    // 反订阅配置节点
    if (config.isSubscribe()) {
        try {
            providerObserver.removeProviderListener(config);
        } catch (Exception e) {
            if (!RpcRunningState.isShuttingDown()) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUB_PROVIDER_CONFIG, EXT_NAME), e);
            }
        }
        try {
            configObserver.removeConfigListener(config);
        } catch (Exception e) {
            if (!RpcRunningState.isShuttingDown()) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUB_CONSUMER_CONFIG, EXT_NAME), e);
            }
        }
        PathChildrenCache childrenCache = INTERFACE_PROVIDER_CACHE.remove(config);
        if (childrenCache != null) {
            try {
                childrenCache.close();
            } catch (Exception e) {
                if (!RpcRunningState.isShuttingDown()) {
                    throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUB_CONSUMER_CONFIG, EXT_NAME), e);
                }
            }
        }
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 60 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class AbstractHttpServer method start.

@Override
public void start() {
    if (started) {
        return;
    }
    synchronized (this) {
        if (started) {
            return;
        }
        try {
            // 启动线程池
            this.bizThreadPool = initThreadPool(serverConfig);
            this.serverHandler.setBizThreadPool(bizThreadPool);
            serverTransport = ServerTransportFactory.getServerTransport(serverTransportConfig);
            started = serverTransport.start();
            if (started) {
                if (EventBus.isEnable(ServerStartedEvent.class)) {
                    EventBus.post(new ServerStartedEvent(serverConfig, bizThreadPool));
                }
            }
        } catch (SofaRpcRuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_START_SERVER, "HTTP/2"), e);
        }
    }
}
Also used : ServerStartedEvent(com.alipay.sofa.rpc.event.ServerStartedEvent) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Aggregations

SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)64 Method (java.lang.reflect.Method)10 KeeperException (org.apache.zookeeper.KeeperException)7 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)6 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)6 NacosException (com.alibaba.nacos.api.exception.NacosException)5 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)4 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)4 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)3 ProviderPubEvent (com.alipay.sofa.rpc.event.ProviderPubEvent)3 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)3 SslContext (io.netty.handler.ssl.SslContext)3 File (java.io.File)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Test (org.junit.Test)3 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2