当前位置:网站首页>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 的微服务架构模式开发解决方案。
项目地址 ?
边栏推荐
- 【Leetcode】1352. 最后 K 个数的乘积
- Basic knowledge points
- Vs2015 secret key
- How much do you know about 3DMAX rendering skills and HDRI light sources? Dry goods sharing
- Cocos2dx screen adaptation
- Es module and commonjs learning notes
- A three-dimensional button
- Simple modal box
- Simple HelloWorld color change
- Data is stored in the form of table
猜你喜欢
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Download and use of font icons
用 Jmeter 工具做个小型压力测试
JVM call not used once in ten years
Optimization scheme of win10 virtual machine cluster
Collapse of adjacent vertical outer margins
PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
Redis 排查大 key 的4种方法,优化必备
Autocad-- dynamic zoom
随机推荐
Cocos progress bar progresstimer
Embedded database development programming (V) -- DQL
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
《动手学深度学习》学习笔记
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
[leetcode] integer inversion [7]
Listview is added and deleted at the index
cocos2dx_ Lua particle system
mysql審計日志歸檔
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
Redis 排查大 key 的4种方法,优化必备
Personal required code
Grail layout and double wing layout
How to choose a panoramic camera that suits you?
3dsmax2018 common operations and some shortcut keys of editable polygons
Page countdown
Forecast report on research and investment prospects of Chinese wormwood industry (2022 Edition)
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
嵌入式数据库开发编程(零)
"Measuring curve length" of CAD dream drawing