Search in sources :

Example 46 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class AbstractInterfaceConfig method updateAttribute.

/**
 * 覆盖属性,可以检查,或者更新
 *
 * @param property    属性
 * @param newValueStr 要设置的值
 * @param overwrite   是否覆盖 true直接覆盖,false为检查
 * @return 是否有变更 boolean
 */
public boolean updateAttribute(String property, String newValueStr, boolean overwrite) {
    try {
        boolean changed = false;
        if (property.charAt(0) == RpcConstants.HIDE_KEY_PREFIX) {
            // 方法级配置 例如.echoStr.timeout
            String methodAndP = property.substring(1);
            int index = methodAndP.indexOf(RpcConstants.HIDE_KEY_PREFIX);
            if (index <= 0) {
                throw ExceptionUtils.buildRuntime(property, newValueStr, "Unknown update attribute key!");
            }
            String methodName = methodAndP.substring(0, index);
            String methodProperty = methodAndP.substring(index + 1);
            MethodConfig methodConfig = getMethodConfig(methodName);
            Method getMethod = ReflectUtils.getPropertyGetterMethod(MethodConfig.class, methodProperty);
            // 旧值的类型
            Class propertyClazz = getMethod.getReturnType();
            // 拿到旧的值
            Object oldValue = null;
            Object newValue = CompatibleTypeUtils.convert(newValueStr, propertyClazz);
            if (methodConfig == null) {
                methodConfig = new MethodConfig();
                methodConfig.setName(methodName);
                if (this.methods == null) {
                    this.methods = new ConcurrentHashMap<String, MethodConfig>();
                }
                this.methods.put(methodName, methodConfig);
                changed = true;
            } else {
                oldValue = BeanUtils.getProperty(methodConfig, methodProperty, propertyClazz);
                if (oldValue == null) {
                    if (newValueStr != null) {
                        changed = true;
                    }
                } else {
                    changed = !oldValue.equals(newValue);
                }
            }
            if (changed && overwrite) {
                // 覆盖属性
                BeanUtils.setProperty(methodConfig, methodProperty, propertyClazz, newValue);
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("Property \"" + methodName + "." + methodProperty + "\" changed from {} to {}", oldValue, newValueStr);
                }
            }
        } else {
            // 接口级配置 例如timeout
            // 先通过get方法找到类型
            Method getMethod = ReflectUtils.getPropertyGetterMethod(getClass(), property);
            // 旧值的类型
            Class propertyClazz = getMethod.getReturnType();
            // 拿到旧的值
            Object oldValue = BeanUtils.getProperty(this, property, propertyClazz);
            Object newValue = CompatibleTypeUtils.convert(newValueStr, propertyClazz);
            if (oldValue == null) {
                if (newValueStr != null) {
                    changed = true;
                }
            } else {
                changed = !oldValue.equals(newValue);
            }
            if (changed && overwrite) {
                // 覆盖属性
                BeanUtils.setProperty(this, property, propertyClazz, newValue);
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("Property \"" + property + "\" changed from {} to {}", oldValue, newValueStr);
                }
            }
        }
        return changed;
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UPDATE_ATTRIBUTE, property, newValueStr), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Method(java.lang.reflect.Method) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 47 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class ExceptionUtilsTest method buildRuntime1.

@Test
public void buildRuntime1() throws Exception {
    SofaRpcRuntimeException exception = ExceptionUtils.buildRuntime("xxx111", "222", "yyy");
    Assert.assertTrue(exception.getMessage().contains("xxx111"));
    Assert.assertTrue(exception.getMessage().contains("222"));
    Assert.assertTrue(exception.getMessage().contains("yyy"));
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Test(org.junit.Test)

Example 48 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class MulticastRegistry method init.

@Override
public void init() {
    if (multicastSocket != null) {
        return;
    }
    String addressInput = registryConfig.getAddress();
    if (StringUtils.isEmpty(addressInput)) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_EMPTY_ADDRESS, EXT_NAME));
    }
    try {
        if (!addressInput.startsWith(EXT_NAME)) {
            addressInput = EXT_NAME + "://" + addressInput;
        }
        URI url = new URI(addressInput);
        multicastPort = url.getPort();
        if (multicastPort <= 0) {
            multicastPort = DEFAULT_MULTICAST_PORT;
        }
        multicastAddress = InetAddress.getByName(url.getHost());
        MulticastRegistryHelper.checkMulticastAddress(multicastAddress);
        multicastSocket = new MulticastSocket(multicastPort);
        NetUtils.joinMulticastGroup(multicastSocket, multicastAddress);
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                byte[] buf = new byte[2048];
                DatagramPacket recv = new DatagramPacket(buf, buf.length);
                while (!multicastSocket.isClosed()) {
                    try {
                        multicastSocket.receive(recv);
                        String msg = new String(recv.getData()).trim();
                        int i = msg.indexOf('\n');
                        if (i > 0) {
                            msg = msg.substring(0, i).trim();
                        }
                        MulticastRegistry.this.receive(msg, (InetSocketAddress) recv.getSocketAddress());
                        Arrays.fill(buf, (byte) 0);
                    } catch (Throwable e) {
                        if (!multicastSocket.isClosed()) {
                            LOGGER.error(e.getMessage(), e);
                        }
                    }
                }
            }
        }, "SofaMulticastRegistryReceiver");
        thread.setDaemon(true);
        thread.start();
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_REGISTRY_INIT, EXT_NAME), e);
    }
    String cleanPeriodStr = registryConfig.getParameter(CLEAN_PERIOD);
    if (StringUtils.isNotBlank(cleanPeriodStr)) {
        this.cleanPeriod = Integer.parseInt(cleanPeriodStr);
    }
    if (!Boolean.FALSE.toString().equalsIgnoreCase(registryConfig.getParameter(CLEAN))) {
        this.cleanFuture = cleanExecutor.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                try {
                    // Remove the expired
                    clean();
                } catch (Throwable t) {
                    // Defensive fault tolerance
                    LOGGER.error("Unexpected exception occur at clean expired provider, cause: " + t.getMessage(), t);
                }
            }
        }, cleanPeriod, cleanPeriod, TimeUnit.MILLISECONDS);
    } else {
        this.cleanFuture = null;
    }
}
Also used : MulticastSocket(java.net.MulticastSocket) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) URI(java.net.URI) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 49 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class LocalRegistryHelper method loadBackupFileToCache.

static Map<String, ProviderGroup> loadBackupFileToCache(String address) {
    Map<String, ProviderGroup> memoryCache = new ConcurrentHashMap<String, ProviderGroup>();
    // 从文件夹下读取指定文件
    File regFile = new File(address);
    if (!regFile.exists()) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Load backup file failure cause by can't found file: {}", regFile.getAbsolutePath());
        }
    } else {
        try {
            String content = FileUtils.file2String(regFile);
            Map<String, ProviderGroup> tmp = unMarshal(content);
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Load backup file from {}", regFile.getAbsolutePath());
            }
            // 加载到内存中
            if (tmp != null) {
                memoryCache.putAll(tmp);
            }
        } catch (IOException e) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_READ_BACKUP_FILE, regFile.getAbsolutePath()), e);
        }
    }
    return memoryCache;
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) IOException(java.io.IOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 50 with SofaRpcRuntimeException

use of com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException in project sofa-rpc by sofastack.

the class PolarisRegistry 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 = findService(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