添加客户API调用量使用90%发送kafka消息
This commit is contained in:
+51
-5
@@ -14,6 +14,7 @@ import com.fangjia.open.model.RestfulModelAndView;
|
|||||||
import com.fangjia.service.open.ApiUserAuthorityService;
|
import com.fangjia.service.open.ApiUserAuthorityService;
|
||||||
import com.fangjia.service.open.ApiVisitCountService;
|
import com.fangjia.service.open.ApiVisitCountService;
|
||||||
import com.fangjia.service.sql.ApiUserService;
|
import com.fangjia.service.sql.ApiUserService;
|
||||||
|
import com.fangjia.service.util.kafka.KafkaProducerUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -30,12 +31,13 @@ import java.util.*;
|
|||||||
* API拦截器
|
* API拦截器
|
||||||
*
|
*
|
||||||
* @author
|
* @author
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AuthenticationInterceptor implements Interceptor {
|
public class AuthenticationInterceptor implements Interceptor {
|
||||||
|
|
||||||
|
|
||||||
private ApiUserService apiUserService;
|
private ApiUserService apiUserService;
|
||||||
|
|
||||||
|
private KafkaProducerUtils kafkaProducerUtils;
|
||||||
private ApiUserAuthorityService apiUserAuthorityService;
|
private ApiUserAuthorityService apiUserAuthorityService;
|
||||||
private ApiVisitCountService apiVisitCountService;
|
private ApiVisitCountService apiVisitCountService;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationInterceptor.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationInterceptor.class);
|
||||||
@@ -43,9 +45,13 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
private static final List<String> HAS_SUB_SERVICE_URIS = new ArrayList<String>();
|
private static final List<String> HAS_SUB_SERVICE_URIS = new ArrayList<String>();
|
||||||
private static final String SUB_SERVICE_SEPARATOR = "#";
|
private static final String SUB_SERVICE_SEPARATOR = "#";
|
||||||
private static final String SERVICE_CODE_PARAM_NAME = "serviceCode";
|
private static final String SERVICE_CODE_PARAM_NAME = "serviceCode";
|
||||||
|
private static final String SMS_DATA_TOPIC = "sms-data-topic";
|
||||||
|
private static final String SMS_DATA_TOPIC_PROFIX = "sendSms90-";
|
||||||
|
|
||||||
static {
|
static {
|
||||||
HAS_SUB_SERVICE_URIS.add("/property/transaction");
|
HAS_SUB_SERVICE_URIS.add("/property/transaction");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token过期时间 单位:s
|
* token过期时间 单位:s
|
||||||
*/
|
*/
|
||||||
@@ -183,6 +189,35 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isReach90SumLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||||
|
|
||||||
|
if (auth == null || auth.getTotalCountMap() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null) {
|
||||||
|
Integer totalLimit = auth.getTotalCountMap().get(uri);
|
||||||
|
Long totalApiVisitCount;
|
||||||
|
if (totalLimit != null && totalLimit > 0) {
|
||||||
|
totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||||
|
if (totalApiVisitCount.intValue() == (int) (totalLimit * 0.9)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||||
|
Integer totalLimit = auth.getTotalCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||||
|
if (totalLimit != null && totalLimit > 0) {
|
||||||
|
Long totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||||
|
if (totalApiVisitCount.intValue() == (int) (totalLimit * 0.9)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private Authority getAuthority(ActionInvocation invocation) {
|
private Authority getAuthority(ActionInvocation invocation) {
|
||||||
Authority authority;
|
Authority authority;
|
||||||
// 获取方法上的权限注解
|
// 获取方法上的权限注解
|
||||||
@@ -232,8 +267,8 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
|
|
||||||
String uri = request.getRequestURI();
|
String uri = request.getRequestURI();
|
||||||
ApiUserAuthority auth = this.apiUserAuthorityService.findByUserId(apiUser.getId());
|
ApiUserAuthority auth = this.apiUserAuthorityService.findByUserId(apiUser.getId());
|
||||||
if(auth != null){
|
|
||||||
User user = apiUserService.findUserById(apiUser.getId());
|
User user = apiUserService.findUserById(apiUser.getId());
|
||||||
|
if (auth != null) {
|
||||||
AuthenticationBean.get(request, apiUser, user, auth);
|
AuthenticationBean.get(request, apiUser, user, auth);
|
||||||
}
|
}
|
||||||
uri = OpenConfig.getConfig().isDevelopmentEnvironment() ? uri.replace("/fangjia-open", "") : uri;
|
uri = OpenConfig.getConfig().isDevelopmentEnvironment() ? uri.replace("/fangjia-open", "") : uri;
|
||||||
@@ -280,6 +315,10 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "已达到配额上限");
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "已达到配额上限");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
// 检测是否达到总数的90%
|
||||||
|
if (isReach90SumLimit(auth, uri, serviceCode)) {
|
||||||
|
kafkaProducerUtils.send(SMS_DATA_TOPIC, SMS_DATA_TOPIC_PROFIX + apiUser.getPhone() + "-" + user.getUserName() + "-" + uri);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -297,6 +336,10 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "已达到配额上限");
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "已达到配额上限");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
// 检测是否达到总数的90%
|
||||||
|
if (isReach90SumLimit(auth, uri, null)) {
|
||||||
|
kafkaProducerUtils.send(SMS_DATA_TOPIC, SMS_DATA_TOPIC_PROFIX + apiUser.getPhone() + "-" + user.getUserName() + "-" + uri);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -305,9 +348,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param request
|
* @param request
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -322,6 +363,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断该API是否拥有子权限
|
* 判断该API是否拥有子权限
|
||||||
|
*
|
||||||
* @param uri
|
* @param uri
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -331,6 +373,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 2016年06月06日以前的用户是老用户,token不过期
|
* 2016年06月06日以前的用户是老用户,token不过期
|
||||||
|
*
|
||||||
* @param apiUser
|
* @param apiUser
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -340,6 +383,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断Token是否过期
|
* 判断Token是否过期
|
||||||
|
*
|
||||||
* @param token
|
* @param token
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -348,7 +392,6 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param ip 验证客户IP是否在注册的白名单中
|
* @param ip 验证客户IP是否在注册的白名单中
|
||||||
* @param apiUser
|
* @param apiUser
|
||||||
* @return
|
* @return
|
||||||
@@ -448,4 +491,7 @@ public class AuthenticationInterceptor implements Interceptor {
|
|||||||
this.apiVisitCountService = apiVisitCountService;
|
this.apiVisitCountService = apiVisitCountService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setKafkaProducerUtils(KafkaProducerUtils kafkaProducerUtils) {
|
||||||
|
this.kafkaProducerUtils = kafkaProducerUtils;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
@@ -2,6 +2,7 @@ package com.fangjia.service.util.kafka;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
@@ -39,6 +40,12 @@ public class KafkaProducerUtils implements DisposableBean {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void send(String topic,String msg){
|
||||||
|
if (StringUtils.isNotBlank(topic) && StringUtils.isNotBlank(msg)){
|
||||||
|
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, msg);
|
||||||
|
this.producer.send(record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() throws Exception {
|
public void destroy() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user