use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class NacosRegistry method unRegister.
@Override
public void unRegister(ProviderConfig config) {
String appName = config.getAppName();
if (!registryConfig.isRegister()) {
// registry ignored
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
}
return;
}
// unregister publisher
if (config.isRegister()) {
try {
List<Instance> instances = providerInstances.remove(config);
if (CommonUtils.isNotEmpty(instances)) {
for (Instance instance : instances) {
String serviceName = instance.getServiceName();
namingService.deregisterInstance(serviceName, instance);
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_UNPUB, serviceName, instances.size()));
}
}
}
} catch (Exception e) {
if (!RpcRunningState.isShuttingDown()) {
if (e instanceof SofaRpcRuntimeException) {
throw (SofaRpcRuntimeException) e;
} else {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNREG_PROVIDER, EXT_NAME), e);
}
}
}
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class NacosRegistry method subscribe.
@Override
public List<ProviderGroup> subscribe(final ConsumerConfig config) {
String appName = config.getAppName();
if (!registryConfig.isSubscribe()) {
// registry ignored
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
}
return null;
}
if (config.isSubscribe()) {
String serviceName = NacosRegistryHelper.buildServiceName(config, config.getProtocol());
if (LOGGER.isInfoEnabled()) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_SUB, serviceName));
}
try {
if (providerObserver == null) {
providerObserver = new NacosRegistryProviderObserver();
}
ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
providerObserver.addProviderListener(config, providerInfoListener);
EventListener eventListener = new EventListener() {
@Override
public void onEvent(Event event) {
if (event instanceof NamingEvent) {
NamingEvent namingEvent = (NamingEvent) event;
List<Instance> instances = namingEvent.getInstances();
// avoid npe
if (null == instances) {
instances = new ArrayList<Instance>();
}
instances.removeIf(i -> !i.isEnabled());
providerObserver.updateProviders(config, instances);
}
}
};
namingService.subscribe(serviceName, defaultCluster, eventListener);
consumerListeners.put(config, eventListener);
List<Instance> allInstances = namingService.getAllInstances(serviceName, defaultCluster);
List<ProviderInfo> providerInfos = NacosRegistryHelper.convertInstancesToProviders(allInstances);
List<ProviderInfo> matchProviders = RegistryUtils.matchProviderInfos(config, providerInfos);
return Collections.singletonList(new ProviderGroup().addAll(matchProviders));
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER, EXT_NAME), e);
}
}
return null;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class DefaultConsumerBootstrap method refer.
@Override
public T refer() {
if (proxyIns != null) {
return proxyIns;
}
synchronized (this) {
if (proxyIns != null) {
return proxyIns;
}
String key = consumerConfig.buildKey();
String appName = consumerConfig.getAppName();
// 检查参数
checkParameters();
// 提前检查接口类
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, "Refer consumer config : {} with bean id {}", key, consumerConfig.getId());
}
// 注意同一interface,同一tags,同一protocol情况
// 计数器
AtomicInteger cnt = REFERRED_KEYS.get(key);
if (cnt == null) {
// 没有发布过
cnt = CommonUtils.putToConcurrentMap(REFERRED_KEYS, key, new AtomicInteger(0));
}
int c = cnt.incrementAndGet();
int maxProxyCount = consumerConfig.getRepeatedReferLimit();
if (maxProxyCount > 0) {
if (c > maxProxyCount) {
cnt.decrementAndGet();
// 超过最大数量,直接抛出异常
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_DUPLICATE_CONSUMER_CONFIG, key, maxProxyCount));
} else if (c > 1) {
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, "Duplicate consumer config with key {} has been referred!" + " Maybe it's wrong config, please check it." + " Ignore this if you did that on purpose!", key);
}
}
}
try {
// build cluster
cluster = ClusterFactory.getCluster(this);
// build listeners
consumerConfig.setConfigListener(buildConfigListener(this));
consumerConfig.setProviderInfoListener(buildProviderInfoListener(this));
// init cluster
cluster.init();
// 构造Invoker对象(执行链)
proxyInvoker = buildClientProxyInvoker(this);
// 创建代理类
proxyIns = (T) ProxyFactory.buildProxy(consumerConfig.getProxy(), consumerConfig.getProxyClass(), proxyInvoker);
// 动态配置
final String dynamicAlias = consumerConfig.getParameter(DynamicConfigKeys.DYNAMIC_ALIAS);
if (StringUtils.isNotBlank(dynamicAlias)) {
final DynamicConfigManager dynamicManager = DynamicConfigManagerFactory.getDynamicManager(consumerConfig.getAppName(), dynamicAlias);
dynamicManager.initServiceConfiguration(consumerConfig.getInterfaceId());
}
} catch (Exception e) {
if (cluster != null) {
cluster.destroy();
cluster = null;
}
consumerConfig.setConfigListener(null);
consumerConfig.setProviderInfoListener(null);
// 发布失败不计数
cnt.decrementAndGet();
if (e instanceof SofaRpcRuntimeException) {
throw (SofaRpcRuntimeException) e;
} else {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_BUILD_CONSUMER_PROXY), e);
}
}
if (consumerConfig.getOnAvailable() != null && cluster != null) {
// 状态变化通知监听器
cluster.checkStateChange(false);
}
RpcRuntimeContext.cacheConsumerConfig(this);
return proxyIns;
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class DefaultConsumerBootstrap method subscribeFromRegistries.
/**
* Subscribe provider list from all registries, the providers will be merged.
*
* @return Provider group list
*/
protected List<ProviderGroup> subscribeFromRegistries() {
List<ProviderGroup> result = new ArrayList<ProviderGroup>();
List<RegistryConfig> registryConfigs = consumerConfig.getRegistry();
if (CommonUtils.isEmpty(registryConfigs)) {
return result;
}
// 是否等待结果
int addressWaitTime = consumerConfig.getAddressWait();
int maxAddressWaitTime = SofaConfigs.getIntegerValue(consumerConfig.getAppName(), SofaOptions.CONFIG_MAX_ADDRESS_WAIT_TIME, SofaOptions.MAX_ADDRESS_WAIT_TIME);
addressWaitTime = addressWaitTime < 0 ? maxAddressWaitTime : Math.min(addressWaitTime, maxAddressWaitTime);
ProviderInfoListener listener = consumerConfig.getProviderInfoListener();
respondRegistries = addressWaitTime == 0 ? null : new CountDownLatch(registryConfigs.size());
// 从注册中心订阅 {groupName: ProviderGroup}
Map<String, ProviderGroup> tmpProviderInfoList = new HashMap<String, ProviderGroup>();
for (RegistryConfig registryConfig : registryConfigs) {
Registry registry = RegistryFactory.getRegistry(registryConfig);
registry.init();
registry.start();
try {
List<ProviderGroup> current;
try {
if (respondRegistries != null) {
consumerConfig.setProviderInfoListener(new WrapperClusterProviderInfoListener(listener, respondRegistries));
}
current = registry.subscribe(consumerConfig);
} finally {
if (respondRegistries != null) {
consumerConfig.setProviderInfoListener(listener);
}
}
if (current == null) {
// 未同步返回结果
continue;
} else {
if (respondRegistries != null) {
respondRegistries.countDown();
}
}
for (ProviderGroup group : current) {
// 当前注册中心的
String groupName = group.getName();
if (!group.isEmpty()) {
ProviderGroup oldGroup = tmpProviderInfoList.get(groupName);
if (oldGroup != null) {
oldGroup.addAll(group.getProviderInfos());
} else {
tmpProviderInfoList.put(groupName, group);
}
}
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
String appName = consumerConfig.getAppName();
if (LOGGER.isWarnEnabled(appName)) {
LOGGER.warnWithApp(appName, LogCodes.getLog(LogCodes.ERROR_SUBSCRIBE_FROM_REGISTRY, registryConfig.getId()), e);
}
}
}
if (respondRegistries != null) {
try {
respondRegistries.await(addressWaitTime, TimeUnit.MILLISECONDS);
} catch (Exception ignore) {
// NOPMD
}
}
return new ArrayList<ProviderGroup>(tmpProviderInfoList.values());
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class DefaultProviderBootstrap method doExport.
private void doExport() {
if (exported) {
return;
}
// 检查参数
checkParameters();
String appName = providerConfig.getAppName();
// key is the protocol of server,for concurrent safe
Map<String, Boolean> hasExportedInCurrent = new ConcurrentHashMap<String, Boolean>();
// 将处理器注册到server
List<ServerConfig> serverConfigs = providerConfig.getServer();
for (ServerConfig serverConfig : serverConfigs) {
String protocol = serverConfig.getProtocol();
String key = providerConfig.buildKey() + ":" + protocol;
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, "Export provider config : {} with bean id {}", key, providerConfig.getId());
}
// 注意同一interface,同一uniqueId,不同server情况
// 计数器
AtomicInteger cnt = EXPORTED_KEYS.get(key);
if (cnt == null) {
// 没有发布过
cnt = CommonUtils.putToConcurrentMap(EXPORTED_KEYS, key, new AtomicInteger(0));
}
int c = cnt.incrementAndGet();
hasExportedInCurrent.put(serverConfig.getProtocol(), true);
int maxProxyCount = providerConfig.getRepeatedExportLimit();
if (maxProxyCount > 0) {
if (c > maxProxyCount) {
decrementCounter(hasExportedInCurrent);
// 超过最大数量,直接抛出异常
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_DUPLICATE_PROVIDER_CONFIG, key, maxProxyCount));
} else if (c > 1) {
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.WARN_DUPLICATE_PROVIDER_CONFIG, key, c));
}
}
}
}
try {
// 构造请求调用器
providerProxyInvoker = new ProviderProxyInvoker(providerConfig);
preProcessProviderTarget(providerConfig, (ProviderProxyInvoker) providerProxyInvoker);
// 初始化注册中心
if (providerConfig.isRegister()) {
List<RegistryConfig> registryConfigs = providerConfig.getRegistry();
if (CommonUtils.isNotEmpty(registryConfigs)) {
for (RegistryConfig registryConfig : registryConfigs) {
// 提前初始化Registry
RegistryFactory.getRegistry(registryConfig);
}
}
}
// 将处理器注册到server
for (ServerConfig serverConfig : serverConfigs) {
try {
Server server = serverConfig.buildIfAbsent();
// 注册请求调用器
server.registerProcessor(providerConfig, providerProxyInvoker);
if (serverConfig.isAutoStart()) {
server.start();
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.ERROR_REGISTER_PROCESSOR_TO_SERVER, serverConfig.getId()), e);
}
}
// 注册到注册中心
providerConfig.setConfigListener(new ProviderAttributeListener());
register();
} catch (Exception e) {
decrementCounter(hasExportedInCurrent);
if (e instanceof SofaRpcRuntimeException) {
throw e;
}
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_BUILD_PROVIDER_PROXY), e);
}
// 记录一些缓存数据
RpcRuntimeContext.cacheProviderConfig(this);
exported = true;
}
Aggregations