当前位置:网站首页>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 ?
边栏推荐
- [turn]: Apache Felix framework configuration properties
- Bubble sort summary
- [binary search] 69 Square root of X
- Insert sort
- Research on the value of background repeat of background tiling
- 搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
- cocos_ Lua loads the file generated by bmfont fnt
- 2022/7/1學習總結
- Unity sends messages and blocks indecent words
- win10虚拟机集群优化方案
猜你喜欢
Yolov5 ajouter un mécanisme d'attention
[to be continued] [UE4 notes] L1 create and configure items
Collapse of adjacent vertical outer margins
服务熔断 Hystrix
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
UE 虚幻引擎,项目结构
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
win10虚拟机集群优化方案
object serialization
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
随机推荐
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
Vs2015 secret key
《动手学深度学习》学习笔记
UE 虚幻引擎,项目结构
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
Simple HelloWorld color change
2022年上半年国家教师资格证考试
【ES实战】ES上的native realm安全方式使用
YOLOv5-Shufflenetv2
Unity enables mobile phone vibration
[turn]: Apache Felix framework configuration properties
Solon Logging 插件的添加器级别控制和日志器的级别控制
[allocation problem] 135 Distribute candy
Yolov5 adds attention mechanism
To the distance we have been looking for -- film review of "flying house journey"
一个新的微型ORM开源框架
Improvement of pointnet++
Haut OJ 1221: a tired day
Haut OJ 1243: simple mathematical problems
YOLOv5添加注意力机制