RabbitMQ安装
docker-hub/rabbitmq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| version: '3' services:
rabbitmq: image: rabbitmq restart: always hostname: xuanps environment: RABBITMQ_DEFAULT_USER: root RABBITMQ_DEFAULT_PASS: ******* ports: - 14002:5672 volumes: - "/dockerdata/v-rabbitmq:/var/lib/rabbitmq" deploy: replicas: 1 restart_policy: condition: on-failure placement: constraints: [node.hostname == xuanps]
rabbitmq: image: rabbitmq:3.7-management-alpine restart: always hostname: me environment: RABBITMQ_DEFAULT_USER: test RABBITMQ_DEFAULT_PASS: test ports: - 14012:5672 - 14013:15672 volumes: - "/home/dockerdata/v-nantian-dev/rabbitmq:/var/lib/rabbitmq" deploy: replicas: 1 restart_policy: condition: on-failure placement: constraints: [node.hostname == me]
|
springboot 连接mq
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
|
mq连接配置application.yml
1 2 3 4 5 6
| spring: rabbitmq: host: 112.74.51.136 port: 14002 username: root password: ********
|
mq发送数据(测试类)
1 2 3 4 5 6 7 8
| @Autowired private AmqpTemplate rabbitTemplate; @Test public void send() { String context="hello"+new Date(); System.out.print("send context:"+context); rabbitTemplate.convertAndSend("hello",context); }
|
mq配置类MqConfig.java
1 2 3 4 5 6 7 8
| import org.springframework.amqp.core.Queue; @Configuration public class MqConfig { @Bean public Queue helloQueue(){ return new Queue("hello"); } }
|
mq接受数据类MqReceiver.java
1 2 3 4 5 6 7 8
| @Component @RabbitListener(queues = "hello") public class MqReceiver { @RabbitHandler public void process(String hello){ System.out.print("receiver:"+hello); } }
|
测试效果:先启动主程序,再点击测试类发送,主程序就可以接收到消息了。(先发送后启动主程序是接受不到的)
centos7 install RabbitMQ
首先下载安装包:erlang、socat、rabbitmq以此用rpm -ivh <>
安装这三个
新建修改配置文件vi /etc/rabbitmq/rabbitmq.config
1 2 3
| [ {rabbit, [{tcp_listeners, [5672]}, {loopback_users, ["admin"]}]} ].
|
启动rabbitmq服务
1 2 3 4
| systemctl start rabbitmq-server
systemctl status rabbitmq-server
|
配置远程管理web界面
1 2 3
| rabbitmq-plugins enable rabbitmq_management rabbitmq-plugins enable rabbitmq_stomp rabbitmq-plugins enable rabbitmq_web_stomp
|
配置用户远程访问
1 2 3 4
| rabbitmqctl add_user test test rabbitmqctl set_user_tags test administrator rabbitmqctl set_permissions -p / test '.*' '.*' '.*'
|
重启rabbitmq服务执行systemctl restart rabbitmq-server
添加防火墙访问端口
1 2 3 4 5 6
| firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
|
测试,浏览器访问http://<服务器ip>:15672/
使用test,test用户密码登陆
常见问题
问题1:.erlang.cookie must be accessible by owner only
不断重启
解决:删除整个挂载目录,包括rabbitmq
,然后重建