感谢 https://www.cnblogs.com/fengyumeng/p/11133924.html#4443030
1按照erlang
由于rabbitmq是基于erlang语言开发的,所以必须先安装erlang。
安装依赖
yum -y install gcc glibc-devel make ncurses-devel openssl-devel xmlto perl wget gtk2-devel binutils-devel
erlang官网:
https://www.erlang.org/downloads
下载(会比较慢,请耐心等待)
wget http://erlang.org/download/otp_src_22.0.tar.gz
解压
tar -zxvf otp_src_22.0.tar.gz
移走
mv otp_src_22.0 /usr/local/
切换目录
cd /usr/local/otp_src_22.0/
创建即将安装的目录
mkdir ../erlang
配置安装路径
./configure --prefix=/usr/local/erlang
如果遇到这个错 你就假装没看到
安装
make install
查看一下是否安装成功
ll /usr/local/erlang/bin
添加环境变量
echo 'export PATH=$PATH:/usr/local/erlang/bin' >> /etc/profile
刷新环境变量
source /etc/profile
甩一条命令
Erl
好的,看到这个就是安装成功了!
在里面输入halt().命令退出来(那个点号别忘记)
2安装RabbitMQ
rabbitmq下载地址:
https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.15
下载
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.15/rabbitmq-server-generic-unix-3.7.15.tar.xz
由于是tar.xz格式的所以需要用到xz,没有的话就先安装
yum install -y xz
第一次解压
/bin/xz -d rabbitmq-server-generic-unix-3.7.15.tar.xz
第二次解压
tar -xvf rabbitmq-server-generic-unix-3.7.15.tar
移走
mv rabbitmq_server-3.7.15/ /usr/local/
改名
mv /usr/local/rabbitmq_server-3.7.15 rabbitmq
配置环境变量
echo 'export PATH=$PATH:/usr/local/rabbitmq/sbin' >> /etc/profile
刷新环境变量
source /etc/profile
创建配置目录
mkdir /etc/rabbitmq
启动命令
启动:
rabbitmq-server -detached
停止:
rabbitmqctl stop
状态:
rabbitmqctl status
防火墙之类的请自行处理(5672和15672端口)
3开启web插件
rabbitmq-plugins enable rabbitmq_management
http://IP:15672/
默认账号密码:guest guest(这个账号只允许本机访问)
4用户管理
查看所有用户
rabbitmqctl list_users
添加一个用户
rabbitmqctl add_user zhaobl 123456
配置权限
rabbitmqctl set_permissions -p "/" zhaobl ".*" ".*" ".*"
查看用户权限
rabbitmqctl list_user_permissions zhaobl
设置tag
rabbitmqctl set_user_tags zhaobl administrator
删除用户(安全起见,删除默认用户)
rabbitmqctl delete_user guest
配置好用户之后重启一下rabbit
然后就可以用新账号进行登陆
5springboot整合rabbitmq
涉及的配置和类为:RabbitConfig.java、Receiver.java、TestController.java、application.properties、pom.xml
Pom.xml
org.springframework.boot
spring-boot-starter-amqp
application.properties
#配置rabbitMq 服务器
spring.rabbitmq.host=ip
spring.rabbitmq.port=5672
spring.rabbitmq.username= zhaobl
spring.rabbitmq.password=123456
#手动确认开启
spring.rabbitmq.listener.simple.acknowledge-mode=manual
RabbitConfig.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* RabbitMQ配置类,指定监听队列
*
*/
@Configuration
public class RabbitConfig implements RabbitTemplate.ReturnCallback, RabbitTemplate.ConfirmCallback{
protected final Logger log = LoggerFactory.getLogger(this.getClass());
@Bean
public Queue queue() {
return new Queue("TestDirectRouting2", true);// 消息队列名称
}
@Bean
public DirectExchange defaultExchange() {
return new DirectExchange("non.existent.exchange2", true, false);
}
/**
* 将用户注册队列绑定到路由交换配置上并设置指定路由键进行转发
* @return
*/
@Bean
public Binding userRegisterBinding()
{
return BindingBuilder.bind(queue()).to(defaultExchange()).with("TestDirectRouting2");
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
if(!ack) {
log.error("confirm error:消息ID:{},原因:{}", correlationData, cause);
}
}
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
log.error("returnedMessage error:消息:{},消息码: {},原因:{},交换器:{}:路由键: {}", message, replyCode, replyText, exchange, routingKey);
}
}
Receiver.java
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
@Component
public class Receiver {
@RabbitListener(queues = "TestDirectRouting2")
public void process(Message message, Channel channel) throws IOException {
// 采用手动应答模式, 手动确认应答更为安全稳定
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
// System.out.println("receive: " + new String(message.getBody()));
System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
}
}
TestController.java
@GetMapping("/TestMessageAck")
public String TestMessageAck() {
String messageId = String.valueOf(UUID.randomUUID());
rabbitTemplate.convertAndSend("non.existent.exchange2", "TestDirectRouting2", messageId);
return "ok";
}
6启动项目测试
启动项目
调用接口
好的,报错了。
这个我找了半天,最后感谢https://blog.csdn.net/m912595719/article/details/83787486 的作者,让我解决了。
这个报错我试过了重启mq,删除队列和交换器都没有用,原因是
1表明这条消息对于MQ来说没找到,重点是这个---unknown delivery tag 1-参考rabbitMQ的tag机制可知这条消息已经完成了消费。
2这条消息被一个设置为自动确认的任务,程序会走自动确认消费,配置文件的配置就不生效了。
那咱们加一下下面的的方法,在RabbitConfig.java中加上下面代码
@Bean
public RabbitListenerContainerFactory> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return factory;
}
我们再重新启动项目,发请求。
成功了!