当前位置:网站首页>How can the Solon framework easily obtain the response time of each request?
How can the Solon framework easily obtain the response time of each request?
2022-07-05 05:19:00 【Lin Xidong】
Students often ask Solon How can I get the response time of each request ? The requirement is that you don't need to annotate each function . therefore , Sort it out .
Don't annotate every function , There are two main ways to get the request response time :
The way 1: Based on global filter
public class DemoApp{ public static void main(String[] args){ SolonApp app = Solon.start(DemoApp.class, args); // Global filter app.filter((ctx, chain) -> { // Record the start time long start = System.currentTimeMillis(); try { chain.doFilter(ctx); } finally { // Acquisition time long elapsed = (System.currentTimeMillis() - start); } }); }}
The way 2: Based on processing chain + Context features
public class DemoApp{ public static void main(String[] args){ SolonApp app = Solon.start(TestApp.class, args); // Preprocessing app.before(c -> c.attrSet("start", System.currentTimeMillis())); // Post processing app.after(c -> { long start = c.attr("start", 0L); long elapsed = (System.currentTimeMillis() - start); }); }}
In fact, there is a third , Based on the controller base class ; And the fourth is based on light gateway .
The way 3: Based on the controller base class ( And way 1 It's a bit like )
//1. Define base class ( Add surround intercept Injection )@Around(TimeInterceptor.class)public class ControllerBase {}//2. Define interceptors 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. application @Controllerpublic class DemoController extends ControllerBase{ @Mapping("/hell") public void hello(){ }}
The way 4: Processing chain based on light gateway
// A lighter example @Mapping("/API/V1/**")@Controllerpublic class ApiGateway extends Gateway { @Override protected void register() { before(new StartHandler()); // Start timing after(new OutputBuildHandler());// Build output after(new OutputHandler());// Output after(new EndBeforeLogHandler());// Log after(new EndHandler("API"));// Closing time , And report to addBeans(bw -> "api".equals(bw.tag())); }}// A heavier example ( Usually I take the interface project framework , This example is used )@Mapping("/API/V2/**")@Controllerpublic class ApiGatewayOfApp extends UapiGateway { @Override protected void register() { filter(new BreakerFilter()); // Meltdown before(new StartHandler()); // Start timing before(new ParamsParseHandler()); // Argument parsing before(new ParamsSignCheckHandler(new Md5Encoder())); // Parameter signature verification before(new ParamsRebuildHandler(new AesDecoder())); // Parameter refactoring before(new ParamsNeedCheckHandler("g_lang"));// Parameter necessity check // Public parameters before(new ParamsLocaleHandler()); after(new OutputBuildHandler(new AesEncoder())); // Output build after(new OutputSignHandler(new Md5Encoder())); // Output signature after(new OutputHandler()); // Output after(new EndBeforeLogHandler()); // journal after(new EndHandler("app.v1")); // Closing time addBeans(bw -> "api".equals(bw.tag())); }}
Students who are confused in this regard , Hope to see this article .
About Solon ?
Solon It's a light weight Java Basic development framework . emphasize , restraint + concise + The principle of openness ; Strive for , smaller 、 faster 、 A freer experience . Support :RPC、REST API、MVC、Job、Micro service、WebSocket、Socket And other development models . Short and sharp !
About Solon Cloud ?
Solon Cloud Is a series of interface standards and configuration specifications , amount to DDD The concept of anticorrosive coating in the model . yes Solon Microservice architecture pattern development solution .
Project address ?
边栏推荐
- Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
- 2022上半年全国教师资格证下
- UE 虚幻引擎,项目结构
- [turn to] MySQL operation practice (I): Keywords & functions
- UE fantasy engine, project structure
- JVM call not used once in ten years
- 小程序直播+电商,想做新零售电商就用它吧!
- National teacher qualification examination in the first half of 2022
- room数据库的使用
- BUUCTF MISC
猜你喜欢
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
Unity check whether the two objects have obstacles by ray
Binary search basis
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
Do a small pressure test with JMeter tool
GBase数据库助力湾区数字金融发展
Bucket sort
Shell Sort
Embedded database development programming (zero)
一个新的微型ORM开源框架
随机推荐
2022/7/1学习总结
object serialization
Unity card flipping effect
2022年上半年国家教师资格证考试
嵌入式数据库开发编程(六)——C API
Pointnet++学习
Es module and commonjs learning notes -- ESM and CJS used in nodejs
The present is a gift from heaven -- a film review of the journey of the soul
Optimization scheme of win10 virtual machine cluster
cocos_ Lua loads the file generated by bmfont fnt
[speed pointer] 142 circular linked list II
Embedded database development programming (V) -- DQL
Dotween usage records ----- appendinterval, appendcallback
django连接数据库报错,这是什么原因
Service fusing hystrix
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Unity shot tracking object
Magnifying glass effect
C language Essay 1
Double pointer Foundation