马府强
1 year ago
19 changed files with 481 additions and 0 deletions
@ -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 |
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<module version="4"> |
|||
<component name="AdditionalModuleElements"> |
|||
<content url="file://$MODULE_DIR$" /> |
|||
</component> |
|||
</module> |
@ -0,0 +1,18 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="CompilerConfiguration"> |
|||
<annotationProcessing> |
|||
<profile name="Maven default annotation processors profile" enabled="true"> |
|||
<sourceOutputDir name="target/generated-sources/annotations" /> |
|||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> |
|||
<outputRelativeToContentRoot value="true" /> |
|||
<module name="CommonSwitch" /> |
|||
</profile> |
|||
</annotationProcessing> |
|||
</component> |
|||
<component name="JavacSettings"> |
|||
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> |
|||
<module name="CommonSwitch" options="-parameters" /> |
|||
</option> |
|||
</component> |
|||
</project> |
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="Encoding"> |
|||
<file url="file://$PROJECT_DIR$/CommonSwitch/src/main/java" charset="UTF-8" /> |
|||
</component> |
|||
</project> |
@ -0,0 +1,20 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="RemoteRepositoriesConfiguration"> |
|||
<remote-repository> |
|||
<option name="id" value="central" /> |
|||
<option name="name" value="Maven Central repository" /> |
|||
<option name="url" value="https://repo1.maven.org/maven2" /> |
|||
</remote-repository> |
|||
<remote-repository> |
|||
<option name="id" value="central" /> |
|||
<option name="name" value="Central Repository" /> |
|||
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public" /> |
|||
</remote-repository> |
|||
<remote-repository> |
|||
<option name="id" value="jboss.community" /> |
|||
<option name="name" value="JBoss Community repository" /> |
|||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> |
|||
</remote-repository> |
|||
</component> |
|||
</project> |
@ -0,0 +1,14 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="ExternalStorageConfigurationManager" enabled="true" /> |
|||
<component name="MavenProjectsManager"> |
|||
<option name="originalFiles"> |
|||
<list> |
|||
<option value="$PROJECT_DIR$/CommonSwitch/pom.xml" /> |
|||
</list> |
|||
</option> |
|||
</component> |
|||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
|||
<output url="file://$PROJECT_DIR$/out" /> |
|||
</component> |
|||
</project> |
@ -0,0 +1,8 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project version="4"> |
|||
<component name="ProjectModuleManager"> |
|||
<modules> |
|||
<module fileurl="file://$PROJECT_DIR$/.idea/CommonSwitch.iml" filepath="$PROJECT_DIR$/.idea/CommonSwitch.iml" /> |
|||
</modules> |
|||
</component> |
|||
</project> |
@ -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/ |
@ -0,0 +1,64 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
<parent> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-parent</artifactId> |
|||
<version>2.7.16</version> |
|||
<relativePath/> <!-- lookup parent from repository --> |
|||
</parent> |
|||
<groupId>com.example</groupId> |
|||
<artifactId>CommonSwitch</artifactId> |
|||
<version>0.0.1-SNAPSHOT</version> |
|||
<name>CommonSwitch</name> |
|||
<description>CommonSwitch</description> |
|||
<properties> |
|||
<java.version>1.8</java.version> |
|||
</properties> |
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-web</artifactId> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-aop</artifactId> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-data-redis</artifactId> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
<optional>true</optional> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-test</artifactId> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<plugins> |
|||
<plugin> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-maven-plugin</artifactId> |
|||
<configuration> |
|||
<excludes> |
|||
<exclude> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
</exclude> |
|||
</excludes> |
|||
</configuration> |
|||
</plugin> |
|||
</plugins> |
|||
</build> |
|||
|
|||
</project> |
@ -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); |
|||
} |
|||
|
|||
} |
@ -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; |
|||
|
|||
/** |
|||
* <p> |
|||
* 通用开关注解 0 关 1 开 |
|||
* 程序员蜗牛 |
|||
* </p> |
|||
*/ |
|||
@Target({ElementType.METHOD}) // 作用在方法上
|
|||
@Retention(RetentionPolicy.RUNTIME) // 运行时起作用
|
|||
public @interface ServiceSwitch { |
|||
|
|||
/** |
|||
* 业务开关的key(不同key代表不同功效的开关) |
|||
* {@link Constant.ConfigCode} |
|||
*/ |
|||
String switchKey(); |
|||
|
|||
// 提示信息,默认值可在使用注解时自行定义。
|
|||
String message() default "当前请求人数过多,请稍后重试。"; |
|||
} |
@ -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; |
|||
|
|||
/** |
|||
* <p> |
|||
* 教你一招 SpringBoot + 自定义注解 + AOP 打造通用开关 |
|||
* 程序员蜗牛 |
|||
* </p> |
|||
*/ |
|||
@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); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
package com.woniu.commonswitch.constant; |
|||
|
|||
/** |
|||
* <p> |
|||
* 常量类 |
|||
* 程序员蜗牛 |
|||
* </p> |
|||
* |
|||
*/ |
|||
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"; |
|||
|
|||
// 其他业务相关的配置常量
|
|||
// ....
|
|||
} |
|||
} |
@ -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; |
|||
|
|||
/** |
|||
* <p> |
|||
* 程序员蜗牛 |
|||
* 教你一招 SpringBoot + 自定义注解 + AOP 打造通用开关 |
|||
* </p> |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/api/reg") |
|||
@AllArgsConstructor |
|||
public class RegController { |
|||
|
|||
private final RegService regService; |
|||
|
|||
@GetMapping("/createOrder") |
|||
public Result createOrder() { |
|||
|
|||
return regService.createOrder(); |
|||
} |
|||
} |
@ -0,0 +1,22 @@ |
|||
package com.woniu.commonswitch.exception; |
|||
|
|||
/** |
|||
* <p> |
|||
* 自定义业务异常 |
|||
* 程序员蜗牛 |
|||
* </p> |
|||
*/ |
|||
public class BusinessException extends RuntimeException { |
|||
|
|||
public BusinessException() { |
|||
super(); |
|||
} |
|||
|
|||
public BusinessException(String errMsg) { |
|||
super(errMsg); |
|||
} |
|||
|
|||
public BusinessException(String errMsg, Throwable throwable) { |
|||
super(errMsg, throwable); |
|||
} |
|||
} |
@ -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(), "挂号下单成功"); |
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
package com.woniu.commonswitch.util; |
|||
|
|||
/** |
|||
* <p> |
|||
* 响应对象封装 |
|||
* </p> |
|||
*/ |
|||
public class Result<T> { |
|||
|
|||
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; |
|||
} |
|||
} |
@ -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 |
@ -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() { |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue