关于springboot:springboot-实现拦截的-3-种方式介绍及异步执行的思考

37次阅读

共计 12441 个字符,预计需要花费 32 分钟才能阅读完成。

springboot 拦挡形式

理论我的项目中,咱们常常须要输入申请参数,响应后果,办法耗时,对立的权限校验等。

本文首先为大家介绍 HTTP 申请中三种常见的拦挡实现,并且比拟一下其中的差别。

(1)基于 Aspect 的拦挡形式

(2)基于 HandlerInterceptor 的拦挡形式

(3)基于 ResponseBodyAdvice 的拦挡形式

举荐浏览:

对立日志框架: https://github.com/houbb/auto-log

springboot 入门案例

为了便于大家学习,咱们首先从最根本的 springboot 例子讲起。

maven 引入

引入必须的 jar 包。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

启动类

实现最简略的启动类。

@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);
    }

}

定义 Controller

为了演示不便,咱们首先实现一个简略的 controller。

@RestController
public class IndexController {@RequestMapping("/index")
    public AsyncResp index() {AsyncResp asyncResp = new AsyncResp();
        asyncResp.setResult("ok");
        asyncResp.setRespCode("00");
        asyncResp.setRespDesc("胜利");

        System.out.println("IndexController#index:" + asyncResp);
        return asyncResp;
    }

}

其中 AsyncResp 的定义如下:

public class AsyncResp {

    private String respCode;

    private String respDesc;

    private String result;


    // getter & setter & toString()}

拦挡形式定义

基于 Aspect

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 *
 * @author binbin.hou
 * @since 1.0.0
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class AspectLogInterceptor {

    /**
     * 日志实例
     * @since 1.0.0
     */
    private static final Logger LOG = LoggerFactory.getLogger(AspectLogInterceptor.class);

    /**
     * 拦挡 controller 下所有的 public 办法
     */
    @Pointcut("execution(public * com.github.houbb.springboot.learn.aspect.controller..*(..))")
    public void pointCut() {//}

    /**
     * 拦挡解决
     *
     * @param point point 信息
     * @return result
     * @throws Throwable if any
     */
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        try {
            //1. 设置 MDC

            // 获取以后拦挡的办法签名
            String signatureShortStr = point.getSignature().toShortString();
            //2. 打印入参信息
            Object[] args = point.getArgs();
            LOG.info("{} 参数: {}", signatureShortStr, Arrays.toString(args));

            //3. 打印后果
            Object result = point.proceed();
            LOG.info("{} 后果: {}", signatureShortStr, result);
            return result;
        } finally {// 移除 mdc}
    }

}

这种实现的长处是比拟通用,能够联合注解实现更加灵便弱小的性能。

是集体十分喜爱的一种形式。

主要用途:

(1)日志的出参 / 入参

(2)对立设置 TraceId

(3)办法的调用耗时统计

基于 HandlerInterceptor

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author binbin.hou
 * @since 1.0.0
 */
@Component
public class LogHandlerInterceptor implements HandlerInterceptor {private Logger logger = LoggerFactory.getLogger(LogHandlerInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 对立的权限校验、路由等
        logger.info("LogHandlerInterceptor#preHandle 申请地址:{}", request.getRequestURI());

        if (request.getDispatcherType().equals(DispatcherType.ASYNC)) {return true;}

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {logger.info("LogHandlerInterceptor#postHandle 调用");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}}

而后须要指定对应的 url 和拦挡形式之间的关系才会失效:

import com.github.houbb.springboot.learn.aspect.aspect.LogHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * spring mvc 配置
 * @since 1.0.0
 */
@Configuration
public class SpringMvcConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private LogHandlerInterceptor logHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(logHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/version");
        super.addInterceptors(registry);
    }

}

这种形式的长处就是能够依据 url 灵便指定不同的拦挡形式。

毛病是次要用于 Controller 层。

基于 ResponseBodyAdvice

此接口有 beforeBodyWrite 办法,参数 body 是响应对象 response 中的响应体,那么咱们就能够用此办法来对响应体做一些对立的操作。

比方加密,签名等。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author binbin.hou
 * @since 1.0.0
 */
@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice<Object> {

    /**
     * 日志实例
     * @since 1.0.0
     */
    private static final Logger LOG = LoggerFactory.getLogger(MyResponseBodyAdvice.class);

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        // 这个中央如果返回 false, 不会执行 beforeBodyWrite 办法
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object resp, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {String uri = serverHttpRequest.getURI().getPath();
        LOG.info("MyResponseBodyAdvice#beforeBodyWrite 申请地址:{}", uri);

        ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) serverHttpRequest;
        HttpServletRequest servletRequest = servletServerHttpRequest.getServletRequest();

        // 能够做对立的拦挡形式解决

        // 能够对后果做动静批改等
        LOG.info("MyResponseBodyAdvice#beforeBodyWrite 响应后果:{}", resp);
        return resp;
    }

}

测试

咱们启动利用,页面拜访:

http://localhost:18080/index

页面响应:

{"respCode":"00","respDesc":"胜利","result":"ok"}

后端日志:

c.g.h.s.l.a.a.LogHandlerInterceptor      : LogHandlerInterceptor#preHandle 申请地址:/index
c.g.h.s.l.a.aspect.AspectLogInterceptor  : IndexController.index() 参数: []
IndexController#index:AsyncResp{respCode='00', respDesc='胜利', result='ok'}
c.g.h.s.l.a.aspect.AspectLogInterceptor  : IndexController.index() 后果: AsyncResp{respCode='00', respDesc='胜利', result='ok'}
c.g.h.s.l.a.aspect.MyResponseBodyAdvice  : MyResponseBodyAdvice#beforeBodyWrite 申请地址:/index
c.g.h.s.l.a.aspect.MyResponseBodyAdvice  : MyResponseBodyAdvice#beforeBodyWrite 响应后果:AsyncResp{respCode='00', respDesc='胜利', result='ok'}
c.g.h.s.l.a.a.LogHandlerInterceptor      : LogHandlerInterceptor#postHandle 调用 

这里执行的先后顺序也比拟明确,此处不再赘述。

异步执行

当然,如果只是下面这些内容,并不是本篇文章的重点。

接下来,咱们一起来看下,如果引入了异步执行会怎么样。

定义异步线程池

springboot 中定义异步线程池,非常简单。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 申请异步解决配置
 *
 * @author binbin.hou
 */
@Configuration
@EnableAsync
public class SpringAsyncConfig {@Bean(name = "asyncPoolTaskExecutor")
    public AsyncTaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(10);
        executor.setCorePoolSize(10);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }

}

异步执行的 Controller

@RestController
public class MyAsyncController extends BaseAsyncController<String> {

    @Override
    protected String process(HttpServletRequest request) {return "ok";}

    @RequestMapping("/async")
    public AsyncResp hello(HttpServletRequest request) {AsyncResp resp = super.execute(request);

        System.out.println("Controller#async 后果:" + resp);
        return resp;
    }

}

其中 BaseAsyncController 的实现如下:

@RestController
public abstract class BaseAsyncController<T> {protected abstract T process(HttpServletRequest request);

    @Autowired
    private AsyncTaskExecutor taskExecutor;

    protected AsyncResp execute(HttpServletRequest request) {
        // 异步响应后果
        AsyncResp resp = new AsyncResp();
        try {taskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {T result = process(request);

                        resp.setRespCode("00");
                        resp.setRespDesc("胜利");
                        resp.setResult(result.toString());

                    } catch (Exception exception) {resp.setRespCode("98");
                        resp.setRespDesc("工作异样");
                    }
                }
            });
        } catch (TaskRejectedException e) {resp.setRespCode("99");
            resp.setRespDesc("工作回绝");
        }

        return resp;
    }

}

execute 的实现也比较简单:

(1)主线程创立一个 AsyncResp,用于返回。

(2)线程池异步执行具体的子类办法,并且设置对应的值。

思考

接下来,问大家一个问题。

如果咱们申请 http://localhost:18080/async,那么:

(1)页面失去的返回值是什么?

(2)Aspect 日志输入的返回值是?

(3)ResponseBodyAdvice 日志输入的返回值是什么?

你能够在这里略微停一下,记录下你的答案。

测试

咱们页面申请 http://localhost:18080/async。

页面响应如下:

{"respCode":"00","respDesc":"胜利","result":"ok"}

后端的日志:

c.g.h.s.l.a.a.LogHandlerInterceptor      : LogHandlerInterceptor#preHandle 申请地址:/async
c.g.h.s.l.a.aspect.AspectLogInterceptor  : MyAsyncController.hello(..) 参数: [org.apache.catalina.connector.RequestFacade@7e931750]
Controller#async 后果:AsyncResp{respCode='null', respDesc='null', result='null'}
c.g.h.s.l.a.aspect.AspectLogInterceptor  : MyAsyncController.hello(..) 后果: AsyncResp{respCode='null', respDesc='null', result='null'}
c.g.h.s.l.a.aspect.MyResponseBodyAdvice  : MyResponseBodyAdvice#beforeBodyWrite 申请地址:/async
c.g.h.s.l.a.aspect.MyResponseBodyAdvice  : MyResponseBodyAdvice#beforeBodyWrite 响应后果:AsyncResp{respCode='00', respDesc='胜利', result='ok'}
c.g.h.s.l.a.a.LogHandlerInterceptor      : LogHandlerInterceptor#postHandle 调用 

比照一下,能够发现咱们下面问题的答案:

(1)页面失去的返回值是什么?

{"respCode":"00","respDesc":"胜利","result":"ok"}

能够获取到异步执行实现的后果。

(2)Aspect 日志输入的返回值是?

AsyncResp{respCode='null', respDesc='null', result='null'}

无奈获取异步后果。

(3)ResponseBodyAdvice 日志输入的返回值是什么?

AsyncResp{respCode='00', respDesc='胜利', result='ok'}

能够获取到异步执行实现的后果。

这个看起来有些奇怪,实质上起因是什么呢?又怎么验证呢?

异步执行

起因

实质上,异步执行和 spring 自身的机制关系不大。

只不过是异步执行的办法自身须要工夫,拦挡形式越靠后,如果异步执行完了,刚好就能够获取到对应的信息而已。

验证形式

如何验证这个猜测呢?

咱们在 process 中增加一个 sleep 即可。

代码调整

  • BaseAsyncController.java

execute 中增加一些执行的日志信息,便于查看工夫。

taskExecutor.execute(new Runnable() {
    @Override
    public void run() {
        try {logger.info("AsyncResp#execute 异步开始执行。");
            T result = process(request);
            resp.setRespCode("00");
            resp.setRespDesc("胜利");
            resp.setResult(result.toString());
            logger.info("AsyncResp#execute 异步实现执行。");
        } catch (Exception exception) {resp.setRespCode("98");
            resp.setRespDesc("工作异样");
        }
    }
});
  • MyAsyncController.java

执行时增加沉睡工夫。

@Override
protected String process(HttpServletRequest request) {
    try {TimeUnit.SECONDS.sleep(5);
        return "ok";
    } catch (InterruptedException e) {return "error";}
}

测试

页面拜访 http://localhost:18080/async

页面返回如下:

{"respCode":null,"respDesc":null,"result":null}

对应的日志如下:

2021-07-10 09:16:08.661  INFO 11008 --- [io-18080-exec-1] c.g.h.s.l.a.a.LogHandlerInterceptor      : LogHandlerInterceptor#preHandle 申请地址:/async
2021-07-10 09:16:08.685  INFO 11008 --- [io-18080-exec-1] c.g.h.s.l.a.aspect.AspectLogInterceptor  : MyAsyncController.hello(..) 参数: [org.apache.catalina.connector.RequestFacade@1d491e0]
Controller#async 后果:AsyncResp{respCode='null', respDesc='null', result='null'}
2021-07-10 09:16:08.722  INFO 11008 --- [io-18080-exec-1] c.g.h.s.l.a.aspect.AspectLogInterceptor  : MyAsyncController.hello(..) 后果: AsyncResp{respCode='null', respDesc='null', result='null'}
2021-07-10 09:16:08.722  INFO 11008 --- [lTaskExecutor-1] c.g.h.s.l.a.c.BaseAsyncController        : AsyncResp#execute 异步开始执行。2021-07-10 09:16:08.777  INFO 11008 --- [io-18080-exec-1] c.g.h.s.l.a.aspect.MyResponseBodyAdvice  : MyResponseBodyAdvice#beforeBodyWrite 申请地址:/async
2021-07-10 09:16:08.777  INFO 11008 --- [io-18080-exec-1] c.g.h.s.l.a.aspect.MyResponseBodyAdvice  : MyResponseBodyAdvice#beforeBodyWrite 响应后果:AsyncResp{respCode='null', respDesc='null', result='null'}
2021-07-10 09:16:08.797  INFO 11008 --- [io-18080-exec-1] c.g.h.s.l.a.a.LogHandlerInterceptor      : LogHandlerInterceptor#postHandle 调用
2021-07-10 09:16:13.729  INFO 11008 --- [lTaskExecutor-1] c.g.h.s.l.a.c.BaseAsyncController        : AsyncResp#execute 异步实现执行。

能够发现 spring 自身仍然依照失常的流程执行,因为 process 的执行工夫过长,导致三种拦挡形式都是无奈获取异步内容。

反思

写到这里,本人的播种还是不少。

(1)拦截器的叫法问题

平时会习惯的叫日志拦截器之类的,所以一开始题目应用的是 3 种拦截器,诚然,谨严的说并不能将这些一概而论。

否则,就如评论区所言,filter 也能够称为拦截器了。

所以将拦截器对立修改为拦挡形式。

(2)对常识的了解问题

第一次实现的时候,因为 process 工夫太短,让人产生误以为 spring 会有非凡的解决机制。

学习自身还是要谨严一些,所以本文从新做了修改。

小结

心愿本文对你有帮忙,如果有其余想法的话,也能够评论区和大家分享哦。

各位极客的点赞珍藏转发,是老马继续写作的最大能源!

我是老马,期待与你的下次重逢。

正文完
 0