private static String generateKey(SoaHeader soaHeader) { return soaHeader.getServiceName() + ":" + soaHeader.getMethodName() + ":" + soaHeader.getVersionName(); }
private String generateKey(SoaHeader header) { return header.getServiceName() + ":" + header.getMethodName() + ":" + header.getVersionName(); } }
@Override public void doFilter(FilterChain chain) throws TException { final long startTime = System.currentTimeMillis(); final SoaHeader soaHeader = (SoaHeader) chain.getAttribute(StubFilterChain.ATTR_KEY_HEADER); try { chain.doFilter(); } finally { LOGGER.info("{} {} {} 耗时:{}ms", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), System.currentTimeMillis() - startTime); } }
@Override public void doFilter(FilterChain chain) throws TException { final long startTime = System.currentTimeMillis(); final SoaHeader soaHeader = (SoaHeader) chain.getAttribute(StubFilterChain.ATTR_KEY_HEADER); try { chain.doFilter(); } finally { LOGGER.info("{} {} {} 耗时:{}ms", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), System.currentTimeMillis() - startTime); } }
private static void methodInvoke(Class<?> logClass, SoaHeader soaHeader, String methodName, Consumer<Logger> loggerConsumer, Object... args) { try { ProcessorKey key = new ProcessorKey(soaHeader.getServiceName(), soaHeader.getVersionName()); ClassLoader appClassLoader = SoaAppClassLoaderCache.getAppClassLoaderMap().get(key); if (appClassLoader != null) { Object appLogger = getLogger(appClassLoader, logClass, appClassLoader.hashCode()); Method infoMethod = getMethod(methodName, logClass, appLogger, appClassLoader.hashCode()); infoMethod.invoke(appLogger, args); } else { Logger containerLogger = LoggerFactory.getLogger(logClass); loggerConsumer.accept(containerLogger); } } catch (Exception e) { //有异常用容器的logger打日志 LOGGER.error(e.getMessage()); Logger containerLogger = LoggerFactory.getLogger(logClass); loggerConsumer.accept(containerLogger); } }
@Override public void doFilter(FilterChain chain) throws TException { final long startTime = System.currentTimeMillis(); final SoaHeader soaHeader = (SoaHeader) chain.getAttribute(ContainerFilterChain.ATTR_KEY_HEADER); final TransactionContext context = (TransactionContext) chain.getAttribute(ContainerFilterChain.ATTR_KEY_CONTEXT); try { chain.doFilter(); } finally { LOGGER.info("{} {} {} {} 耗时:{}ms", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), context.getSeqid(), System.currentTimeMillis() - startTime); } }
@Override public void doFilter(FilterChain chain) throws TException { final SoaHeader soaHeader = (SoaHeader) chain.getAttribute(StubFilterChain.ATTR_KEY_HEADER); final Object request = chain.getAttribute(StubFilterChain.ATTR_KEY_REQUEST); LOGGER.info("{} {} {} request header:{} body:{}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), soaHeader.toString(), request.toString()); try { chain.doFilter(); } finally { Object response = chain.getAttribute(StubFilterChain.ATTR_KEY_RESPONSE); if (response != null) LOGGER.info("{} {} {} response header:{} body:{}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), soaHeader.toString(), formatToString(response.toString())); } }
public Task(TransactionContext context) { this.startTime = System.currentTimeMillis(); this.seqid = context.getSeqid(); SoaHeader soaHeader = context.getHeader(); this.serviceName = soaHeader.getServiceName(); this.versionName = soaHeader.getVersionName(); this.methodName = soaHeader.getMethodName(); this.callerFrom = soaHeader.getCallerFrom().isPresent() ? soaHeader.getCallerFrom().get() : null; this.callerIp = soaHeader.getCallerIp().isPresent() ? soaHeader.getCallerIp().get() : null; this.operatorId = soaHeader.getOperatorId().isPresent() ? soaHeader.getOperatorId().get() : null; this.operatorName = soaHeader.getOperatorName().isPresent() ? soaHeader.getOperatorName().get() : null; this.customerId = soaHeader.getCustomerId().isPresent() ? soaHeader.getCustomerId().get() : null; this.customerName = soaHeader.getCustomerName().isPresent() ? soaHeader.getCustomerName().get() : null; this.currentThread = Thread.currentThread(); }
@Override public void doFilter(FilterChain chain) throws TException { final SoaHeader soaHeader = (SoaHeader) chain.getAttribute(StubFilterChain.ATTR_KEY_HEADER); final Object request = chain.getAttribute(StubFilterChain.ATTR_KEY_REQUEST); final long startTime = System.currentTimeMillis(); final InvocationContext context = (InvocationContext) chain.getAttribute(StubFilterChain.ATTR_KEY_CONTEXT); LOGGER.info("{} {} {} request", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName()); try { chain.doFilter(); } finally { // Object response = chain.getAttribute(StubFilterChain.ATTR_KEY_RESPONSE); final long endTime = System.currentTimeMillis(); LOGGER.info("{} {} {} callee: {}:{} response respCode:{} time:{}ms", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), context.getCalleeIp(), context.getCalleePort(), soaHeader.getRespCode(), endTime-startTime); } }
private void writeErrorMessage(ChannelHandlerContext ctx, ByteBuf outputBuf, TransactionContext context, SoaHeader soaHeader, TSoaTransport outputSoaTransport, TSoaServiceProtocol outputProtocol, SoaException e) { if (outputProtocol != null) { try { if (outputBuf.writerIndex() > 0) outputBuf.writerIndex(Integer.BYTES); soaHeader.setRespCode(Optional.ofNullable(e.getCode())); soaHeader.setRespMessage(Optional.ofNullable(e.getMsg())); outputProtocol.writeMessageBegin(new TMessage(soaHeader.getServiceName() + ":" + soaHeader.getMethodName(), TMessageType.REPLY, context.getSeqid())); outputProtocol.writeMessageEnd(); outputSoaTransport.flush(); ctx.writeAndFlush(outputBuf); LOGGER.info("{} {} {} response header:{} body:{null}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), soaHeader.toString()); } catch (Throwable e1) { LOGGER.error(e1.getMessage(), e1); } } } }
/** * SoaHeader验证 */ @Override public void validate(SoaHeader bean) throws TException { if (bean.getServiceName() == null) throw new SoaException(SoaBaseCode.NotNull, "serviceName字段不允许为空"); if (bean.getMethodName() == null) throw new SoaException(SoaBaseCode.NotNull, "methodName字段不允许为空"); if (bean.getVersionName() == null) throw new SoaException(SoaBaseCode.NotNull, "versionName字段不允许为空"); }
/** * SoaHeader验证 */ @Override public void validate(SoaHeader bean) throws TException { if (bean.getServiceName() == null) throw new SoaException(SoaBaseCode.NotNull, "serviceName字段不允许为空"); if (bean.getMethodName() == null) throw new SoaException(SoaBaseCode.NotNull, "methodName字段不允许为空"); if (bean.getVersionName() == null) throw new SoaException(SoaBaseCode.NotNull, "versionName字段不允许为空"); }
/** * 根据matcher.Id,返回上下文中对应的值 * * @param ctx * @param matcher * @return */ public static Object checkFieldMatcher(InvocationContext ctx, Matcher matcher) { Id id = matcher.getId(); if ("operatorId".equals(id.getName())) { return ctx.getHeader().getOperatorId().orElse(null); } else if ("callerFrom".equals(id.getName())) { return ctx.getHeader().getCallerFrom().orElse(null); } else if ("ip".equals(id.getName())) { return ctx.getHeader().getCallerIp().orElse(null); } else if ("customerId".equals(id.getName())) { return ctx.getHeader().getCustomerId().orElse(null); } else if ("service".equals(id.getName())) { return ctx.getHeader().getServiceName(); } else if ("method".equals(id.getName())) { return ctx.getHeader().getMethodName(); } else if ("version".equals(id.getName())) { return ctx.getHeader().getVersionName(); } else { throw new AssertionError("not support Field: " + id.getName()); } }
/** * 根据matcher.Id,返回上下文中对应的值 * * @param ctx * @param matcher * @return */ public static Object checkFieldMatcher(InvocationContext ctx, Matcher matcher) { Id id = matcher.getId(); if ("operatorId".equals(id.getName())) { return ctx.getHeader().getOperatorId().orElse(null); } else if ("callerFrom".equals(id.getName())) { return ctx.getHeader().getCallerFrom().orElse(null); } else if ("ip".equals(id.getName())) { return ctx.getHeader().getCallerIp().orElse(null); } else if ("customerId".equals(id.getName())) { return ctx.getHeader().getCustomerId().orElse(null); } else if ("service".equals(id.getName())) { return ctx.getHeader().getServiceName(); } else if ("method".equals(id.getName())) { return ctx.getHeader().getMethodName(); } else if ("version".equals(id.getName())) { return ctx.getHeader().getVersionName(); } else { throw new AssertionError("not support Field: " + id.getName()); } }
/** * 异步处理,当返回结果被complete时调用 * * @param context * @param soaProcessFunction * @param result * @param out * @param future */ private void AsyncAccept(Context context, SoaProcessFunction<I, Object, Object, ? extends TBeanSerializer<Object>, ? extends TBeanSerializer<Object>> soaProcessFunction, Object result, TProtocol out, CompletableFuture future) { try { TransactionContext.Factory.setCurrentInstance((TransactionContext) context); SoaHeader soaHeader = context.getHeader(); LOGGER.info("{} {} {} {} response header:{} body:{}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), context.getSeqid(), soaHeader.toString(), formatToString(soaProcessFunction.getResSerializer().toString(result))); soaHeader.setRespCode(Optional.of("0000")); soaHeader.setRespMessage(Optional.of("成功")); out.writeMessageBegin(new TMessage(soaHeader.getMethodName(), TMessageType.CALL, context.getSeqid())); soaProcessFunction.getResSerializer().write(result, out); out.writeMessageEnd(); /** * 通知外层handler处理结果 */ future.complete(context); } catch (TException e) { e.printStackTrace(); } }
/** * 异步处理,当返回结果被complete时调用 * * @param context * @param soaProcessFunction * @param result * @param out * @param future */ private void AsyncAccept(Context context, SoaProcessFunction<I, Object, Object, ? extends TBeanSerializer<Object>, ? extends TBeanSerializer<Object>> soaProcessFunction, Object result, TProtocol out, CompletableFuture future) { try { TransactionContext.Factory.setCurrentInstance((TransactionContext) context); SoaHeader soaHeader = context.getHeader(); LOGGER.info("{} {} {} {} response header:{} body:{}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), context.getSeqid(), soaHeader.toString(), formatToString(soaProcessFunction.getResSerializer().toString(result))); soaHeader.setRespCode(Optional.of("0000")); soaHeader.setRespMessage(Optional.of("成功")); out.writeMessageBegin(new TMessage(soaHeader.getMethodName(), TMessageType.CALL, context.getSeqid())); soaProcessFunction.getResSerializer().write(result, out); out.writeMessageEnd(); /** * 通知外层handler处理结果 */ future.complete(context); } catch (TException e) { e.printStackTrace(); } }
/** * 异步处理,当返回结果被complete时调用 * * @param context * @param soaProcessFunction * @param result * @param out * @param future */ private void AsyncAccept(Context context, SoaProcessFunction<I, Object, Object, ? extends TCommonBeanSerializer<Object>, ? extends TCommonBeanSerializer<Object>> soaProcessFunction, Object result, TProtocol out, CompletableFuture future) { try { TransactionContext.Factory.setCurrentInstance((TransactionContext) context); SoaHeader soaHeader = context.getHeader(); LOGGER.info("{} {} {} {} response header:{} body:{}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), context.getSeqid(), soaHeader.toString(), formatToString(soaProcessFunction.getResSerializer().toString(result))); soaHeader.setRespCode(Optional.of("0000")); soaHeader.setRespMessage(Optional.of("成功")); out.writeMessageBegin(new TMessage(soaHeader.getMethodName(), TMessageType.CALL, context.getSeqid())); soaProcessFunction.getResSerializer().write(result, out); out.writeMessageEnd(); /** * 通知外层handler处理结果 */ future.complete(context); } catch (TException e) { e.printStackTrace(); } }
LOGGER.info("{} {} {} {} request header:{} body:{}", soaHeader.getServiceName(), soaHeader.getVersionName(), soaHeader.getMethodName(), context.getSeqid(), soaHeader.toString(), formatToString(soaProcessFunction.getReqSerializer().toString(args))); long startTime = System.currentTimeMillis();
String serviceKey = soaHeader.getServiceName() + "." + soaHeader.getVersionName() + "." + soaHeader.getMethodName() + ".consumer"; RegistryAgent registryAgent = RegistryAgentProxy.getCurrentInstance(RegistryAgentProxy.Type.Client);
public static PlatformProcessData getNewInstance(SoaHeader soaHeader) { PlatformProcessData processData = new PlatformProcessData(); processData.setServiceName(soaHeader.getServiceName()); processData.setMethodName(soaHeader.getMethodName()); processData.setVersionName(soaHeader.getVersionName()); processData.setServerIP(IPUtils.localIp()); processData.setServerPort(SoaSystemEnvProperties.SOA_CONTAINER_PORT); processData.setIAverageTime(0l); processData.setIMaxTime(0l); processData.setIMinTime(0l); processData.setITotalTime(0l); processData.setPAverageTime(0l); processData.setPMaxTime(0l); processData.setPMinTime(0l); processData.setPTotalTime(0l); processData.setFailCalls(0); processData.setSucceedCalls(0); processData.setTotalCalls(0); processData.setRequestFlow(0); processData.setResponseFlow(0); return processData; }