调用 REST API 的次数 [英] Number of times REST API was called

查看:51
本文介绍了调用 REST API 的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 HTTP REST API:example/getCount

If I have a HTTP REST API : example /getCount

示例代码

@RestController
public class CountController {
    long count = 0;
    @Getmapping("/getCount")
    public long getCount(){
        count++; // Here is the problem, how to keep the latest count if the API was hit by multiple Application/clients
        return count;
    }
}

问题描述:每次任何应用/系统调用上述API,计数都会增加1,调用者实体可以获得准确的命中数.

Problem Description: Every time any app/system call the above mentioned API, count will be increased by 1 and the caller entity can get the exact number of hit.

条件:它是一个开放的 API.需要指导如何实现它.这是我遇到的唯一问题陈述

Condition: Its a open API.Need guidance how to achieve it. This is the only problem statement I am having

推荐答案

如果你想要计数器行为,你可以使用静态类型

you can use static type if you want counter behavior as follows

  • 计数器对于调用 API 的所有客户端都是通用的.
  • 一旦服务器重新启动,计数器就会重新启动为 0.

@RestController
public class CountController {
    static long count = 0;
    @Getmapping("/getCount")
    public long getCount(){
        count++; // Here is the problem, how to keep the latest count if the API was hit by multiple Application/clients
        return count;
    }
}

如果您希望所有客户端使用不同的计数器,或者即使服务器关闭或重新启动也想保存计数器值,则需要使用 DB 来存储计数器.

If You want different counter for all client or want to hold counter value even if server is shutdown or restart then you need to use DB to store counter.

这篇关于调用 REST API 的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆