统计客户调用接口的次数

由于用户行为异常,需要定位是哪个用户频繁调用某一个接口。

日志大致格式如下:

10:33:13.412 [DubboServerHandler-10.999.111.888:20880-thread-200] INFO  c.s.f.c.a.ServiceExceptionAdvice[64] - call method createToken,input:["0001122798"],return:{"data":"Zzzfa8a48c8-71ff-4dca-86e0-2da9f25f9939","errNo":"000000","success":true},cost:7

简洁方式:

grep "call method getCustBaseInfo" c.log| awk -F"," '{print substr($2,9,10)}'|sort|uniq -c|sort -n

下面写的比较啰嗦:

查找出所有调用该接口的用户:

cat c.log |grep 'createToken'|grep 'data' 
可以保存到文件

cat c.log |grep 'createToken'|grep 'data' > tmp.txt
列出客户编号:

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
统计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接口,即可获取到接口方法调用次数统计结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值