Search in sources :

Example 31 with SofaRpcRuntimeException

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

the class ProxyFactory method getInvoker.

/**
 * 解析出代理类的Invoker对象
 *
 * @param proxyObject 代理类实现
 * @return Invoker对象
 */
public static Invoker getInvoker(Object proxyObject, String proxyType) {
    try {
        ExtensionClass<Proxy> ext = ExtensionLoaderFactory.getExtensionLoader(Proxy.class).getExtensionClass(proxyType);
        if (ext == null) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Registry", proxyType));
        }
        Proxy proxy = ext.getExtInstance();
        return proxy.getInvoker(proxyObject);
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Registry", proxyType));
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 32 with SofaRpcRuntimeException

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

the class ProxyFactory method buildProxy.

/**
 * 构建代理类实例
 *
 * @param proxyType    代理类型
 * @param clazz        原始类
 * @param proxyInvoker 代码执行的Invoker
 * @param <T>          类型
 * @return 代理类实例
 * @throws Exception
 */
public static <T> T buildProxy(String proxyType, Class<T> clazz, Invoker proxyInvoker) throws Exception {
    try {
        ExtensionClass<Proxy> ext = ExtensionLoaderFactory.getExtensionLoader(Proxy.class).getExtensionClass(proxyType);
        if (ext == null) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Proxy", proxyType));
        }
        Proxy proxy = ext.getExtInstance();
        return proxy.getProxy(clazz, proxyInvoker);
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Proxy", proxyType), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 33 with SofaRpcRuntimeException

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

the class RegistryFactory method getRegistry.

/**
 * 得到注册中心对象
 *
 * @param registryConfig RegistryConfig类
 * @return Registry实现
 */
public static synchronized Registry getRegistry(RegistryConfig registryConfig) {
    if (ALL_REGISTRIES.size() > 3) {
        // 超过3次 是不是配错了?
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Size of registry is greater than 3, Please check it!");
        }
    }
    String protocol = null;
    try {
        // 注意:RegistryConfig重写了equals方法,如果多个RegistryConfig属性一样,则认为是一个对象
        Registry registry = ALL_REGISTRIES.get(registryConfig);
        if (registry == null) {
            protocol = registryConfig.getProtocol();
            ExtensionClass<Registry> ext = ExtensionLoaderFactory.getExtensionLoader(Registry.class).getExtensionClass(protocol);
            if (ext == null) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Registry", protocol));
            }
            registry = ext.getExtInstance(new Class[] { RegistryConfig.class }, new Object[] { registryConfig });
            ALL_REGISTRIES.put(registryConfig, registry);
        }
        return registry;
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Registry", protocol));
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ExtensionClass(com.alipay.sofa.rpc.ext.ExtensionClass)

Example 34 with SofaRpcRuntimeException

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

the class ServerFactory method getServer.

/**
 * 初始化Server实例
 *
 * @param serverConfig 服务端配置
 * @return Server
 */
public static synchronized Server getServer(ServerConfig serverConfig) {
    try {
        Server server = SERVER_MAP.get(Integer.toString(serverConfig.getPort()));
        if (server == null) {
            // 算下网卡和端口
            resolveServerConfig(serverConfig);
            ExtensionClass<Server> ext = ExtensionLoaderFactory.getExtensionLoader(Server.class).getExtensionClass(serverConfig.getProtocol());
            if (ext == null) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_UNSUPPORTED_PROTOCOL, serverConfig.getProtocol()));
            }
            server = ext.getExtInstance();
            server.init(serverConfig);
            SERVER_MAP.put(serverConfig.getPort() + "", server);
        }
        return server;
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_GET_SERVER), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 35 with SofaRpcRuntimeException

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

the class MsgPackHelper method loadClassToCache.

/**
 * load method paramters and return types to cache, will not pass through to next
 *
 * @param key        key
 * @param clazz      interface name
 * @param methodName method name
 */
private void loadClassToCache(String key, Class clazz, String methodName) {
    Method pbMethod = null;
    Method[] methods = clazz.getMethods();
    for (Method method : methods) {
        if (methodName.equals(method.getName())) {
            pbMethod = method;
            break;
        }
    }
    if (pbMethod == null) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(), methodName));
    }
    Class[] parameterTypes = pbMethod.getParameterTypes();
    if (parameterTypes == null || parameterTypes.length != 1) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_ONLY_ONE_PARAM, "msgpack", clazz.getName()));
    }
    Class reqClass = parameterTypes[0];
    requestClassCache.put(key, reqClass);
    Class resClass = pbMethod.getReturnType();
    if (resClass == void.class) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_VOID_RETURN, "msgpack", clazz.getName()));
    }
    responseClassCache.put(key, resClass);
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Method(java.lang.reflect.Method)

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