Java代码实现记录接口调用次数

解决多台服务记录冲突或者覆盖的问题,可以使用 Redis 的分布式锁来保证同一时刻只有一个服务可以进行记录接口调用次数的操作。具体实现步骤如下:

  1. 在 Redis 上创建一个计数器,用于记录接口调用次数。
  2. 使用 Redis 的分布式锁机制,保证同一时刻只有一个服务可以进行接口调用次数的记录。可以使用 Redisson 等 Redis 客户端库来实现分布式锁。
  3. 在记录接口调用次数的代码中,加入获取分布式锁的逻辑,保证同一时刻只有一个服务可以进行记录操作。
public class ApiCounter {
    private RedissonClient redissonClient;
    public ApiCounter(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }
    public void recordApiCall(String apiName) {
        String lockKey = "api-call-lock-" + apiName;
        RLock lock = redissonClient.getLock(lockKey);
        try {
            lock.lock();
            // 获取分布式锁成功,进行接口调用次数记录
            String counterKey = "api-call-counter-" + apiName;
            RAtomicLong counter = redissonClient.getAtomicLong(counterKey);
            counter.incrementAndGet();
        } finally {
            // 释放分布式锁
            lock.unlock();
        }
    }
}

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
统计Java接口调用次数,可以使用AOP(面向切面编程)技术,在接口调用时拦截请求,并记录接口调用次数。 具体实现步骤如下: 1. 定义一个注解,用于标记需要统计调用次数接口方法。 例如: ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Count { } ``` 2. 创建一个切面类,使用@Aspect注解标记,并在类中定义一个切点,用于匹配被@Count注解标记的方法。 例如: ```java @Aspect @Component public class CountAspect { private Map<String, Integer> countMap = new ConcurrentHashMap<>(); @Pointcut("@annotation(com.example.demo.annotation.Count)") public void countPointcut() { } @Around("countPointcut()") public Object countAround(ProceedingJoinPoint joinPoint) throws Throwable { String methodName = joinPoint.getSignature().getName(); String className = joinPoint.getSignature().getDeclaringTypeName(); String key = className + "." + methodName; Integer count = countMap.get(key); if (count == null) { count = 1; } else { count++; } countMap.put(key, count); Object result = joinPoint.proceed(); return result; } } ``` 在上述代码中,我们使用了ConcurrentHashMap来存储每个接口方法的调用次数,初始值为1,每次调用自增1。 3. 在需要统计调用次数接口方法上添加@Count注解。 例如: ```java @RestController @RequestMapping("/demo") public class DemoController { @Autowired private DemoService demoService; @Count @GetMapping("/hello") public String hello() { return "hello"; } @GetMapping("/count") public Map<String, Integer> count() { return demoService.getCountMap(); } } ``` 在上述代码中,我们在hello()方法上添加了@Count注解,表示需要统计该方法的调用次数。 4. 创建一个服务类,用于获取调用次数统计结果。 例如: ```java @Service public class DemoService { @Autowired private CountAspect countAspect; public Map<String, Integer> getCountMap() { return countAspect.getCountMap(); } } ``` 在上述代码中,我们通过@Autowired注解注入了CountAspect切面类,并通过该类的getCountMap()方法获取到统计结果。 最后,启动应用程序,在浏览器中访问/hello接口,多次刷新页面,然后访问/count接口,即可获取到接口方法调用次数统计结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一百减一是零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值