use of com.alipay.sofa.rpc.invoke.Invoker in project sofa-rpc by sofastack.
the class AbstractHttpServerTask method run.
@Override
public void run() {
// RPC内置上下文
RpcInternalContext context = RpcInternalContext.getContext();
context.setProviderSide(true);
String appName = request.getTargetAppName();
if (appName == null) {
// 默认全局appName
appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
}
try {
// 这个 try-finally 为了保证Context一定被清理
Channel channel = ctx.channel();
// 远程地址
context.setRemoteAddress((InetSocketAddress) channel.remoteAddress());
// 远程返回的通道
context.setAttachment(RpcConstants.HIDDEN_KEY_ASYNC_CONTEXT, channel);
if (EventBus.isEnable(ServerReceiveEvent.class)) {
EventBus.post(new ServerReceiveEvent(request));
}
// 开始处理
// 响应,用于返回
SofaResponse response = null;
// 异常,用于记录
Throwable throwable = null;
HttpResponseStatus status = null;
ProviderConfig providerConfig = null;
String serviceName = request.getTargetServiceUniqueName();
Serializer serializer = null;
if (request.getSerializeType() > 0) {
serializer = SerializerFactory.getSerializer(request.getSerializeType());
}
try {
// 这个try-catch 保证一定有Response
invoke: {
// 查找服务
Invoker invoker = serverHandler.getInvokerMap().get(serviceName);
if (invoker == null) {
throwable = cannotFoundService(appName, serviceName);
response = MessageBuilder.buildSofaErrorResponse(throwable.getMessage());
status = HttpResponseStatus.NOT_FOUND;
break invoke;
}
if (invoker instanceof ProviderProxyInvoker) {
providerConfig = ((ProviderProxyInvoker) invoker).getProviderConfig();
// 找到服务后,打印服务的appName
appName = providerConfig != null ? providerConfig.getAppName() : null;
}
// 查找方法
String methodName = request.getMethodName();
Method serviceMethod = serverHandler.getMethod(serviceName, methodName);
if (serviceMethod == null) {
throwable = cannotFoundServiceMethod(appName, methodName, serviceName);
response = MessageBuilder.buildSofaErrorResponse(throwable.getMessage());
status = HttpResponseStatus.NOT_FOUND;
break invoke;
} else {
request.setMethod(serviceMethod);
}
AbstractByteBuf reqData = request.getData();
if (reqData != null) {
try {
Map<String, String> map = new HashMap<String, String>(4);
map.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
map.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
map.put(RemotingConstants.HEAD_TARGET_APP, request.getTargetAppName());
serializer.decode(reqData, request, map);
} catch (Exception e) {
LOGGER.errorWithApp(appName, "Server deserialize error, request from " + channel.remoteAddress(), e);
response = MessageBuilder.buildSofaErrorResponse("Server deserialize error, " + e.getMessage());
break invoke;
}
} else if (request.getMethodArgs() == null) {
request.setMethodArgs(CodecUtils.EMPTY_OBJECT_ARRAY);
}
// 真正调用
response = doInvoke(serviceName, invoker, request);
}
} catch (Exception e) {
// 服务端异常,不管是啥异常
LOGGER.errorWithApp(appName, "Server Processor Error!", e);
throwable = e;
response = MessageBuilder.buildSofaErrorResponse(e.getMessage());
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
}
// Response不为空,代表需要返回给客户端
if (response != null) {
response.setSerializeType(request.getSerializeType());
// 其它正常请求
try {
// 这个try-catch 保证一定要记录tracer
if (response.isError()) {
ByteBuf content = ctx.alloc().buffer();
content.writeBytes(StringSerializer.encode(response.getErrorMsg()));
sendRpcError(status == null ? HttpResponseStatus.INTERNAL_SERVER_ERROR : status, content);
} else {
if (response.getAppResponse() instanceof Throwable) {
ByteBuf content = ctx.alloc().buffer();
String errorMsg = ExceptionUtils.toString((Throwable) response.getAppResponse());
content.writeBytes(StringSerializer.encode(errorMsg));
sendAppError(HttpResponseStatus.OK, content);
} else {
ByteBuf content = ctx.alloc().buffer();
if (request.getSerializeType() > 0) {
AbstractByteBuf bs = serializer.encode(response, null);
content.writeBytes(bs.array());
} else {
content.writeBytes(StringSerializer.encode(response.getAppResponse().toString()));
}
sendAppResponse(HttpResponseStatus.OK, content);
}
}
} finally {
if (EventBus.isEnable(ServerSendEvent.class)) {
EventBus.post(new ServerSendEvent(request, response, throwable));
}
}
}
} catch (Throwable e) {
// 可能有返回时的异常
if (LOGGER.isErrorEnabled(appName)) {
LOGGER.errorWithApp(appName, e.getMessage(), e);
}
} finally {
serverHandler.getProcessingCount().decrementAndGet();
if (EventBus.isEnable(ServerEndHandleEvent.class)) {
EventBus.post(new ServerEndHandleEvent());
}
RpcInvokeContext.removeContext();
RpcInternalContext.removeAllContext();
}
}
use of com.alipay.sofa.rpc.invoke.Invoker in project sofa-rpc by sofastack.
the class UniqueIdInvoker method invoke.
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
// find invoker and invoke
String interfaceName = request.getInterfaceName();
String uniqueId = this.getUniqueIdFromInvokeContext();
Invoker invoker = this.findInvoker(interfaceName, uniqueId);
return invoker.invoke(request);
}
Aggregations