做网站的大骗子,俄语免费网站制作,深圳 建网站,山东网站推广有限公司1.概述
中间件是一种介于操作系统和应用软件之间#xff0c;为应用软件提供服务功能的软件#xff0c;按功能划分有消息中间件#xff08;Kafka、RocketMQ#xff09;、通信中间件#xff08;RPC通信中间件#xff0c;dubbo等#xff09;#xff0c;应用服务器等。中间…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确定是否生成SpringAopLogAspectSpringAopLogAspect业务逻辑类拦截Controller并进行日志打印。 2.3.1 pom文件 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationclassifierexec/classifier/configuration/plugin/plugins/build2.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 {BeanConditionalOnMissingBeanpublic 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 {Autowiredprivate 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.EnableAutoConfigurationcom.eckey.lab.config.AopLogConfig2.3.5 测试结果
1.引入pom dependencygroupIdcom.eckey.lab/groupIdartifactIdspring-aop-log-starter/artifactIdversion1.0-SNAPSHOT/version/dependency2.在properties中配置
#配置切面打印日志
aop.log.enabletrue3.测试验证 在订单服务中访问地址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