当前位置:网站首页>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 ?
边栏推荐
猜你喜欢
随机推荐
cocos2dx_ Lua card flip
2022 / 7 / 1 Résumé de l'étude
Lua wechat avatar URL
Learning notes of "hands on learning in depth"
Grail layout and double wing layout
[LeetCode] 整数反转【7】
object serialization
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
Solon 框架如何方便获取每个请求的响应时间?
Haut OJ 1350: choice sends candy
十年不用一次的JVM调用
2022/7/1 learning summary
[turn to] MySQL operation practice (I): Keywords & functions
Es module and commonjs learning notes
Unity enables mobile phone vibration
[allocation problem] 135 Distribute candy
[to be continued] [UE4 notes] L2 interface introduction
Haut OJ 1245: large factorial of CDs --- high precision factorial
[es practice] use the native realm security mode on es