【SpringBoot】Spring Boot 实现接口防刷

AOP + 自定义注解 + Redis 实现接口防刷

自定义注解:

Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyPrevent {

    /**
     * redis 缓存前缀,用来区分是哪个方法(有可能有多个方法被注解)
     */
    String prefix();

    /**
     * 过期时间
     */
    int expire() default 1;

    /**
     * 过期单位
     */
    TimeUnit timeUnit() default TimeUnit.SECONDS;

}

AOP:

@Aspect
@Configuration
public class MyPreventAspect {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Around("@annotation(com.zzc.hardcore.annotation.MyPrevent)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        MyPrevent myPrevent = method.getAnnotation(MyPrevent.class);

        // 用户Id 是可以获取得到的
        String userId = "666666";
        String prefix = myPrevent.prefix();
        final String redisKey = prefix + "_" + userId;
        String s = stringRedisTemplate.opsForValue().get(redisKey);
        if (!StringUtils.isEmpty(s)) {
            Map<String, Object> result = new HashMap<>(2);
            result.put("code", "500");
            result.put("msg", "系统数据处理中...,请稍后");
            return result;
        }
        stringRedisTemplate.opsForValue().set(redisKey, userId, myPrevent.expire(), myPrevent.timeUnit());
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throw new RuntimeException("系统异常");
        }
    }

}

控制器测试:

@RequestMapping("/myPrevent")
@RestController
public class MyPreventController {

    @MyPrevent(prefix = "test", expire = 3, timeUnit = TimeUnit.SECONDS)
    @RequestMapping("/test")
    public Object test() {
        return "test";
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 API 接口合并可以通过 Spring Boot 中的 Feign 和 Ribbon 组件来实现。具体步骤如下: 1. 在 pom.xml 中添加依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 2. 创建一个 Feign 接口,用于调用需要合并的 API 接口: ``` @FeignClient(name = "service-name") public interface ApiService { @GetMapping("/api/path") String getData(); } ``` 3. 在 Spring Boot 启动类中添加 @EnableFeignClients 和 @RibbonClients 注解: ``` @SpringBootApplication @EnableFeignClients @RibbonClients(defaultConfiguration = RibbonConfiguration.class) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 创建一个 Ribbon 配置类,用于配置负载均衡策略: ``` @Configuration public class RibbonConfiguration { @Bean public IRule ribbonRule() { // 配置负载均衡策略 return new RandomRule(); } } ``` 5. 创建一个自定义的 API 接口,用于合并多个 API 接口: ``` @RestController @RequestMapping("/api") public class CombinedApiController { @Autowired private ApiService apiService; @GetMapping("/combined") public List<String> getDataFromMultipleApis() { List<String> result = new ArrayList<>(); result.add(apiService.getData()); // 调用其他需要合并的接口并将结果加入到 result 中 return result; } } ``` 通过以上步骤,就可以实现 API 接口的合并。在 CombinedApiController 中,通过调用 ApiService 中的接口来获取数据,然后将多个接口返回的数据合并在一起返回给客户端。同时,通过 Ribbon 配置负载均衡策略,可以实现对多个 API 服务的负载均衡。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值