spring boot 一个极简单的 demo 示例

1. 描述

一个spring boot 的简单demo示例。

环境:

IDE(idea):2021.3
JDK:1.8
maven:3.8.4
spring boot:2.5.6

2. 结果

  • 接口返回 Hello {name}!,eg: Hello World!

在这里插入图片描述

3. demo

3.0 项目结构
在这里插入图片描述

3.1 pom.xml

  • 引入 web jar包

    <?xml version="1.0" encoding="UTF-8"?>


    4.0.0

    <parent>
        <groupId>com.byrc</groupId>
        <artif
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用Spring Boot和RabbitMQ的限流示例: 1. 首先,需要在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 创建一个RabbitMQ配置类,用于定义连接工厂和RabbitMQ模板: ``` @Configuration public class RabbitMQConfig { @Value("${spring.rabbitmq.host}") private String host; @Value("${spring.rabbitmq.port}") private int port; @Value("${spring.rabbitmq.username}") private String username; @Value("${spring.rabbitmq.password}") private String password; @Value("${spring.rabbitmq.virtual-host}") private String virtualHost; @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost(host); connectionFactory.setPort(port); connectionFactory.setUsername(username); connectionFactory.setPassword(password); connectionFactory.setVirtualHost(virtualHost); return connectionFactory; } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); return rabbitTemplate; } } ``` 3. 创建一个消息接收者,并使用@RabbitListener注释指定要监听的队列: ``` @Component public class MessageReceiver { @RabbitListener(queues = "test.queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 4. 创建一个消息发布者,并使用RabbitTemplate发送消息: ``` @Component public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("test.exchange", "test.routingkey", message); } } ``` 5. 创建一个限流过滤器,用于限制消息的发送速率: ``` @Component public class RateLimitFilter implements Filter { private static final int MAX_MESSAGES_PER_SECOND = 10; private static final int INTERVAL = 1000; private final AtomicLong lastTime = new AtomicLong(System.currentTimeMillis()); private final AtomicInteger count = new AtomicInteger(0); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { long currentTime = System.currentTimeMillis(); long timeSinceLast = currentTime - lastTime.get(); if (timeSinceLast > INTERVAL) { int currentCount = count.getAndSet(0); lastTime.set(currentTime); return true; } else { int currentCount = count.incrementAndGet(); if (currentCount > MAX_MESSAGES_PER_SECOND) { response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value()); return false; } else { return true; } } } } ``` 6. 将限流过滤器添加到应用程序中: ``` @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private RateLimitFilter rateLimitFilter; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(rateLimitFilter); } } ``` 7. 在应用程序中使用消息发布者发送消息: ``` @Autowired private MessageSender messageSender; @GetMapping("/send") public void sendMessage() { messageSender.sendMessage("Hello, RabbitMQ!"); } ``` 现在,应用程序将限制每秒发送的消息数量,以避免过度负载RabbitMQ服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值