提交 80b3fffb 作者: 沈振路

频率控制

上级 a1e6598d
...@@ -224,10 +224,10 @@ ...@@ -224,10 +224,10 @@
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency> </dependency>
<!-- <dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>--> </dependency>
<dependency> <dependency>
......
package com.yaoyaozw.customer.configs; package com.yaoyaozw.customer.configs;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -10,10 +14,10 @@ import java.util.Map; ...@@ -10,10 +14,10 @@ import java.util.Map;
* @author darker * @author darker
* @date 2022/10/21 16:04 * @date 2022/10/21 16:04
*/ */
//@Configuration @Configuration
public class RabbitConfig { public class RabbitConfig {
/* @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", "DEATH_EXCHANGE");
...@@ -45,7 +49,7 @@ public class RabbitConfig { ...@@ -45,7 +49,7 @@ public class RabbitConfig {
@Bean @Bean
public Binding bindingDeath(){ public Binding bindingDeath(){
return BindingBuilder.bind(deathQueue()).to(deathExchange()).with("death"); return BindingBuilder.bind(deathQueue()).to(deathExchange()).with("death");
}*/ }
} }
package com.yaoyaozw.customer.controller; package com.yaoyaozw.customer.controller;
import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.yaoyaozw.customer.constants.CustomerCommonConstant;
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;
import com.yaoyaozw.customer.service.CustomerGraphicsService; import com.yaoyaozw.customer.service.CustomerGraphicsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelCallback;
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.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -15,6 +24,7 @@ import java.util.Calendar; ...@@ -15,6 +24,7 @@ import java.util.Calendar;
* @author wgh * @author wgh
* @date 2022/10/10 20:04 * @date 2022/10/10 20:04
*/ */
@Slf4j
@RestController @RestController
@RequestMapping("/customer-service/test") @RequestMapping("/customer-service/test")
public class TestController { public class TestController {
...@@ -25,6 +35,10 @@ public class TestController { ...@@ -25,6 +35,10 @@ public class TestController {
private AccountOrderService accountOrderService; private AccountOrderService accountOrderService;
@Autowired @Autowired
private CustomerDelayPublishService customerDelayPublishService; private CustomerDelayPublishService customerDelayPublishService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/CustomerTest") @GetMapping("/CustomerTest")
public void test(){ public void test(){
...@@ -69,4 +83,52 @@ public class TestController { ...@@ -69,4 +83,52 @@ public class TestController {
integrationRequestDTO.setRequestDate(calendar.getTime()); integrationRequestDTO.setRequestDate(calendar.getTime());
accountOrderService.activeUserByOrder(integrationRequestDTO); accountOrderService.activeUserByOrder(integrationRequestDTO);
} }
@GetMapping("/ygLimit")
public void ygLimit(){
limit();
}
private void limit() {
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;
}
log.info("当前count: {}", count);
long stamp = System.currentTimeMillis();
// 判断计数是否达到上限
go = (Integer)count < 80;
if (go) {
// 没有达到上限,发送消息并且计数加1
rabbitTemplate.convertAndSend("TTL_EXCHANGE", "yg_limit", stamp);
redisTemplate.opsForValue().increment(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
}
if (!go) {
// 计数达到上限了,休眠30秒,递归调用
try {
log.info("达到上限, 休眠30秒");
Thread.sleep(30000);
limit();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@RabbitListener(queues = "DEATH_QUEUE")
public void listener() {
redisTemplate.opsForValue().decrement(CustomerCommonConstant.YANG_GUANG_ACCESS_LIMIT_REDIS_KEY);
}
} }
...@@ -4,7 +4,7 @@ spring: ...@@ -4,7 +4,7 @@ spring:
application: application:
name: customer-service name: customer-service
profiles: profiles:
active: pro active: dev
--- ---
spring: spring:
profiles: dev profiles: dev
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论