赞
踩
cd /opt && rpm -ivh erlang-21.3-1.el7.x86_64.rpm
cd /opt && yum install socat -y
rpm -ivh rabbitmq-server-3.8.8-1.el7.noarch.rpm
/sbin/service rabbitmq-server start
/sbin/service rabbitmq-server status
rabbitmq-plugins enable rabbitmq_management
chkconfig rabbitmq-server on
/sbin/service rabbitmq-server stop
/sbin/service rabbitmq-server start && /sbin/service rabbitmq-server status
/sbin/service rabbitmq-server stop && /sbin/service rabbitmq-server status
/sbin/service rabbitmq-server stop && /sbin/service rabbitmq-server start && /sbin/service rabbitmq-server status
systemctl start firewalld && firewall-cmd --permanent --add-port=15672/tcp && firewall-cmd --reload && firewall-cmd --query-port=15672/tcp
cd /etc/rabbitmq && vim rabbitmq.config
[
{
rabbit,
[{
loopback_users,[]
}]
}
].
/sbin/service rabbitmq-server stop && /sbin/service rabbitmq-server start && /sbin/service rabbitmq-server status
<!--rabbitmq-需要的 AMQP 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
cd /etc/rabbitmq && vim rabbitmq.config
[
{rabbit, [
{tcp_listeners, [{"0.0.0.0", 6783}]},
{loopback_users, []}
]}
].
/sbin/service rabbitmq-server stop && /sbin/service rabbitmq-server start && /sbin/service rabbitmq-server status
netstat -plnt | grep 6783
systemctl start firewalld && firewall-cmd --permanent --add-port=6783/tcp && firewall-cmd --reload && firewall-cmd --query-port=6783/tcp
# RabbitMQ 配置 rabbitmq: # 服务器地址 host: # 用户名 username: # 密码 password: ****** # 虚拟主机 virtual-host: / # 端口 port: 6783 # 消息监听器配置 listener: simple: #消费者最小数量 concurrency: 10 #消费者最大数量 max-concurrency: 10 #限制消费者,每次只能处理一条消息,处理完才能继续下一条消息 prefetch: 1 #启动时是否默认启动容器,默认为 true auto-startup: true #被拒绝时重新进入队列的 default-requeue-rejected: true # 消息重试配置 template: retry: #启用消息重试机制,默认为 false enabled: true #初始重试间隔时间 initial-interval: 1000ms #重试最大次数,默认为 3 次 max-attempts: 3 #重试最大时间间隔,默认 10000ms max-interval: 10000ms #重试的间隔乘数 #配置 2 的话,第一次等 1s,第二次等 2s,第三次等 4s multiplier: 1
package com.sxs.seckill.config; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Description: * * @Author sun * @Create 2024/5/11 13:58 * @Version 1.0 */ @Configuration public class RabbitMQConfig { private static final String QUEUE = "queue"; /** * 1. 配置队列 * 2. 队列名为 queue * 3. true 表示: 持久化 * durable: 队列是否持久化。 队列默认是存放到内存中的,rabbitmq 重启则丢失, * 若想重启之后还存在则队列要持久化, * 保存到 Erlang 自带的 Mnesia 数据库中,当 rabbitmq 重启之后会读取该数据库 */ @Bean public Queue queue() { return new Queue(QUEUE, true); } }
package com.sxs.seckill.rabbitmq; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Description: 消息发送者 * * @Author sun * @Create 2024/5/11 14:04 * @Version 1.0 */ @Service @Slf4j public class MQSender { @Resource private RabbitTemplate rabbitTemplate; public void send(Object message) { log.info("发送消息: " + message); // 发送消息到队列,需要指定在配置类中配置的队列名 rabbitTemplate.convertAndSend("queue", message); } }
package com.sxs.seckill.rabbitmq; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; /** * Description: 消息接收者 * * @Author sun * @Create 2024/5/11 14:08 * @Version 1.0 */ @Service @Slf4j public class MQReceiver { // 方法,接收消息 @RabbitListener(queues = "queue") public void receive(Object message) { log.info("接收消息: " + message); } }
package com.sxs.seckill.controller; import com.sxs.seckill.rabbitmq.MQSender; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; /** * Description: * * @Author sun * @Create 2024/5/11 14:13 * @Version 1.0 */ @Controller public class RabbitMQHandler { @Resource private MQSender mqSender; // 调用发送消息的方法 @RequestMapping("/mq") @ResponseBody public void mq() { mqSender.send("hello rabbitmq"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。