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));
}
}
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());
}
}
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);
}
}
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;
}
Aggregations