• SpringBoot + 自定义注解 + AOP 高级玩法打造通用开关


    前言

    最近在工作中迁移代码的时候发现了以前自己写的一个通用开关实现,发现挺不错,特地拿出来分享给大家。

    为了有良好的演示效果,我特地重新建了一个项目,把核心代码提炼出来加上了更多注释说明,希望xdm喜欢。

    案例

    1、项目结构

    image

    2、引入依赖
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-aopartifactId>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <optional>trueoptional>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    3、yml配置

    连接Redis的配置修改成自己的

    server:
      port: 8888
    
    spring:
      redis:
        database: 6
        host: xx.xx.xx.xx
        port: 6379
        password: 123456
        jedis:
          pool:
            max-active: 100
            max-wait: -1ms
            max-idle: 50
            min-idle: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    4、自定义注解

    这里稍微说明下,定义了一个key对应不同功效的开关,定义了一个val作为开关是否打开的标识,以及一个message作为消息提示。

    package com.example.commonswitch.annotation;
    
    import com.example.commonswitch.constant.Constant;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * 

    * 通用开关注解 *

    * * @author 程序员济癫 * @since 2023-10-16 17:38 */
    @Target({ElementType.METHOD}) // 作用在方法上 @Retention(RetentionPolicy.RUNTIME) // 运行时起作用 public @interface ServiceSwitch { /** * 业务开关的key(不同key代表不同功效的开关) * {@link Constant.ConfigCode} */ String switchKey(); // 开关,0:关(拒绝服务并给出提示),1:开(放行) String switchVal() default "0"; // 提示信息,默认值可在使用注解时自行定义。 String message() default "当前请求人数过多,请稍后重试。"; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    5、定义常量

    主要用来存放各种开关的key

    package com.example.commonswitch.constant;
    
    /**
     * 

    * 常量类 *

    * * @author 程序员济癫 * @since 2023-10-16 17:45 */
    public class Constant { // .... 其他业务相关的常量 .... // 配置相关的常量 public static class ConfigCode { // 挂号支付开关(0:关,1:开) public static final String REG_PAY_SWITCH = "reg_pay_switch"; // 门诊支付开关(0:关,1:开) public static final String CLINIC_PAY_SWITCH = "clinic_pay_switch"; // 其他业务相关的配置常量 // .... } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    6、AOP核心实现

    核心实现中我专门加了详细的注释说明,保证大家一看就懂,而且把查询开关的方式列举出来供大家自己选择。

    package com.example.commonswitch.aop;
    
    import com.example.commonswitch.annotation.ServiceSwitch;
    import com.example.commonswitch.constant.Constant;
    import com.example.commonswitch.exception.BusinessException;
    import com.example.commonswitch.util.Result;
    import lombok.AllArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    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.aspectj.lang.reflect.MethodSignature;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    
    /**
     * 

    * 开关实现的切面类 *

    * * @author 程序员济癫 * @since 2023-10-16 17:56 */
    @Aspect @Component @Slf4j @AllArgsConstructor public class ServiceSwitchAOP { private final StringRedisTemplate redisTemplate; /** * 定义切点,使用了@ServiceSwitch注解的类或方法都拦截 */ @Pointcut("@annotation(com.example.commonswitch.annotation.ServiceSwitch)") public void pointcut() { } @Around("pointcut()") public Object around(ProceedingJoinPoint point) { // 获取被代理的方法的参数 Object[] args = point.getArgs(); // 获取被代理的对象 Object target = point.getTarget(); // 获取通知签名 MethodSignature signature = (MethodSignature) point.getSignature(); try { // 获取被代理的方法 Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes()); // 获取方法上的注解 ServiceSwitch annotation = method.getAnnotation(ServiceSwitch.class); // 核心业务逻辑 if (annotation != null) { String switchKey = annotation.switchKey(); String switchVal = annotation.switchVal(); String message = annotation.message(); /* 获取配置项说明 这里有两种方式:1、配置加在Redis,查询时从Redis获取; 2、配置加在数据库,查询时从表获取。(MySQL单表查询其实很快,配置表其实也没多少数据) 我在工作中的做法:直接放到数据库,但是获取配置项的方法用SpringCache缓存, 然后在后台管理中操作配置项,变更时清理缓存即可。 我这么做就是结合了上面两种各自的优点,因为项目中配置一般都是用后台管理来操作的, 查表当然更舒适,同时加上缓存提高查询性能。 */ // 下面这块查询配置项,大家可以自行接入并修改。 // 数据库这么查询:String configVal = systemConfigService.getConfigByKey(switchKey); // 这里我直接从redis中取,使用中大家可以按照意愿自行修改。 String configVal = redisTemplate.opsForValue().get(Constant.ConfigCode.REG_PAY_SWITCH); if (switchVal.equals(configVal)) { // 开关打开,则返回提示。 return new Result<>(HttpStatus.FORBIDDEN.value(), message); } } // 放行 return point.proceed(args); } catch (Throwable e) { throw new BusinessException(e.getMessage(), e); } } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    7、使用注解

    我们定义一个服务来使用这个开关,我设定了一个场景是挂号下单,也就是把开关用在支付业务这里。

    因为支付场景在线上有可能出现未知问题,比如第三方rpc调用超时或不响应,或者对方业务出现缺陷,导致我方不断出现长款,那么我们此时立马操作后台将支付开关关掉,能最大程度止损。

    package com.example.commonswitch.service;
    
    import com.example.commonswitch.annotation.ServiceSwitch;
    import com.example.commonswitch.constant.Constant;
    import com.example.commonswitch.util.Result;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Service;
    
    /**
     * 

    * 挂号服务 *

    * * @author 程序员济癫 * @since 2023-10-16 18:48 */
    @Service public class RegService { /** * 挂号下单 */ @ServiceSwitch(switchKey = Constant.ConfigCode.REG_PAY_SWITCH) public Result createOrder() { // 具体下单业务逻辑省略.... return new Result(HttpStatus.OK.value(), "挂号下单成功"); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    8、测试效果

    好了,接下来我们定义一个接口来测试效果如何。

    package com.example.commonswitch.controller;
    
    import com.example.commonswitch.service.RegService;
    import com.example.commonswitch.util.Result;
    import lombok.AllArgsConstructor;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 

    * 挂号接口 *

    * * @author 程序员济癫 * @since 2023-10-16 18:51 */
    @RestController @RequestMapping("/api/reg") @AllArgsConstructor public class RegController { private final RegService regService; @PostMapping("/createOrder") public Result createOrder() { return regService.createOrder(); } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    Redis中把开关加上去(实际工作中是后台添加的哈),此时开关是1,表示开关打开。

    image

    调接口,可以发现,目前是正常的业务流程

    image

    接下来,我们假定线上出了问题,要立马将开关关闭。(还是操作Redis,实际工作中是后台直接关掉哈)

    我们将其改为0,也就是表示开关给关闭。

    image

    看效果,OK,没问题,是我们预想的结果。

    image

    这里要记住一点,提示可以自定义,但是不要直接返回给用户系统异常,给一个友好提示即可。

    下载

    Gitee:https://gitee.com/fangfuji/java-share

    找到相关的博文名称,然后下载到本地IDEA运行即可。

    总结

    文中使用到的技术主要是这些:SpringBoot、自定义注解、AOP、Redis、Lombok。

    其中,自定义注解和AOP是核心实现,Redis是可选项,你也可以接入到数据库。

    lombok的话大家可以仔细看代码,我用它帮助省略了所有@Autowaird,这样就使用了官方及IDEA推荐的构造器注入方式。

    好了,今天的小案例,xdm学会了吗。


    如果喜欢,请点赞+关注↓↓↓,持续分享干货哦!

  • 相关阅读:
    基础设施即代码(IAC),Zalando Postgres Operator UI 入门
    微信“刷掌支付”上线,扫手就可以付款!你知道怎么开通了吗?
    对示例程序spinner_asyncio.py进行修改使其能运行
    时钟有关概念汇总
    微服务15:微服务治理之超时
    vue中如何动态绑定高度
    new Vue的时候到底做了什么
    深度剖析Gateway在微服务治理中的关键角色
    【快应用】通知消息定时提醒
    Python编程基础:函数的使用
  • 原文地址:https://blog.csdn.net/xiangyangsanren/article/details/133872407