一个注解让业务逻辑学会了川剧变脸,Redis当起了隐形操盘手
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BusinessSwitch {
String sceneId(); // 业务场景ID
String fallbackMethod();// 降级方法名
int grayRatio() default 0; // 灰度百分比
}
Object execute(Method method, Object[] args) {
SwitchConfig config = redisTemplate.opsForHash()
.get("SWITCH_CONFIG", method.getAnnotation(BusinessSwitch.class).sceneId());
if(config.isOpen() && isGrayRequest(config.getGrayRatio())){
Method fallback = method.getDeclaringClass()
.getMethod(config.getFallbackMethod(), method.getParameterTypes());
return fallback.invoke(target, args);
}
return method.invoke(target, args);
}
采用Redis Hash结构存储配置,通过pipeline批量读取:
List<Object> configs = redisTemplate.executePipelined(
(RedisCallback<Object>) conn -> {
conn.hGetAll("SWITCH_CONFIG".getBytes());
return null;
});
指标 | 改造前(DB配置) | 新方案 | 提升幅度 |
---|---|---|---|
配置生效延迟 | 2-5分钟 | 50ms | 99% |
系统吞吐量 | 1200 TPS | 4500 TPS | 275% |
Full GC次数 | 3次/天 | 0次 | 100% |