当前位置:网站首页>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 ?
边栏推荐
- Grail layout and double wing layout
- [allocation problem] 135 Distribute candy
- cocos2dx_ Lua particle system
- Embedded database development programming (V) -- DQL
- [allocation problem] 455 Distribute cookies
- Unity check whether the two objects have obstacles by ray
- Solon 框架如何方便获取每个请求的响应时间?
- 64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
- Heap sort summary
- Fragment addition failed error lookup
猜你喜欢
服务熔断 Hystrix
[to be continued] [UE4 notes] L2 interface introduction
Simple modal box
Service fusing hystrix
Grail layout and double wing layout
Stm32cubemx (8): RTC and RTC wake-up interrupt
Count sort
Research on the value of background repeat of background tiling
Unity ugui source code graphic
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
随机推荐
Pointnet++学习
[binary search] 69 Square root of X
[merge array] 88 merge two ordered arrays
嵌入式数据库开发编程(五)——DQL
Programmers' experience of delivering takeout
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Do a small pressure test with JMeter tool
2022/7/2 question summary
十年不用一次的JVM调用
[es practice] use the native realm security mode on es
room数据库的使用
嵌入式数据库开发编程(零)
Shell Sort
小程序直播+電商,想做新零售電商就用它吧!
Solon Auth 认证框架使用演示(更简单的认证框架)
SDEI初探-透过事务看本质
[to be continued] [UE4 notes] L2 interface introduction
The next key of win generates the timestamp file of the current day
Cocos create Jiugongge pictures
Use of snippets in vscode (code template)