windows搭建rabbitmq鏡像集羣
1.簡介
鏡像集羣可以實現消息的備份,一個出現問題,可以直接切到備份
服務器分別是102,103
2.下載erlang,安裝,rabbitmq解壓到d盤,注意版本之間的兼容性
erlang
鏈接: https://pan.baidu.com/s/15pjgfyDUpNi_eppjuaZKaQ 密碼: b93v
rabbitmq
鏈接: https://pan.baidu.com/s/1A2Ickthf2KRiYyl-RB0WvQ 密碼: rx4c
3.創建環境變量
ERLANG_HOME C:\Program Files\erl9.3
4.創建下面的目錄備用
D:\rabbitmq-server-windows-3.7.6_storage\db
D:\rabbitmq-server-windows-3.7.6_storage\log
5.下載rabbitmq.config放到etc目錄下
鏈接: https://pan.baidu.com/s/1cg-HdedoyiYFd2Njv6XQbg 密碼: u2nx
6.兩個節點102,103分貝修改rabbitmq-env.bat,第16行加入
--102--
set RABBITMQ_CONFIG_FILE=!RABBITMQ_HOME!\etc\rabbitmq
set RABBITMQ_LOG_BASE=D:\rabbitmq-server-windows-3.7.6_storage\log
set RABBITMQ_MNESIA_BASE=D:\rabbitmq-server-windows-3.7.6_storage\db
set RABBITMQ_NODENAME=rabbit@node1
set RABBITMQ_NODE_IP_ADDRESS=10.168.100.102
set RABBITMQ_NODE_PORT=5672
--103--
set RABBITMQ_CONFIG_FILE=!RABBITMQ_HOME!\etc\rabbitmq
set RABBITMQ_LOG_BASE=D:\rabbitmq-server-windows-3.7.6_storage\log
set RABBITMQ_MNESIA_BASE=D:\rabbitmq-server-windows-3.7.6_storage\db
set RABBITMQ_NODENAME=rabbit@node2
set RABBITMQ_NODE_IP_ADDRESS=10.168.100.103
set RABBITMQ_NODE_PORT=5672
7.兩個節點配置host
--102--
10.168.100.102 node1
10.168.100.103 node2
ipconfig /flushdns
--103--
10.168.100.103 node2
10.168.100.102 node1
ipconfig /flushdns
8.分別安裝啓動兩個節點服務
切換到D:\rabbitmq-server-windows-3.7.6\rabbitmq_server-3.7.6\sbin
rabbitmq-service.bat --install
9.同步cookie,102拷貝到103覆蓋,反之也可以,cookie用來驗證的
C:\Users\登錄用戶名\.erlang.cookie
C:\Windows\System32\config\systemprofile\.erlang.cookie
10.配置集羣,在103節點下執行
rabbitmqctl.bat stop_app
rabbitmqctl.bat reset
rabbitmqctl.bat join_cluster --ram rabbit@node1
rabbitmqctl.bat start_app
11.配置鏡像集羣
在102節點下瀏覽器執行 http://localhost:15672
guest,guest登錄管理端
在admin下policy下添加一個all策略,來實現消息備份
12.客戶端,rabbitmq.client
public class RabbitmqHelper
{
private bool IsOnline = false;
private IConnection _conn;
private IModel _channel;
private string _exchangePrefix = "wzexchange_";
private string _routingKeyPrefix = "wzroutingKey_";
public RabbitmqHelper(string uri = "")
{
if (string.IsNullOrEmpty(uri))
{
uri = IsOnline
? string.Format("amqp://{0}:{1}@{2}:{3}/{4}", "guest", "guest", "localhost", "5672", "/")
: string.Format("amqp://{0}:{1}@{2}:{3}/{4}", "wztest", "wztest", "10.168.100.102", "5672", "/");
}
InitConfig(uri);
}
private void InitConfig(string uri)
{
ConnectionFactory factory = new ConnectionFactory
{
Uri = new Uri(uri)
};
_conn = factory.CreateConnection();
_channel = _conn.CreateModel();
}
public bool QueueCreate(string queueName, bool durable = false)
{
QueueDeclareOk queueDeclareOk = _channel.QueueDeclare(queueName, durable, false, false, null);
if (string.IsNullOrEmpty(queueDeclareOk.QueueName))
{
return false;
}
_channel.ExchangeDeclare(_exchangePrefix + queueName, ExchangeType.Direct, durable);
_channel.QueueBind(queueName, _exchangePrefix + queueName, _routingKeyPrefix + queueName, null);
return true;
}
public bool QueueDelete(string queueName,bool constraint = false)
{
int i = (int)_channel.QueueDelete(queueName, !constraint, !constraint);
return i > 0;
}
public int QueueLength(string queueName)
{
QueueDeclareOk queueDeclareOk = _channel.QueueDeclarePassive(queueName);
return Convert.ToInt32(queueDeclareOk.MessageCount);
}
public int QueueConsumerCount(string queueName)
{
QueueDeclareOk queueDeclareOk = _channel.QueueDeclarePassive(queueName);
return Convert.ToInt32(queueDeclareOk.ConsumerCount);
}
public bool QueueClear(string queueName)
{
int i = (int) _channel.QueuePurge(queueName);
return i > 0;
}
public bool QueuePush(string queueName,string message,bool durable = false)
{
byte[] messageBodyBytes = Encoding.UTF8.GetBytes(message);
IBasicProperties props = _channel.CreateBasicProperties();
props.ContentType = "text/plain";
props.DeliveryMode = 2;
_channel.BasicPublish(_exchangePrefix + queueName, _routingKeyPrefix + queueName, durable ? props : null, messageBodyBytes);
return true;
}
public MqModel QueuePull(string queueName,bool autoAck = true)
{
BasicGetResult result = _channel.BasicGet(queueName, autoAck);
if (result == null)
{
return null;
}
byte[] body = result.Body;
return new MqModel()
{
Mq = Encoding.UTF8.GetString(body),
DeliveryTag = (int) result.DeliveryTag
};
}
public bool Ack(int deliveryTag)
{
_channel.BasicAck((uint)deliveryTag, false);
return true;
}
public bool QueueClose()
{
if (_channel != null && _channel.IsOpen)
{
_channel.Close();
}
if (_conn != null && _conn.IsOpen)
{
_conn.Close();
}
return true;
}
~RabbitmqHelper()
{
QueueClose();
}
}
public class MqModel
{
public string Mq { get; set; }
public int DeliveryTag { get; set; }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.