SpringCloud源码探析(十二)-基于SpringBoot开发自定义中间件

1.概述

中间件是一种介于操作系统和应用软件之间,为应用软件提供服务功能的软件,按功能划分有消息中间件(Kafka、RocketMQ)、通信中间件(RPC通信中间件,dubbo等),应用服务器等。中间件屏蔽了底层操作系统的复杂性,让开发工程师可以把更多的专注力放在业务系统上,能够有效提高开发人员效率。本文主要分析利用springboot开发自定义日志中间件,通过此中间件能够打印请求入参及返回结果,帮助大家更好地理解利用springboot如何开发中间件。

2.自定义AOP日志中间件

利用springboot开发中间件主要包含以下几个步骤:

1.创建自定义的starter项目
2.定义Starter需要的配置类
3.编写业务功能
4.编写自动配置类
5.编写spring.factories文件加载自动配置类
6.打包安装

本文将会按照上述步骤,以自定义AOP日志中间件为例进行分析。

2.1 需求背景

在利用spring开发的web应用中,请求会从controller进入并经过多次流转,最后返回结果。在这过程中可能会打印大量日志,进行问题排查时需要耗费大量时间和精力,为了能够提升排查问题效率,可以将每一次的请求进入和结束进行标识,打印请求IP、入参以及返回结果,这样在排查问题时能够快速定位请求内容及结果。
所以,基于上述背景,开发一个利用AOP对于入口Controller文件进行拦截处理,打印入参及返回结果等信息,所有利用spring开发的web应用能够直接引用此中间件,直接实现入口日志打印。

2.2 方案设计

整体设计方案如下图所示:
在这里插入图片描述
上述设计图主要包括以下内容:

1.SpringBoot Starter 的实现会自动加载配置,通过配置文件确定是否生成SpringAopLogAspect Bean;
2.在SpringAopLogAspect定义切面进行入口日志打印输出。

2.3 代码实现

spring-aop-log-starter类图关系如下图所示:在这里插入图片描述

  • AopLogProperties:属性配置类,获取日志打印开关属性,若为true,开启打印;
  • AopLogConfig:配置类,依赖AopLogProperties确定是否生成SpringAopLogAspect;
  • SpringAopLogAspect:业务逻辑类,拦截Controller并进行日志打印。

2.3.1 pom文件

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.3.2 AopLogProperties

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @Author: Marinc
 * @CreateTime: 2023-12-18  14:29
 * @Description: TODO
 * @Version: 1.0
 */
@ConfigurationProperties(prefix = "aop.log")
public class AopLogProperties {
    
    

    private boolean enable;

    public AopLogProperties() {
    
    
    }

    public boolean isEnable() {
    
    
        return enable;
    }

    public void setEnable(boolean enable) {
    
    
        this.enable = enable;
    }

}

@ConfigurationProperties,用于创建指定前缀( prefix = “aop.log”)的自定义配置信息,这样就在 yml 或者 properties 中读取到我们自己设定的配置信息。

2.3.3 AopLogConfig

import com.eckey.lab.aop.SpringAopLogAspect;
import com.eckey.lab.properties.AopLogProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: Marinc
 * @CreateTime: 2023-12-18  15:16
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
@EnableConfigurationProperties({
    
    AopLogProperties.class})
@ConditionalOnProperty(prefix = "aop.log", value = "enable", havingValue = "true")
public class AopLogConfig {
    
    

    @Bean
    @ConditionalOnMissingBean
    public SpringAopLogAspect springLogAspect() {
    
    
        return new SpringAopLogAspect();
    }

}

@Configuration是定义一个配置类;
@EnableConfigurationProperties({AopLogProperties.class})注解的作用是让@ConfigurationProperties注解生效,如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的;
@ConditionalOnProperty(prefix = “aop.log”,value = “enable”,havingValue = “true”)会将配置文件中的值和havingValue的值对比,如果一样则加载Bean;
@ConditionalOnMissingBean仅仅在当前上下文中不存在某个对象时,才会实例化一个 Bean。

2.3.4 SpringAopLogAspect

import com.eckey.lab.utils.IpInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Author: Marinc
 * @CreateTime: 2023-12-18  14:33
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Aspect
@Component
public class SpringAopLogAspect {
    
    

    @Autowired
    private IpInfoUtil ipInfoUtil;

    @Pointcut("execution(* *..*Controller.*(..))")
    public void springAopLog(){
    
    }


    @Before("springAopLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    
    
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        log.info("------------------请求开始------------------");
        // 记录下请求内容
        log.info("请求路径:{}", request.getRequestURL().toString());
        log.info("客户端IP :{}" , ipInfoUtil.getIpAddr(request));
        log.info("参数值 :{}", Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(returning = "res", pointcut = "springAopLog()")
    public void doAfterReturning(Object res) throws Throwable {
    
    
        // 处理完请求,返回内容
        log.info("返回值 : {}" , res);
        log.info("------------------请求结束------------------");
    }

}

注解@Aspect定义类为切面类;
@Component 注解,将类生成为 Bean对象;
@Pointcut(“execution(* *…Controller.(…))”),定义切点。在Pointcut中提供了多种切点寻找方式(指定方法名称、范围筛选表达式、自定义注解等),一般在中间件开发中,自定义注解的使用比较多;
@Before(“springAopLog()”),可以理解为是对方法增强的织入动作,在方法执行前先执行;
@AfterReturning(returning = “res”, pointcut = “springAopLog()”)被代理的方法执行完成之后要执行的代码。

2.3.4 spring.factories

1.在resources下新建META-INF文件夹,然后创建spring.factories文件
2.在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.eckey.lab.config.AopLogConfig

2.3.5 测试结果

1.引入pom

    <dependency>
        <groupId>com.eckey.lab</groupId>
        <artifactId>spring-aop-log-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

2.在properties中配置

#配置切面打印日志
aop.log.enable=true

3.测试验证
在订单服务中访问地址:http://127.0.0.1:8082/order/test,结果如下:
在这里插入图片描述
在这里插入图片描述

3.小结

1.本文初步分析了一个基于切面和SpringBoot结合开发的中间件,包括了自定义配置如何设置、SpringBoot如何加载和生成Bean,以及切面拦截后的处理;
2.在切面拦截的逻辑相对比较简单,仅仅时拦截并打印了一些信息,这里可以进行拓展一下,通过自定义注解,配置在需要统计的方法上,统计一些关键信息,进行统计汇总,具体可以看第4节参考文献进行发散;
3.基于springboot开发中间件是一项基本技能,可以基于自己日常中常用的场景(短信发送、邮件发送等),基于不同场景多动手实践。

4.参考文献

1.https://blog.csdn.net/qq_33479841/article/details/116306864
2.https://zhuanlan.zhihu.com/p/642035645

5.附录

1.https://gitee.com/Marinc/nacos/tree/master/spring-aop-log-starter

猜你喜欢

转载自blog.csdn.net/qq_33479841/article/details/135066076