zoukankan      html  css  js  c++  java
  • Solon 框架如何方便获取每个请求的响应时间?

    经常会有同学问 Solon 怎样才能获取每个请求的响应时间?要求是不需要给每个函数加注解。故此,整理了一下。

    不给每个函数加注解,主要有两种方式可以获取请求响应时间:

    方式1:基于全局过滤器

    public class DemoApp{
        public static void main(String[] args){
            SolonApp app = Solon.start(DemoApp.class, args);
    
            //全局过滤器
            app.filter((ctx, chain) -> {
                //记录开始时间
                long start = System.currentTimeMillis();
                try {
                    chain.doFilter(ctx);
                } finally {
                    //获得耗时
                    long elapsed = (System.currentTimeMillis() - start);
                }
            });
        }
    }
    

    方式2:基于处理链 + 上下文特性

    public class DemoApp{
        public static void main(String[] args){
            SolonApp app = Solon.start(TestApp.class, args);
    
            //前置处理
            app.before(c -> c.attrSet("start", System.currentTimeMillis()));
    
            //后置处理
            app.after(c -> {
                long start = c.attr("start", 0L);
                long elapsed = (System.currentTimeMillis() - start);
            });
        }
    }
    

    其实也还有第三种,基于控制器基类;以及第四种基于轻网关。

    方式3:基于控制器基类(和方式1 有点儿像)

    //1.定义基类(增加包围拦截注入)
    @Around(TimeInterceptor.class)
    public class ControllerBase {
    }
    
    //2.定义拦截器
    public class TimeInterceptor implements Interceptor {
        @Override
        public Object doIntercept(Invocation inv) throws Throwable {
            long start = System.currentTimeMillis();
            try {
                return inv.invoke();
            } finally {
                long elapsed = (System.currentTimeMillis() - start);
            }
        }
    }
    
    //3.应用
    @Controller
    public class DemoController extends ControllerBase{
        @Mapping("/hell")
        public void hello(){
            
        }
    }
    

    方式4:基于轻网关的处理链

    //轻一点的示例
    @Mapping("/API/V1/**")
    @Controller
    public class ApiGateway extends Gateway {
        @Override
        protected void register() {
            before(new StartHandler()); //开始计时
    
            after(new OutputBuildHandler());//构建输出内容
            after(new OutputHandler());//输出
            after(new EndBeforeLogHandler());//记录日志
            after(new EndHandler("API"));//结束计时,并上报
    
            addBeans(bw -> "api".equals(bw.tag()));
        }
    }
    
    //重一点的示例(平时我搭接口项目框架,用的是这一例)
    @Mapping("/API/V2/**")
    @Controller
    public class ApiGatewayOfApp extends UapiGateway {
        @Override
        protected void register() {
            filter(new BreakerFilter()); //融断
    
            before(new StartHandler()); //开始计时
            before(new ParamsParseHandler()); //参数解析
            before(new ParamsSignCheckHandler(new Md5Encoder())); //参数签名较验
            before(new ParamsRebuildHandler(new AesDecoder())); //参数重构
            before(new ParamsNeedCheckHandler("g_lang"));//参数必要性检查//即公共参数
            before(new ParamsLocaleHandler());
    
            after(new OutputBuildHandler(new AesEncoder())); //输出构建
            after(new OutputSignHandler(new Md5Encoder())); //输出签名
            after(new OutputHandler()); //输出
            after(new EndBeforeLogHandler()); //日志
            after(new EndHandler("app.v1")); //结束计时
    
           addBeans(bw -> "api".equals(bw.tag()));
        }
    }
    

    有这方面困惑的同学,希望能看到此文。

    关于 Solon ?

    Solon 是一个轻量的Java基础开发框架。强调,克制 + 简洁 + 开放的原则;力求,更小、更快、更自由的体验。支持:RPC、REST API、MVC、Job、Micro service、WebSocket、Socket 等多种开发模式。短小而精悍!

    关于 Solon Cloud ?

    Solon Cloud 是一系列的接口标准和配置规范,相当于DDD模式里的防腐层概念。是 Solon 的微服务架构模式开发解决方案。

    项目地址 ?

  • 相关阅读:
    大型站点技术架构PDF阅读笔记(一):
    【大话QT之十三】系统软件自己主动部署实现方案
    VS编译duilib项目时候的错误解决方法整理
    Missing iOS Distribution signing identity for …, 在打包的时候发现证书过期了。
    Django项目国际化
    Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
    BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]
    NOIP2016DAY1题解
    清北学堂入学测试P4751 H’s problem(h)
    BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
  • 原文地址:https://www.cnblogs.com/noear/p/15433245.html
Copyright © 2011-2022 走看看