当前位置:网站首页>Solon 框架如何方便获取每个请求的响应时间?
Solon 框架如何方便获取每个请求的响应时间?
2022-07-05 05:13:00 【林西东】
经常会有同学问 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.应用@Controllerpublic class DemoController extends ControllerBase{ @Mapping("/hell") public void hello(){ }}
方式4:基于轻网关的处理链
//轻一点的示例@Mapping("/API/V1/**")@Controllerpublic 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/**")@Controllerpublic 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 的微服务架构模式开发解决方案。
项目地址 ?
边栏推荐
- cocos_ Lua listview loads too much data
- Common technologies of unity
- 2022/7/1学习总结
- Research on the value of background repeat of background tiling
- 嵌入式数据库开发编程(零)
- Do a small pressure test with JMeter tool
- Research on the value of background repeat of background tiling
- Lua determines whether the current time is the time of the day
- MySQL audit log Archive
- The next key of win generates the timestamp file of the current day
猜你喜欢
随机推荐
被舆论盯上的蔚来,何时再次“起高楼”?
小程序直播+電商,想做新零售電商就用它吧!
cocos2dx_ Lua card flip
Count sort
Pause and resume of cocos2dx Lua scenario
MySQL audit log archiving
China polyurethane rigid foam Market Research and investment forecast report (2022 Edition)
Use of snippets in vscode (code template)
中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
Data is stored in the form of table
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Basic knowledge points
Unity ugui source code graphic
[转]:Apache Felix Framework配置属性
Collapse of adjacent vertical outer margins
Cocos2dx screen adaptation
AutoCAD - set layer
LeetCode之单词搜索(回溯法求解)
2022/7/1學習總結
Unity connects to the database