Search in sources :

Example 51 with SofaRpcRuntimeException

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);
        }
    }
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) NacosException(com.alibaba.nacos.api.exception.NacosException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 52 with SofaRpcRuntimeException

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);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 53 with SofaRpcRuntimeException

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);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) NacosException(com.alibaba.nacos.api.exception.NacosException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 54 with SofaRpcRuntimeException

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;
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) NewService(com.ecwid.consul.v1.agent.model.NewService) URL(java.net.URL) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 55 with SofaRpcRuntimeException

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);
    }
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) 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