当前位置:网站首页>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 ?

原网站

版权声明
本文为[Lin Xidong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050513075333.html