use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class NacosRegistry method register.
@Override
public void register(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()) {
// register server
try {
List<Instance> instances = NacosRegistryHelper.convertProviderToInstances(config);
if (CommonUtils.isNotEmpty(instances)) {
for (Instance instance : instances) {
String serviceName = instance.getServiceName();
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_START, serviceName));
}
namingService.registerInstance(serviceName, instance);
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_OVER, serviceName));
}
}
providerInstances.put(config, instances);
}
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_REG_PROVIDER, "NacosRegistry", config.buildKey()), e);
}
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class NacosRegistry method init.
@Override
public synchronized void init() {
if (namingService != null) {
return;
}
// xxx:8848,yyy:8848/namespace
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);
String namespace;
// IP地址
String address;
if (idx > 0) {
address = addressInput.substring(0, idx);
namespace = addressInput.substring(idx + 1);
// for host:port/ this scene
if (StringUtils.isBlank(namespace)) {
namespace = DEFAULT_NAMESPACE;
}
} else {
address = addressInput;
namespace = DEFAULT_NAMESPACE;
}
defaultCluster = Collections.singletonList(NacosRegistryHelper.DEFAULT_CLUSTER);
nacosConfig.put(PropertyKeyConst.SERVER_ADDR, address);
nacosConfig.put(PropertyKeyConst.NAMESPACE, namespace);
try {
namingService = NamingFactory.createNamingService(nacosConfig);
} catch (NacosException e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_INIT_NACOS_NAMING_SERVICE, address), e);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class NacosRegistry method unSubscribe.
@Override
public void unSubscribe(ConsumerConfig config) {
if (config.isSubscribe()) {
String serviceName = NacosRegistryHelper.buildServiceName(config, config.getProtocol());
try {
EventListener eventListener = consumerListeners.remove(config);
if (null != eventListener) {
namingService.unsubscribe(serviceName, defaultCluster, eventListener);
}
} catch (Exception e) {
if (!RpcRunningState.isShuttingDown()) {
if (e instanceof SofaRpcRuntimeException) {
throw (SofaRpcRuntimeException) e;
} else {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUB_LISTENER, EXT_NAME), e);
}
}
}
providerObserver.removeProviderListener(config);
}
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ConsulRegistry method buildCheck.
private NewService.Check buildCheck(String serverHost, int serverPort) {
NewService.Check check = new NewService.Check();
ConsulRegistryProperties.HealthCheckType healthCheckType = properties.getHealthCheckType();
if (healthCheckType == ConsulRegistryProperties.HealthCheckType.TTL) {
check.setTtl(properties.getHealthCheckTTL());
} else if (healthCheckType == ConsulRegistryProperties.HealthCheckType.TCP) {
String host = properties.getHealthCheckHost(serverHost);
int port = properties.getHealthCheckPort(serverPort);
check.setTcp(host + ":" + port);
check.setInterval(properties.getHealthCheckInterval());
check.setTimeout(properties.getHealthCheckTimeout());
} else {
String host = properties.getHealthCheckHost(serverHost);
int port = properties.getHealthCheckPort(serverPort);
String address;
try {
address = new URL(properties.getHealthCheckProtocol(), host, port, properties.getHealthCheckPath()).toString();
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HEALTH_CHECK_URL), e);
}
check.setHttp(address);
check.setMethod(properties.getHealthCheckMethod());
check.setInterval(properties.getHealthCheckInterval());
check.setTimeout(properties.getHealthCheckTimeout());
}
return check;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.
the class ConsulRegistry method subscribe.
@Override
public List<ProviderGroup> subscribe(ConsumerConfig config) {
String appName = config.getAppName();
if (!registryConfig.isSubscribe()) {
// 注册中心不订阅
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
}
return null;
}
if (!config.isSubscribe()) {
return null;
}
try {
List<ProviderInfo> providers = lookupHealthService(config);
if (EventBus.isEnable(ConsumerSubEvent.class)) {
ConsumerSubEvent event = new ConsumerSubEvent(config);
EventBus.post(event);
}
return Collections.singletonList(new ProviderGroup().addAll(providers));
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Exception e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER, EXT_NAME), e);
}
}
Aggregations