1. Import POM dependencies

        <! -- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.5</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>
        <! -- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
Copy the code

2. Create the log entity class

@Builder
@Data
public class FlowApiLog implements Serializable {

    private String id;

    private String url;

    private String createTime;

    private String source;      // source, reserved for distributed use

    private String ipaddr;

}
Copy the code

3. Create a slice

@Slf4j
@Aspect
@Component
public class FlowApiAspect {
    /* Time format */
    private static String YMDMHS = "yyyy-MM-dd HH:mm:ss";

    @Autowired
    private ApiLogService apiLogService;

    // Distributed reservation
   /* @Value(value = "${spring.application.name}") private String serverName; * /

    @Pointcut(value = "@annotation(org.springframework.web.bind.annotation.GetMapping) ||" + "@annotation(org.springframework.web.bind.annotation.PostMapping) ||" + "@annotation(org.springframework.web.bind.annotation.PutMapping) ||" + "@annotation(org.springframework.web.bind.annotation.DeleteMapping) ||" + "@annotation(org.springframework.web.bind.annotation.RequestMapping) ")
    public void  flowApi(a){}// Wraparound implementation
    @Around(value = "flowApi()")
    public Object flowControl(ProceedingJoinPoint joinPoint){
        Object proceed = null;
        long time = System.currentTimeMillis();
        try {
            proceed = joinPoint.proceed();
            time = System.currentTimeMillis() - time;
            log.info("Method execution time =" + time);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            // after the method is executed
            saveFlowApi(joinPoint);
        }
        return proceed;
    }

    private void saveFlowApi(ProceedingJoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        
        String re = "Prefix (local | deploy online prefix)";  // rejudge the environment
        / / get the URL
        String url = request.getRequestURL().toString().replaceAll(re,"");
        // Get the IP of the caller
// String ip = IPUtils.getRemoteHost(request);
        // The time of the call
        String date = DateFormatUtils.format(new Date(), YMDMHS);

        FlowApiLog flowApiLog = FlowApiLog.builder()
                .id(UUID.randomUUID().toString().replace("-".""))
                .url(url)
                .createTime(date)
//.source(serverName) // Distributed reservation field
                .ipaddr(request.getRemoteAddr()).build();
        // It can be persisted here
        apiLogService.insertApiLog(flowApiLog);
        log.info("Interface call information {}",flowApiLog); }}Copy the code