在程序化广告行业中,广告主希望广告能够精准展示给目标用户,同时避免广告被恶意刷量或重复曝光给同一用户。为此,需要实现一个基于IP频次过滤的广告请求处理系统,主要目标包括:
系统采用分层架构,分为以下模块:
ip:IP地址,值为时间戳:访问次数。KafkaTemplate将IP地址发送到Kafka的ad-requests主题。@KafkaListener注解实现消息消费。IpFilterService.checkIpFrequency(ipAddress)进行频次检查。public boolean checkIpFrequency(String ipAddress) {
String key = "ip:" + ipAddress;
long currentTime = System.currentTimeMillis() / 1000;
long startOfDayTimestamp = getStartOfDayTimestamp();
String value = redisTemplate.opsForValue().get(key);
if (value != null) {
String[] parts = value.split(":");
long lastAccessTime = Long.parseLong(parts[0]);
int accessCount = Integer.parseInt(parts[1]);
if (lastAccessTime < startOfDayTimestamp) {
redisTemplate.opsForValue().set(key, currentTime + ":1", getRemainingSecondsUntilEndOfDay(), TimeUnit.SECONDS);
return true;
} else {
if (accessCount >= FREQUENCY_THRESHOLD) {
return false;
} else {
redisTemplate.opsForValue().set(key, currentTime + ":" + (accessCount + 1), getRemainingSecondsUntilEndOfDay(), TimeUnit.SECONDS);
return true;
}
}
} else {
redisTemplate.opsForValue().set(key, currentTime + ":1", getRemainingSecondsUntilEndOfDay(), TimeUnit.SECONDS);
return true;
}
}@KafkaListener(topics = "ad-requests", groupId = "ip-filter-group")
public void consume(String ipAddress) {
boolean allowAccess = ipFilterService.checkIpFrequency(ipAddress);
if (allowAccess) {
showAd(ipAddress);
} else {
logRejectedRequest(ipAddress);
}
}本方案通过IP频次过滤和Kafka异步处理,实现了高效、可靠的广告请求处理系统。系统具备高扩展性和灵活性,能够满足程序化广告行业的需求,同时为后续优化和扩展提供了良好的基础。