【发布时间】:2020-04-22 13:39:04
【问题描述】:
如果我有 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,count都会加1,调用者实体可以得到准确的命中次数。
条件:它是一个开放的 API。需要指导如何实现它。 这是我唯一的问题陈述
【问题讨论】:
-
如果系统重启,计数将被重置为0
-
是的..它会的。但是我们必须保持旧的计数,下次任何应用点击 URL 时都会得到更新。这也需要小心。
-
如果您需要计数,在应用重启后,您应该将计数存储在文件或数据库中的某个位置。
-
Microprofile Metrics Counter 可能很合适。
-
/getCount URL 命中时是否需要增加计数?还是您需要增加计数的每个请求?