Rabbitmq cluster on docker-compose

来自linux中国网wiki
跳到导航 跳到搜索

进阶

http://rabbitmq.mr-ping.com/tutorials_with_python/[1]Hello_World.html


透彻rabbitmq

消息队列的使用场景是怎样的

https://www.rabbitmq.com/getstarted.html

名师讲坛——Java微服务架构实战(SpringBoot+SpringCloud+Docker+RabbitMQ http://product.china-pub.com/8066694#ml

http://product.china-pub.com/8063896#ml

Spring Boot微服务实战 使用RabbitMQ、Eureka、Ribbon、Zuul和Cucumber开发RESTful服务

站内资源

Rabbitmq常用命令

docker简易搭建RabbitMQ集群HA-good

ins rabbitmq

 Sep 22 2021
 可以用alpine 

cat docker-compose.yml 
version: '3.2'  
services:
  rabbitmq:
    image: rabbitmq:3.8.22-management-alpine
    ports:
      - "5672:5672"
      - "4369:4369"
      #- "1883:1883"
      - "15672:15672"
      - "25672:25672"

    container_name: rabbitmq01

    hostname: rabbitmq01
    environment:

      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /

      RABBITMQ_DEFAULT_USER: dddadmin

      RABBITMQ_DEFAULT_PASS: ddddSNTTo886Gq

      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log

      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log

    volumes:
      - ./data/rabbitmq:/var/lib/rabbitmq

    restart: always  


info

 适合于 3.610 and 3.7.5 版本
 -p 15672:15672 http访问端口
 -p 5672:5672 amqp访问端口
 -p 4369 #unable to connect to epmd (port 4369) on rabbitmq01: address (cannot connect to host/port)

Thu Sep 10 18:33:00 CST 2020  试过了 这个 版本现在的能用 
sed  -i 's!rabbitmq:3.6.10-management!rabbitmq:3.7.26-management-alpine!' docker-compose.yml



git 
cd  /data/apps
git clone https://github.com/evan886/mq.git 
mv  mq/docker-compose-mqcluster/   . && rm  -rf  mq

把你要的在那台机器改为 docker-compose.yml 和改好相关配置 然后删除其它的 


 

https://x-team.com/blog/set-up-rabbitmq-with-docker-compose/

下面开始的是mq cluster

mq1

Note

  如果是 alpine  rabbitmq:3.6.10-management  换成   其它基础不用变,除了进入容器变成 sh 
  sed  -i 's!rabbitmq:3.6.10-management!rabbitmq:3.7.26-management-alpine!' docker-compose.yml

 有一种是 node1 不加别 , node2  add node1  ; node3  add node1 and node2  但是  rabbitmqctl cluster_status 可能看起来有些node alarms是down的 不过通过浏览器的不会   

#记得  extra_hosts 改成你的相关IP
mkdir -p /home/data/docker-compose-mqcluster
cd /home/data/docker-compose-mqcluster
##"登录容器请执行:rabbitmq-plugins enable rabbitmq_management 才可>使用WEB管理页面"
vi docker-compose.yml

version: '3.1'  
services:
  rabbitmq:
    image: rabbitmq:3.6.10-management
    ports:
      - "5672:5672"
      - "4369:4369"
      #- "1883:1883"
      - "15672:15672"
      - "25672:25672"

    container_name: rabbitmqCluster01

    hostname: rabbitmq01
    extra_hosts:
      #- "rabbitmq01:192.168.10.211"#不要加上自己,不然会老是重启的
      - "rabbitmq02:192.168.10.212" 
      - "rabbitmq03:192.168.10.213"
    environment:

      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /

      RABBITMQ_DEFAULT_USER: lxtxadmin

      RABBITMQ_DEFAULT_PASS: tSNTTo886Gq

      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log

      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log

    volumes:
      - ./data/rabbitmq:/var/lib/rabbitmq

    restart: always  
# docker-compose  up -d 

mq2

mkdir -p /home/data/docker-compose-mqcluster
cd /home/data/docker-compose-mqcluster

vi docker-compose.yml
 
version: '3.1'  
services:
  rabbitmq:
    image: rabbitmq:3.6.10-management

    ports:
      - "5672:5672"
      - "4369:4369"
      #- "1883:1883"
      - "15672:15672"
      - "25672:25672"

    container_name: rabbitmqCluster02
    hostname: rabbitmq02
    extra_hosts:
      - "rabbitmq01:192.168.10.211"
      #- "rabbitmq02:192.168.10.212" #不要加上自己,不然会老是重启的
      - "rabbitmq03:192.168.10.213"
    environment:
      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /
      RABBITMQ_DEFAULT_USER: lxtxadmin
      RABBITMQ_DEFAULT_PASS: tSNTTo886Gq
      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log
      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log
      CLUSTERED: 'true'
      CLUSTER_WITH: rabbit1
      RAM_NODE: 'true'

    volumes:
      - ./data/rabbitmq:/var/lib/rabbitmq
    restart: always  

mq3

mkdir -p /home/data/docker-compose-mqcluster
cd /home/data/docker-compose-mqcluster

vi docker-compose.yml

 
version: '3.1'  
services:
  rabbitmq:
    image: rabbitmq:3.6.10-management

    ports:
      - "5672:5672"
      - "4369:4369"
      #- "1883:1883"
      - "15672:15672"
      - "25672:25672"

    container_name: rabbitmqCluster03

    hostname: rabbitmq03
    extra_hosts:
      - "rabbitmq01:10.3.10.141"
      - "rabbitmq02:10.3.10.142"
    environment:

      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /

      RABBITMQ_DEFAULT_USER: lxtxadmin

      RABBITMQ_DEFAULT_PASS: tSNTTo886Gq

      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log

      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log
      CLUSTERED: 'true'
      CLUSTER_WITH: rabbit1
      RAM_NODE: 'true'

    volumes:

      - ./data/rabbitmq:/var/lib/rabbitmq

    restart: always  

config

将节点2,3加入集群

#在rabbit2机器进入容器的命令行
sudo docker exec -it rabbitmqCluster02  sh  #/bin/bash

#加入集群
rabbitmqctl stop_app  && rabbitmqctl join_cluster rabbit@rabbitmq01  &&rabbitmqctl start_app

rabbit3执行相同的命令
docker exec -it rabbitmqCluster03  sh #/bin/bash
rabbitmqctl stop_app && rabbitmqctl join_cluster rabbit@rabbitmq01  &&rabbitmqctl start_app


查询集群状态
rabbitmqctl cluster_status

nginx 配置


#如果是yum nginx  在放在 conf.d/mq.conf
   server {
        listen       80;
        server_name  mq.com;
 

        location / {
            proxy_pass http://10.3.10.141:15672;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           
        }
     }


NOTE  selinux  记得关掉  不然可能是502哦 
#SELinux设置
getenforce   #查看SELINUX工作模式

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #禁用SELINUX
 setenforce 0 #临时禁用SELINUX,无需重启
 
 
 SELINUX=enforcing
#此项定义selinux状态。
#enforcing—是强制模式系统受selinux保护。就是你违反了策略,你就无法继续操作下去
#permissive—是提示模式系统不会受到selinux保护,只是收到警告信息。permissive就是Selinux有效,但是即使你违反了策略的话它让你继续操作,但是把你的违反的内容记录下来(警告信息)
#disabled—禁用selinux。



下面的配置是不对的 会跑到框架里面去了

页面错误!请稍后再试~


   listen       80;

    server_name rabbitmq.com;
    #index       index.html index.html index.php;

    #charset koi8-r;
    charset     utf-8;


    location /{
        proxy_pass http://cfb.rabbitmq.server:15672;
        #proxy_set_header Host $host:$server_port;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
       #proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #index    index.html index.htm index.php;
    }

镜像模式

在我的wiki上 开启镜像模式

RabbitMQ实现延迟队列(使用delay插件


docker cp rabbitmq_delayed_message_exchange-3.6.6.ez   c77afe9fa73f:/plugins


: 进入docker容器内 docker exec  -t rabbit  bash

8. 执行命令让插件生效: 启动延时插件:
rabbitmq-plugins enable rabbitmq_delayed_message_exchange

https://www.jianshu.com/p/197715cea172

troubleshooting


1.docker-compose.yml文件报错
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.rabbitmq.environment.CLUSTERED contains true, which is an invalid type, it should be a string, number, or a null


CLUSTERED: true -->CLUSTERED: 'true'


绑hosts的前进 不要全局FQ  SSR常常坑我们 


问题2 
15672 端口不通 也就是web界面打不开 

问题 镜像用了 rabbitmq 而不是rabbitmq-manager

发现过程 web打不开   15672 端口不能 ,而 5672是通的 进入容器发现是ubuntu 而我记得以前是debian呀 问了下sy才知道 他换了,没全用我的,晕死 ,坑自己人啊 

补救办法  开启 rabbitmq-plugins enable rabbitmq_management


crash dump is being written to: erl_crash.dump...sh: echo: I/O error
原来是空间暴了呢


https://www.rabbitmq.com/management.html

RabbitMQ镜像集群脑裂


现象:
RabbitMQ GUI上显示

Network partition detected
Mnesia reports that this RabbitMQ cluster has experienced a network partition. There is a risk of losing data. Please read RabbitMQ documentation about network partitions and the possible solutions.


节点3 是挂,以节点1 为准  在 node2  node3 上 分别执行
rabbitmqctl stop_app
rabbitmqctl start_app

就行 

端口在 进入容器 cannot exec in a stopped state: unknown

今天是端口在 docker restart id 都不行,进程也还在的 重启了 docker 服务才行了

rabbitmq node not running


行情 重启了一下 docker  mq集群就好了 

试过 telnet  15276 有点不太对 一下就跳出来了  

也登录不了容器  一开始还以为是防火墙  



 docker exec  -it af1934d27e21 /bin/bash 
root@rabbitmq02:/# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq02
[{nodes,[{disc,[rabbit@rabbitmq01,rabbit@rabbitmq02,rabbit@rabbitmq03]}]},
 {alarms,[]}]



再想加入 报错
:/# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbitmq02

root@rabbitmq02:/# rabbitmqctl join_cluster rabbit@rabbitmq01
Clustering node rabbit@rabbitmq02 with rabbit@rabbitmq01
Error: unable to connect to node rabbit@rabbitmq02: nodedown

DIAGNOSTICS
===========

attempted to contact: [rabbit@rabbitmq02]

rabbit@rabbitmq02:
  * connected to epmd (port 4369) on rabbitmq02
  * epmd reports node 'rabbit' running on port 25672
  * TCP connection succeeded but Erlang distribution failed

  * Distribution failed unexpectedly while waiting for challenge: {error,
                                                                   closed}


current node details:
- node name: 'rabbitmq-cli-69@rabbitmq02'
- home dir: /var/lib/rabbitmq
- cookie hash: /HHYeuND8Y2blMJ8WoOg1g==

root@rabbitmq02:/# rabbitmqctl start_app
Starting node rabbit@rabbitmq02
Error: unable to connect to node rabbit@rabbitmq02: nodedown


DIAGNOSTICS
===========

attempted to contact: [rabbit@rabbitmq02]

rabbit@rabbitmq02:
current node details:
- node name: 'rabbitmq-cli-69@rabbitmq02'
- home dir: /var/lib/rabbitmq
- cookie hash: /HHYeuND8Y2blMJ8WoOg1g==

root@rabbitmq02:/# rabbitmqctl start_app
Starting node rabbit@rabbitmq02
Error: unable to connect to node rabbit@rabbitmq02: nodedown


DIAGNOSTICS
===========

attempted to contact: [rabbit@rabbitmq02]

rabbit@rabbitmq02:
  * connected to epmd (port 4369) on rabbitmq02
  * epmd reports node 'rabbit' running on port 25672
  * TCP connection succeeded but Erlang distribution failed

  * TCP connection to remote host has timed out. Is the Erlang distribution using TLS?


current node details:
- node name: 'rabbitmq-cli-97@rabbitmq02'
- home dir: /var/lib/rabbitmq
- cookie hash: /HHYeuND8Y2blMJ8WoOg1g==

重启 docker 服务  再 如上添加 不过提示 The node is already a member of this cluster  所以应该是重启docker 就好了

今天是 监听的IP不对 模板的是10 我用的 192.168

单机版集群

version: '3.1'  
services:
  rabbitmq01:
    image: rabbitmq:3.6.10-management
    ports:
      - "5672:5672"
        #- "4369:4369"
      - "15672:15672"
        #- "25672:25672"
    container_name: rabbitmqCluster01
    hostname: rabbitmq01
    environment:
      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /
      RABBITMQ_DEFAULT_USER: lxtxadmin
      RABBITMQ_DEFAULT_PASS: tSNTTo886Gq
      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log
      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log
    volumes:
      - ./data/rabbitmq:/var/lib/rabbitmq
    restart: always  


  rabbitmq02:
    image: rabbitmq:3.6.10-management
    ports:
      - "5673:5672"
        #- "4369:4369"
      - "15673:15672"
        #- "25672:25672"
    container_name: rabbitmqCluster02
    hostname: rabbitmq02
    links:
      - rabbitmq01
    environment:
      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /
      RABBITMQ_DEFAULT_USER: lxtxadmin
      RABBITMQ_DEFAULT_PASS: tSNTTo886Gq
      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log
      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log
    volumes:
      - ./data/rabbitmq02:/var/lib/rabbitmq
    restart: always  


  rabbitmq03:
    image: rabbitmq:3.6.10-management
    ports:
      - "5674:5672"
        #- "4369:4369"
      - "15674:15672"
        #- "25672:25672"
    container_name: rabbitmqCluster03
    hostname: rabbitmq03
    links:
      - rabbitmq01
    environment:
      RABBITMQ_ERLANG_COOKIE: 'rabbitmqCookie'   
      RABBITMQ_DEFAULT_VHOST: /
      RABBITMQ_DEFAULT_USER: lxtxadmin
      RABBITMQ_DEFAULT_PASS: tSNTTo886Gq
      RABBITMQ_LOGS: /var/lib/rabbitmq/rabbitmq.log
      RABBITMQ_SASL_LOGS: /var/lib/rabbitmq/rabbitmq-sasl.log
    volumes:
      - ./data/rabbitmq03:/var/lib/rabbitmq
    restart: always  





然后再加入集群 同上 

python操作RabbitMQ


pip3 install pika 


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# auth :   send.py
import pika
# ######################### 生产者 #########################
credentials = pika.PlainCredentials('admin', 'tSNTTo886Gq')
#链接rabbit服务器(localhost是本机,如果是其他服务器请修改为ip地址)
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.10.51',5672,'/',credentials))
#创建频道
channel = connection.channel()
# 声明消息队列,消息将在这个队列中进行传递。如果将消息发送到不存在的队列,rabbitmq将会自动清除这些消息。如果队列不存在,则创建
channel.queue_declare(queue='hello')
#exchange -- 它使我们能够确切地指定消息应该到哪个队列去。
#向队列插入数值 routing_key是队列名 body是要插入的内容

channel.basic_publish(exchange='',
                  routing_key='hello',
                  body='Hello World!')
print("开始队列")
#缓冲区已经flush而且消息已经确认发送到了RabbitMQ中,关闭链接
connection.close()




#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# auth :  receive.py

import pika

# ########################## 消费者 ##########################
credentials = pika.PlainCredentials('admin', 'tSNTTo886Gq')
#链接rabbit服务器(localhost是本机,如果是其他服务器请修改为ip地址)
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.10.51',5672,'/',credentials))
channel = connection.channel()

# 声明消息队列,消息将在这个队列中进行传递。如果队列不存在,则创建
channel.queue_declare(queue='hello')


# 定义一个回调函数来处理,这边的回调函数就是将信息打印出来。
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)



# 告诉rabbitmq使用callback来接收信息  
chan.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
#TypeError: basic_consume() got multiple values for argument 'queue'

#channel.basic_consume(callback,
#                      queue='hello',
#                      no_ack=True)
 # no_ack=True表示在回调函数中不需要发送确认标识

print(' [*] Waiting for messages. To exit press CTRL+C')

# 开始接收信息,并进入阻塞状态,队列里有信息才会调用callback进行处理。按ctrl+c退出。
channel.start_consuming()


 python3  receive.py 
 [*] Waiting for messages. To exit press CTRL+C
 [x] Received b'Hello World!'

py mq

http://rabbitmq.mr-ping.com/


python - 操作RabbitMQ

python 操作RabbitMq详解

https://www.rabbitmq.com/tutorials/tutorial-one-python.html


https://www.jianshu.com/p/e5c9f85e18f1

https://www.cnblogs.com/kerwinC/p/5967584.html


see also

https://www.rabbitmq.com/clustering.html#peer-discovery-how-does-it-work

RabbitMQ 集群高可用原理及实战部署介绍

RabbitMQ 集群部署

Setting up a RabbitMQ Cluster on Docker

Docker分布式部署RabbitMQ集群

多台机器 值得看 Docker:搭建RabbitMQ集群

docker-compose 安装搭建 RabbitMQ 集群

单一台机器母机docker简易搭建RabbitMQ集群

RabbitMQ之集群搭建

也是单物理机么 ? https://www.cnblogs.com/vipstone/p/9362388.html

docker简易搭建RabbitMQ集群

rabbitmq 原理、集群、基本运维操作、常见故障处理