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);
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
}
}
}
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);
}
}
}
Aggregations