use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ConnectionHolderFactory method getConnectionHolder.
/**
* 根据配置得到连接管理器
*
* @param consumerBootstrap 服务消费者配置
* @return ConnectionHolder
*/
public static ConnectionHolder getConnectionHolder(ConsumerBootstrap consumerBootstrap) {
String connectionHolder = null;
try {
connectionHolder = consumerBootstrap.getConsumerConfig().getConnectionHolder();
ExtensionClass<ConnectionHolder> ext = ExtensionLoaderFactory.getExtensionLoader(ConnectionHolder.class).getExtensionClass(connectionHolder);
if (ext == null) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_CONNECTION_HOLDER, connectionHolder));
}
return ext.getExtInstance(new Class[] { ConsumerBootstrap.class }, new Object[] { consumerBootstrap });
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_CONNECTION_HOLDER, connectionHolder), e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ZookeeperRegistry method subscribe.
@Override
public List<ProviderGroup> subscribe(final ConsumerConfig config) {
String appName = config.getAppName();
if (!registryConfig.isSubscribe()) {
// 注册中心不订阅
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
}
return null;
}
// 订阅如果有必要
subscribeConsumerUrls(config);
if (config.isSubscribe()) {
List<ProviderInfo> matchProviders;
// 订阅配置
if (!INTERFACE_CONFIG_CACHE.containsKey(buildConfigPath(rootPath, config))) {
// 订阅接口级配置
subscribeConfig(config, config.getConfigListener());
}
if (!INTERFACE_OVERRIDE_CACHE.containsKey(buildOverridePath(rootPath, config))) {
// 订阅IP级配置
subscribeOverride(config, config.getConfigListener());
}
// 订阅Providers节点
try {
if (providerObserver == null) {
// 初始化
providerObserver = new ZookeeperProviderObserver();
}
final String providerPath = buildProviderPath(rootPath, config);
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_SUB, providerPath));
}
PathChildrenCache pathChildrenCache = INTERFACE_PROVIDER_CACHE.get(config);
if (pathChildrenCache == null) {
// 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
providerObserver.addProviderListener(config, providerInfoListener);
// TODO 换成监听父节点变化(只是监听变化了,而不通知变化了什么,然后客户端自己来拉数据的)
pathChildrenCache = new PathChildrenCache(zkClient, providerPath, true);
final PathChildrenCache finalPathChildrenCache = pathChildrenCache;
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
if (LOGGER.isDebugEnabled(config.getAppName())) {
LOGGER.debugWithApp(config.getAppName(), "Receive zookeeper event: " + "type=[" + event.getType() + "]");
}
switch(event.getType()) {
case // 加了一个provider
CHILD_ADDED:
providerObserver.addProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
break;
case // 删了一个provider
CHILD_REMOVED:
providerObserver.removeProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
break;
case // 更新一个Provider
CHILD_UPDATED:
providerObserver.updateProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
break;
default:
break;
}
}
});
pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
INTERFACE_PROVIDER_CACHE.put(config, pathChildrenCache);
}
List<ProviderInfo> providerInfos = ZookeeperRegistryHelper.convertUrlsToProviders(providerPath, pathChildrenCache.getCurrentData());
matchProviders = ZookeeperRegistryHelper.matchProviderInfos(config, providerInfos);
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER, EXT_NAME), e);
}
if (EventBus.isEnable(ConsumerSubEvent.class)) {
ConsumerSubEvent event = new ConsumerSubEvent(config);
EventBus.post(event);
}
return Collections.singletonList(new ProviderGroup().addAll(matchProviders));
}
return null;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ZookeeperRegistry method subscribeOverride.
/**
* 订阅IP级配置(服务发布暂时不支持动态配置,暂时支持订阅ConsumerConfig参数设置)
*
* @param config consumer config
* @param listener config listener
*/
protected void subscribeOverride(final ConsumerConfig config, ConfigListener listener) {
try {
if (overrideObserver == null) {
// 初始化
overrideObserver = new ZookeeperOverrideObserver();
}
overrideObserver.addConfigListener(config, listener);
final String overridePath = buildOverridePath(rootPath, config);
final AbstractInterfaceConfig registerConfig = getRegisterConfig(config);
// 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, overridePath, 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 // 新增IP级配置
CHILD_ADDED:
overrideObserver.addConfig(config, overridePath, event.getData());
break;
case // 删除IP级配置
CHILD_REMOVED:
overrideObserver.removeConfig(config, overridePath, event.getData(), registerConfig);
break;
case // 更新IP级配置
CHILD_UPDATED:
overrideObserver.updateConfig(config, overridePath, event.getData());
break;
default:
break;
}
}
});
pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
INTERFACE_OVERRIDE_CACHE.put(overridePath, pathChildrenCache);
overrideObserver.updateConfigAll(config, overridePath, pathChildrenCache.getCurrentData());
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER_OVERRIDE, EXT_NAME), e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ZookeeperRegistry method init.
@Override
public synchronized void init() {
if (zkClient != null) {
return;
}
// xxx:2181,yyy:2181/path1/paht2
String addressInput = registryConfig.getAddress();
if (StringUtils.isEmpty(addressInput)) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_EMPTY_ADDRESS, EXT_NAME));
}
int idx = addressInput.indexOf(CONTEXT_SEP);
// IP地址
String address;
if (idx > 0) {
address = addressInput.substring(0, idx);
rootPath = addressInput.substring(idx);
if (!rootPath.endsWith(CONTEXT_SEP)) {
// 保证以"/"结尾
rootPath += CONTEXT_SEP;
}
} else {
address = addressInput;
rootPath = CONTEXT_SEP;
}
preferLocalFile = !CommonUtils.isFalse(registryConfig.getParameter(PARAM_PREFER_LOCAL_FILE));
ephemeralNode = !CommonUtils.isFalse(registryConfig.getParameter(PARAM_CREATE_EPHEMERAL));
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Init ZookeeperRegistry with address {}, root path is {}. preferLocalFile:{}, ephemeralNode:{}", address, rootPath, preferLocalFile, ephemeralNode);
}
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFrameworkFactory.Builder zkClientuilder = CuratorFrameworkFactory.builder().connectString(address).sessionTimeoutMs(registryConfig.getConnectTimeout() * 3).connectionTimeoutMs(registryConfig.getConnectTimeout()).canBeReadOnly(false).retryPolicy(retryPolicy).defaultData(null);
// 是否需要添加zk的认证信息
List<AuthInfo> authInfos = buildAuthInfo();
if (CommonUtils.isNotEmpty(authInfos)) {
zkClientuilder = zkClientuilder.aclProvider(getDefaultAclProvider()).authorization(authInfos);
}
zkClient = zkClientuilder.build();
zkClient.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("reconnect to zookeeper,recover provider and consumer data");
}
if (newState == ConnectionState.RECONNECTED) {
recoverRegistryData();
}
}
});
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ZookeeperRegistry method subscribeConsumerUrls.
/**
* 订阅
* @param config
*/
protected void subscribeConsumerUrls(ConsumerConfig config) {
// 注册Consumer节点
String url = null;
if (config.isRegister()) {
try {
String consumerPath = buildConsumerPath(rootPath, config);
if (consumerUrls.containsKey(config)) {
url = consumerUrls.get(config);
} else {
url = ZookeeperRegistryHelper.convertConsumerToUrl(config);
consumerUrls.put(config, url);
}
String encodeUrl = URLEncoder.encode(url, "UTF-8");
getAndCheckZkClient().create().creatingParentContainersIfNeeded().withMode(// Consumer临时节点
CreateMode.EPHEMERAL).forPath(consumerPath + CONTEXT_SEP + encodeUrl);
} catch (KeeperException.NodeExistsException nodeExistsException) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("consumer has exists in zookeeper, consumer=" + url);
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_REG_CONSUMER_CONFIG, EXT_NAME), e);
}
}
}
Aggregations