Search in sources :

Example 61 with SofaRpcRuntimeException

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

the class AbstractHttpServer method registerProcessor.

@Override
public void registerProcessor(ProviderConfig providerConfig, Invoker instance) {
    // 缓存Invoker对象
    String serviceName = getUniqueName(providerConfig);
    serverHandler.getInvokerMap().put(serviceName, instance);
    // 解析方法,不支持方法重载
    Class itfClass = providerConfig.getProxyClass();
    HashMap<String, Method> methodsLimit = new HashMap<String, Method>(16);
    for (Method method : itfClass.getMethods()) {
        String methodName = method.getName();
        if (methodsLimit.containsKey(methodName)) {
            // 重名的方法
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_OVERLOADING_METHOD, itfClass.getName(), methodName));
        }
        methodsLimit.put(methodName, method);
    }
    for (Map.Entry<String, Method> entry : methodsLimit.entrySet()) {
        // 缓存接口的方法
        ReflectCache.putMethodCache(serviceName, entry.getValue());
        ReflectCache.putMethodSigsCache(serviceName, entry.getKey(), ClassTypeUtils.getTypeStrs(entry.getValue().getParameterTypes(), true));
    }
}
Also used : HashMap(java.util.HashMap) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 62 with SofaRpcRuntimeException

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

the class SofaHystrixCommand method getFallback.

protected SofaResponse getFallback(SofaResponse response, Throwable t) {
    FallbackFactory fallbackFactory = SofaHystrixConfig.loadFallbackFactory((ConsumerConfig) invoker.getConfig());
    if (fallbackFactory == null) {
        return super.getFallback();
    }
    Object fallback = fallbackFactory.create(new FallbackContext(invoker, request, response, t));
    if (fallback == null) {
        return super.getFallback();
    }
    try {
        Object fallbackResult = request.getMethod().invoke(fallback, request.getMethodArgs());
        SofaResponse actualResponse = new SofaResponse();
        actualResponse.setAppResponse(fallbackResult);
        return actualResponse;
    } catch (IllegalAccessException e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HYSTRIX_FALLBACK_FAIL), e);
    } catch (InvocationTargetException e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_HYSTRIX_FALLBACK_FAIL), e.getTargetException());
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 63 with SofaRpcRuntimeException

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

the class GenericServiceImpl method generic.

@Override
public void generic(Request request, StreamObserver<Response> responseObserver) {
    SofaRequest sofaRequest = TracingContextKey.getKeySofaRequest().get(Context.current());
    String methodName = sofaRequest.getMethodName();
    String interfaceName = sofaRequest.getInterfaceName();
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Class proxyClass = ClassTypeUtils.getClass(interfaceName);
        ClassLoader interfaceClassLoader = proxyClass.getClassLoader();
        Thread.currentThread().setContextClassLoader(interfaceClassLoader);
        Class[] argTypes = getArgTypes(request);
        Serializer serializer = SerializerFactory.getSerializer(request.getSerializeType());
        Method declaredMethod = proxyClass.getDeclaredMethod(methodName, argTypes);
        Object[] invokeArgs = getInvokeArgs(request, argTypes, serializer);
        // fill sofaRequest
        sofaRequest.setMethod(declaredMethod);
        sofaRequest.setMethodArgs(invokeArgs);
        sofaRequest.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
        SofaResponse response = invoker.invoke(sofaRequest);
        Object ret = getAppResponse(declaredMethod, response);
        Response.Builder builder = Response.newBuilder();
        builder.setSerializeType(request.getSerializeType());
        builder.setType(declaredMethod.getReturnType().getName());
        builder.setData(ByteString.copyFrom(serializer.encode(ret, null).array()));
        Response build = builder.build();
        responseObserver.onNext(build);
        responseObserver.onCompleted();
    } catch (Exception e) {
        LOGGER.error("Invoke " + methodName + " error:", e);
        throw new SofaRpcRuntimeException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ByteString(com.google.protobuf.ByteString) Method(java.lang.reflect.Method) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Response(triple.Response) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Example 64 with SofaRpcRuntimeException

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

the class SslContextBuilder method buildForClient.

public static SslContext buildForClient() {
    // Configure SSL.
    SslContext sslCtx;
    try {
        if (SSL) {
            SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
            sslCtx = io.netty.handler.ssl.SslContextBuilder.forClient().sslProvider(provider).ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE).trustManager(InsecureTrustManagerFactory.INSTANCE).applicationProtocolConfig(new ApplicationProtocolConfig(ApplicationProtocolConfig.Protocol.ALPN, // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
            ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
            ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)).build();
        } else {
            sslCtx = null;
        }
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_START_CLIENT, "HTTP/2"), e);
    }
    return sslCtx;
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SslProvider(io.netty.handler.ssl.SslProvider) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SslContext(io.netty.handler.ssl.SslContext) ApplicationProtocolConfig(io.netty.handler.ssl.ApplicationProtocolConfig)

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