From 8dede2325881cb5da403be7eb6b2888f056e8345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Sat, 11 Nov 2023 11:14:08 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CommonSwitch/.idea/.gitignore | 8 ++ CommonSwitch/.idea/CommonSwitch.iml | 6 ++ CommonSwitch/.idea/compiler.xml | 18 ++++ CommonSwitch/.idea/encodings.xml | 6 ++ CommonSwitch/.idea/jarRepositories.xml | 20 +++++ CommonSwitch/.idea/misc.xml | 14 +++ CommonSwitch/.idea/modules.xml | 8 ++ CommonSwitch/CommonSwitch/.gitignore | 33 +++++++ CommonSwitch/CommonSwitch/pom.xml | 64 ++++++++++++++ .../commonswitch/CommonSwitchApplication.java | 13 +++ .../annotation/ServiceSwitch.java | 28 ++++++ .../commonswitch/aop/ServiceSwitchAOP.java | 86 +++++++++++++++++++ .../woniu/commonswitch/constant/Constant.java | 30 +++++++ .../controller/RegController.java | 28 ++++++ .../exception/BusinessException.java | 22 +++++ .../commonswitch/service/RegService.java | 22 +++++ .../com/woniu/commonswitch/util/Result.java | 48 +++++++++++ .../src/main/resources/application.yml | 14 +++ .../CommonSwitchApplicationTests.java | 13 +++ 19 files changed, 481 insertions(+) create mode 100644 CommonSwitch/.idea/.gitignore create mode 100644 CommonSwitch/.idea/CommonSwitch.iml create mode 100644 CommonSwitch/.idea/compiler.xml create mode 100644 CommonSwitch/.idea/encodings.xml create mode 100644 CommonSwitch/.idea/jarRepositories.xml create mode 100644 CommonSwitch/.idea/misc.xml create mode 100644 CommonSwitch/.idea/modules.xml create mode 100644 CommonSwitch/CommonSwitch/.gitignore create mode 100644 CommonSwitch/CommonSwitch/pom.xml create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/CommonSwitchApplication.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/annotation/ServiceSwitch.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/aop/ServiceSwitchAOP.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/constant/Constant.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/controller/RegController.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/exception/BusinessException.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/service/RegService.java create mode 100644 CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/util/Result.java create mode 100644 CommonSwitch/CommonSwitch/src/main/resources/application.yml create mode 100644 CommonSwitch/CommonSwitch/src/test/java/com/woniu/commonswitch/CommonSwitchApplicationTests.java diff --git a/CommonSwitch/.idea/.gitignore b/CommonSwitch/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/CommonSwitch/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/CommonSwitch/.idea/CommonSwitch.iml b/CommonSwitch/.idea/CommonSwitch.iml new file mode 100644 index 0000000..6054576 --- /dev/null +++ b/CommonSwitch/.idea/CommonSwitch.iml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CommonSwitch/.idea/compiler.xml b/CommonSwitch/.idea/compiler.xml new file mode 100644 index 0000000..643e1c0 --- /dev/null +++ b/CommonSwitch/.idea/compiler.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CommonSwitch/.idea/encodings.xml b/CommonSwitch/.idea/encodings.xml new file mode 100644 index 0000000..85a5ec2 --- /dev/null +++ b/CommonSwitch/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CommonSwitch/.idea/jarRepositories.xml b/CommonSwitch/.idea/jarRepositories.xml new file mode 100644 index 0000000..e6b00eb --- /dev/null +++ b/CommonSwitch/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/CommonSwitch/.idea/misc.xml b/CommonSwitch/.idea/misc.xml new file mode 100644 index 0000000..7af4467 --- /dev/null +++ b/CommonSwitch/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/CommonSwitch/.idea/modules.xml b/CommonSwitch/.idea/modules.xml new file mode 100644 index 0000000..9623e60 --- /dev/null +++ b/CommonSwitch/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/CommonSwitch/CommonSwitch/.gitignore b/CommonSwitch/CommonSwitch/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/CommonSwitch/CommonSwitch/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/CommonSwitch/CommonSwitch/pom.xml b/CommonSwitch/CommonSwitch/pom.xml new file mode 100644 index 0000000..fc6a40d --- /dev/null +++ b/CommonSwitch/CommonSwitch/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.16 + + + com.example + CommonSwitch + 0.0.1-SNAPSHOT + CommonSwitch + CommonSwitch + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/CommonSwitchApplication.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/CommonSwitchApplication.java new file mode 100644 index 0000000..33a8726 --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/CommonSwitchApplication.java @@ -0,0 +1,13 @@ +package com.woniu.commonswitch; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CommonSwitchApplication { + + public static void main(String[] args) { + SpringApplication.run(CommonSwitchApplication.class, args); + } + +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/annotation/ServiceSwitch.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/annotation/ServiceSwitch.java new file mode 100644 index 0000000..22debf0 --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/annotation/ServiceSwitch.java @@ -0,0 +1,28 @@ +package com.woniu.commonswitch.annotation; + +import com.woniu.commonswitch.constant.Constant; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + *

+ * 通用开关注解 0 关 1 开 + * 程序员蜗牛 + *

+ */ +@Target({ElementType.METHOD}) // 作用在方法上 +@Retention(RetentionPolicy.RUNTIME) // 运行时起作用 +public @interface ServiceSwitch { + + /** + * 业务开关的key(不同key代表不同功效的开关) + * {@link Constant.ConfigCode} + */ + String switchKey(); + + // 提示信息,默认值可在使用注解时自行定义。 + String message() default "当前请求人数过多,请稍后重试。"; +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/aop/ServiceSwitchAOP.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/aop/ServiceSwitchAOP.java new file mode 100644 index 0000000..74a2e43 --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/aop/ServiceSwitchAOP.java @@ -0,0 +1,86 @@ +package com.woniu.commonswitch.aop; + +import com.woniu.commonswitch.annotation.ServiceSwitch; +import com.woniu.commonswitch.constant.Constant; +import com.woniu.commonswitch.exception.BusinessException; +import com.woniu.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; + +/** + *

+ * 教你一招 SpringBoot + 自定义注解 + AOP 打造通用开关 + * 程序员蜗牛 + *

+ */ +@Aspect +@Component +@Slf4j +@AllArgsConstructor +public class ServiceSwitchAOP { + + private final StringRedisTemplate redisTemplate; + + /** + * 定义切点,使用了@ServiceSwitch注解的类或方法都拦截 + */ + @Pointcut("@annotation(com.woniu.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 message = annotation.message(); + + /* + 获取配置项说明 + 这里有两种方式:1、配置加在Redis,查询时从Redis获取; + 2、配置加在数据库,查询时从表获取。(MySQL单表查询其实很快,配置表其实也没多少数据) + 我在工作中的做法: 直接放到数据字典里 同时加上缓存提高查询性能。 + */ + + // 这里我直接从redis中取,使用中大家可以按照意愿自行修改。 + String configVal = redisTemplate.opsForValue().get(switchKey); + if (Constant.SWITCHCLOSE.equals(configVal)) { + // 开关关闭,则返回提示。 + return new Result<>(HttpStatus.FORBIDDEN.value(), message); + } + } + + // 放行 + return point.proceed(args); + + } catch (Throwable e) { + throw new BusinessException(e.getMessage(), e); + } + } +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/constant/Constant.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/constant/Constant.java new file mode 100644 index 0000000..5b645cb --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/constant/Constant.java @@ -0,0 +1,30 @@ +package com.woniu.commonswitch.constant; + +/** + *

+ * 常量类 + * 程序员蜗牛 + *

+ * + */ +public class Constant { + + /** + * (0:关,1:开) + */ + public static final String SWITCHCLOSE = "0"; + + // .... 其他业务相关的常量 .... + + // 配置相关的常量 + 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"; + + // 其他业务相关的配置常量 + // .... + } +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/controller/RegController.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/controller/RegController.java new file mode 100644 index 0000000..d49df9f --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/controller/RegController.java @@ -0,0 +1,28 @@ +package com.woniu.commonswitch.controller; + +import com.woniu.commonswitch.service.RegService; +import com.woniu.commonswitch.util.Result; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + *

+ * 程序员蜗牛 + * 教你一招 SpringBoot + 自定义注解 + AOP 打造通用开关 + *

+ */ +@RestController +@RequestMapping("/api/reg") +@AllArgsConstructor +public class RegController { + + private final RegService regService; + + @GetMapping("/createOrder") + public Result createOrder() { + + return regService.createOrder(); + } +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/exception/BusinessException.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/exception/BusinessException.java new file mode 100644 index 0000000..7331b4c --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/exception/BusinessException.java @@ -0,0 +1,22 @@ +package com.woniu.commonswitch.exception; + +/** + *

+ * 自定义业务异常 + * 程序员蜗牛 + *

+ */ +public class BusinessException extends RuntimeException { + + public BusinessException() { + super(); + } + + public BusinessException(String errMsg) { + super(errMsg); + } + + public BusinessException(String errMsg, Throwable throwable) { + super(errMsg, throwable); + } +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/service/RegService.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/service/RegService.java new file mode 100644 index 0000000..04dadec --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/service/RegService.java @@ -0,0 +1,22 @@ +package com.woniu.commonswitch.service; + +import com.woniu.commonswitch.annotation.ServiceSwitch; +import com.woniu.commonswitch.constant.Constant; +import com.woniu.commonswitch.util.Result; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +@Service +public class RegService { + + /** + * 下单 + */ + @ServiceSwitch(switchKey = Constant.ConfigCode.REG_PAY_SWITCH) + public Result createOrder() { + + // 具体下单业务逻辑省略.... + + return new Result(HttpStatus.OK.value(), "挂号下单成功"); + } +} diff --git a/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/util/Result.java b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/util/Result.java new file mode 100644 index 0000000..dc5fd7e --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/java/com/woniu/commonswitch/util/Result.java @@ -0,0 +1,48 @@ +package com.woniu.commonswitch.util; + +/** + *

+ * 响应对象封装 + *

+ */ +public class Result { + + public int code; + public String msg; + public T data; + + public Result(int code, String msg) { + this.code = code; + this.msg = msg; + } + + public Result(int code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } +} diff --git a/CommonSwitch/CommonSwitch/src/main/resources/application.yml b/CommonSwitch/CommonSwitch/src/main/resources/application.yml new file mode 100644 index 0000000..2112065 --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/main/resources/application.yml @@ -0,0 +1,14 @@ +server: + port: 8888 + +spring: + redis: + host: 192.168.56.10 + port: 6379 + password: 123456 + jedis: + pool: + max-active: 100 + max-wait: -1ms + max-idle: 50 + min-idle: 1 \ No newline at end of file diff --git a/CommonSwitch/CommonSwitch/src/test/java/com/woniu/commonswitch/CommonSwitchApplicationTests.java b/CommonSwitch/CommonSwitch/src/test/java/com/woniu/commonswitch/CommonSwitchApplicationTests.java new file mode 100644 index 0000000..3b26138 --- /dev/null +++ b/CommonSwitch/CommonSwitch/src/test/java/com/woniu/commonswitch/CommonSwitchApplicationTests.java @@ -0,0 +1,13 @@ +package com.woniu.commonswitch; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class CommonSwitchApplicationTests { + + @Test + void contextLoads() { + } + +}