提交 c45213f7 作者: 沈振路

频率控制

上级 b0da370a
...@@ -8,9 +8,11 @@ import cn.hutool.json.JSONUtil; ...@@ -8,9 +8,11 @@ import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yaoyaozw.customer.common.R; import com.yaoyaozw.customer.common.R;
import com.yaoyaozw.customer.constants.ApiResultConstant; import com.yaoyaozw.customer.constants.ApiResultConstant;
import com.yaoyaozw.customer.constants.CustomerCommonConstant; import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.constants.RabbitCommonNameConstant;
import com.yaoyaozw.customer.entity.*; import com.yaoyaozw.customer.entity.*;
import com.yaoyaozw.customer.enums.CustomerStoreTemplateEnum; import com.yaoyaozw.customer.enums.CustomerStoreTemplateEnum;
import com.yaoyaozw.customer.feigns.ReferralFeignClient; import com.yaoyaozw.customer.feigns.ReferralFeignClient;
...@@ -28,6 +30,8 @@ import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO; ...@@ -28,6 +30,8 @@ import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
...@@ -69,6 +73,8 @@ public class CustomerServiceCommonAsyncComponent { ...@@ -69,6 +73,8 @@ public class CustomerServiceCommonAsyncComponent {
private KanbanCommonMapper kanbanCommonMapper; private KanbanCommonMapper kanbanCommonMapper;
@Autowired @Autowired
private CustomerGraphicsDelayMapper customerGraphicsDelayMapper; private CustomerGraphicsDelayMapper customerGraphicsDelayMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
@Async("myExecutor") @Async("myExecutor")
...@@ -542,6 +548,7 @@ public class CustomerServiceCommonAsyncComponent { ...@@ -542,6 +548,7 @@ public class CustomerServiceCommonAsyncComponent {
* 以下是公有方法 * 以下是公有方法
*/ */
public void getCopyReferral(String dateStr, AuthInfoVO authInfoVo, ReferralEntity referralEntity) { public void getCopyReferral(String dateStr, AuthInfoVO authInfoVo, ReferralEntity referralEntity) {
ygAccessLimit(true, authInfoVo.getStoreType());
referralEntity.setStoreType(authInfoVo.getStoreType()); referralEntity.setStoreType(authInfoVo.getStoreType());
// 非常用链接类型的name需要处理 // 非常用链接类型的name需要处理
String name = referralEntity.getName(); String name = referralEntity.getName();
...@@ -600,7 +607,50 @@ public class CustomerServiceCommonAsyncComponent { ...@@ -600,7 +607,50 @@ public class CustomerServiceCommonAsyncComponent {
return userEntityList; return userEntityList;
} }
private void ygAccessLimit(Boolean checkStoreType, String storeType) {
if (checkStoreType && !CustomerCommonConstant.STORE_NAME_YANG_GUANG.equals(storeType)) {
// 需要校验书城但是不是阳光书城,直接返回
return;
}
boolean go;
synchronized (rabbitTemplate) {
// 从redis查询计数
Object count = redisTemplate.opsForValue().get(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
if (ObjectUtil.isNull(count)) {
// redis中没有,设置0
redisTemplate.opsForValue().set(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY, 0);
count = 0;
}
LOCAL_LOG.info("当前count: {}", count);
long stamp = System.currentTimeMillis();
// 判断计数是否达到上限
go = (Integer)count < 80;
if (go) {
// 没有达到上限,发送消息并且计数加1
rabbitTemplate.convertAndSend(RabbitCommonNameConstant.OPERATE_COMMON_EXCHANGE, RabbitCommonNameConstant.YG_LIMIT_TTL_ROUTE_KEY, stamp);
redisTemplate.opsForValue().increment(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
}
if (!go) {
// 计数达到上限了,休眠30秒,递归调用
try {
LOCAL_LOG.info("达到上限, 休眠15秒");
Thread.sleep(15000);
// 再次调用不需要校验书城
ygAccessLimit(false, "");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@RabbitListener(queues = RabbitCommonNameConstant.YG_LIMIT_DEATH_QUEUE)
public void listener() {
redisTemplate.opsForValue().decrement(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
} }
package com.yaoyaozw.customer.configs; package com.yaoyaozw.customer.configs;
import com.yaoyaozw.customer.constants.RabbitCommonNameConstant;
import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -17,38 +20,44 @@ import java.util.Map; ...@@ -17,38 +20,44 @@ import java.util.Map;
@Configuration @Configuration
public class RabbitConfig { public class RabbitConfig {
/* @Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}*/
@Bean @Bean
public Queue directQueue(){ public Queue directQueue(){
Map<String, Object> map = new HashMap<>(4); Map<String, Object> map = new HashMap<>(4);
map.put("x-dead-letter-exchange", "DEATH_EXCHANGE"); map.put("x-dead-letter-exchange", RabbitCommonNameConstant.DEATH_EXCHANGE);
map.put("x-dead-letter-routing-key", "death"); map.put("x-dead-letter-routing-key", RabbitCommonNameConstant.YG_LIMIT_DEATH_ROUTE_KEY);
map.put("x-message-ttl", 60000); map.put("x-message-ttl", 60000);
return new Queue("YG_ACCESS_LIMIT", true, false, false, map); return new Queue(RabbitCommonNameConstant.YG_LIMIT_TTL_QUEUE, true, false, false, map);
} }
@Bean @Bean
public DirectExchange directExchange(){ public DirectExchange directExchange(){
return new DirectExchange("TTL_EXCHANGE"); return new DirectExchange(RabbitCommonNameConstant.OPERATE_COMMON_EXCHANGE);
} }
@Bean @Bean
public Queue deathQueue(){ public Queue deathQueue(){
return new Queue("DEATH_QUEUE"); return new Queue(RabbitCommonNameConstant.YG_LIMIT_DEATH_QUEUE);
} }
@Bean @Bean
public DirectExchange deathExchange(){ public DirectExchange deathExchange(){
return new DirectExchange("DEATH_EXCHANGE"); return new DirectExchange(RabbitCommonNameConstant.DEATH_EXCHANGE);
} }
@Bean @Bean
public Binding bindingDirect(){ public Binding bindingDirect(){
return BindingBuilder.bind(directQueue()).to(directExchange()).with("yg_limit"); return BindingBuilder.bind(directQueue()).to(directExchange()).with(RabbitCommonNameConstant.YG_LIMIT_TTL_ROUTE_KEY);
} }
@Bean @Bean
public Binding bindingDeath(){ public Binding bindingDeath(){
return BindingBuilder.bind(deathQueue()).to(deathExchange()).with("death"); return BindingBuilder.bind(deathQueue()).to(deathExchange()).with(RabbitCommonNameConstant.YG_LIMIT_DEATH_ROUTE_KEY);
} }
......
package com.yaoyaozw.customer.constants;
/**
* @author darker
* @date 2022/10/24 14:40
*/
public class RabbitCommonNameConstant {
/**
* 1、运营系统交换机
*/
public static final String OPERATE_COMMON_EXCHANGE = "OPERATE_COMMON_EXCHANGE";
/**
* 阳光访问频率控制
*/
public static final String YG_LIMIT_TTL_QUEUE = "YG_LIMIT_TTL_QUEUE";
public static final String YG_LIMIT_TTL_ROUTE_KEY = "YG_LIMIT";
/**
* 2、死信交换机 - 死信队列 - routeKey
*/
public static final String DEATH_EXCHANGE = "DEATH_EXCHANGE";
/**
* 阳光访问频率死信
*/
public static final String YG_LIMIT_DEATH_QUEUE = "YG_LIMIT_DEATH_QUEUE";
public static final String YG_LIMIT_DEATH_ROUTE_KEY = "YG_LIMIT_DEATH";
}
...@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil; ...@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.AMQP; import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import com.yaoyaozw.customer.constants.CustomerCommonConstant; import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.constants.RabbitCommonNameConstant;
import com.yaoyaozw.customer.dto.integration.IntegrationRequestDTO; import com.yaoyaozw.customer.dto.integration.IntegrationRequestDTO;
import com.yaoyaozw.customer.service.AccountOrderService; import com.yaoyaozw.customer.service.AccountOrderService;
import com.yaoyaozw.customer.service.CustomerDelayPublishService; import com.yaoyaozw.customer.service.CustomerDelayPublishService;
...@@ -105,19 +106,19 @@ public class TestController { ...@@ -105,19 +106,19 @@ public class TestController {
log.info("当前count: {}", count); log.info("当前count: {}", count);
long stamp = System.currentTimeMillis(); long stamp = System.currentTimeMillis();
// 判断计数是否达到上限 // 判断计数是否达到上限
go = (Integer)count < 80; go = (Integer)count < 40;
if (go) { if (go) {
// 没有达到上限,发送消息并且计数加1 // 没有达到上限,发送消息并且计数加1
rabbitTemplate.convertAndSend("TTL_EXCHANGE", "yg_limit", stamp); rabbitTemplate.convertAndSend(RabbitCommonNameConstant.OPERATE_COMMON_EXCHANGE, RabbitCommonNameConstant.YG_LIMIT_TTL_ROUTE_KEY, stamp);
redisTemplate.opsForValue().increment(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY); redisTemplate.opsForValue().increment(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
} }
} }
if (!go) { if (!go) {
// 计数达到上限了,休眠30秒,递归调用 // 计数达到上限了,休眠30秒,递归调用
try { try {
log.info("达到上限, 休眠1分钟"); log.info("达到上限, 休眠15秒");
Thread.sleep(60000); Thread.sleep(15000);
limit(); limit();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -126,8 +127,9 @@ public class TestController { ...@@ -126,8 +127,9 @@ public class TestController {
} }
@RabbitListener(queues = "DEATH_QUEUE") @RabbitListener(queues = RabbitCommonNameConstant.YG_LIMIT_DEATH_QUEUE)
public void listener() { public void listener() {
log.info("消费");
redisTemplate.opsForValue().decrement(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY); redisTemplate.opsForValue().decrement(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论