RabbitMQ集群+Hapoxy负载安装

一.搭建单机RabbitMQ

1.安装Erlang环境
tar xvzf otp_src_R16B.tar.gz
cd otp_src_R14B03
./configure
根据 configure的提示安装一些其他包(yum -y install xxx)
检查完毕以后 
make
make install (需要用root权限) 

2.安装RabbitMQ
$ tar xvzf rabbitmq-server-3.3.1.tar.gz
$ cd rabbitmq-server-3.3.1
$ make
make  TARGET_DIR=/opt/rabbitmq SBIN_DIR= /opt/rabbitmq/ sbin MAN_DIR= /opt/rabbitmq /man  install 
同样,少什么用yum装什么 

3.开启监控后台
mkdir /etc/rabbitmq
./sbin/rabbitmq-plugins enable rabbitmq_management
删除后台guest用户只能在localhost登录的限制
cd /opt/rabbitmq/
ebin  
vim rabbit.app

把loopback_users里的<<"guest">>删掉
 

4.单机运行RabbitMQ
启动: ./sbin/rabbitmq-server start(后台运行: nohup ./sbin/
rabbitmq-server start >nohup.out&  )
停止:./sbin/
rabbitmqctl stop

5.访问监控后台
http://192.168.7.79:15672
guest/guest 

 

二.搭建Rabbit集群
1.在各台机器上安装rabbitMQ以后,先修改各台机器的hosts
192.168.130.245 为例(hostname: host130-245 )
vim /etc/hosts
127.0.0.1 host130-245
192.168.130.245 host130-245
192.168.130.246 host130-246


2.复制.erlang.cookie
在主节点服务器上复制 $HOST/.erlang.cookie 到各个从节点相同位置

cd ~
chmod 777 .erlang.cookie
scp .erlang.cookie root@
192.168.130.246:/root/
将主节点和其他子节点的.erlang.cookie权限改回400
chmod 400  
.erlang.cookie

3.采用-detached 启动各节点
cd /opt/rabbitmq/sbin/
./rabbitmqctl stop
nohup ./rabbitmq-server -detached >nohup.out&


4.组成集群
到子节点上执行

cd /opt/rabbitmq/sbin/
./rabbitmqctl stop_app
./rabbitmqctl join cluster --ram rabbit@host130-245(如果不加--ram 则默认是disk模式)
./rabbitmqctl start_app 
查看集群配置是否成功
./rabbitmqctl cluster_status 

5.设置镜像队列策略
在任意节点执行

./rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'

6.安装并配置HaProxy负载 

tar -zxvf haproxy-1.4.24.tar.gz
cd  
haproxy-1.4.24
make TARGET=linux24 PREFIX=/opt/haproxy   (此处为安装地址)
make install PREFIX= 
/opt/haproxy
从安装文件内复制haproxy.cfg 
cp 
haproxy-1.4.24/example/haproxy.cfg    haproxy/
编辑 
haproxy.cfg

 
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  dontlognull
        retries 3
        option  redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  rabbitmq_cluster 0.0.0.0:56720
        mode    tcp
        balance roundrobin
        server  host130-245 192.168.130.245:5672  check inter 2000 rise 2 fall 5
        server  host130-246 192.168.130.246:5672  check inter 2000 rise 2 fall 5

 7.启动haproxy
cd /opt/haproxy/sbin/
./haproxy -f ../haproxy.cfg
ps: 
[ALERT] 236/190345 (21350) : [haproxy.main()] Cannot chroot(/usr/share/haproxy).
如果遇到这个报错,就执行
 
mkdir /usr/share/haproxy
检查是否启动成功
netstat -anp|grep 56720
 
ps:所需的安装包的地址:
http://pan.baidu.com/s/1nuBO8B3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Spring Boot、RabbitMQ集群和Controller的完整示例: 1. 首先,需要在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 然后,在application.yml文件中添加以下RabbitMQ配置: ``` spring: rabbitmq: addresses: server1:5672,server2:5672 # RabbitMQ集群地址 username: guest # RabbitMQ用户名 password: guest # RabbitMQ密码 virtual-host: / # RabbitMQ虚拟主机 ``` 3. 创建一个RabbitMQ配置类,如下所示: ``` @Configuration public class RabbitMQConfig { @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setAddresses("server1:5672,server2:5672"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); connectionFactory.setVirtualHost("/"); return connectionFactory; } @Bean public RabbitTemplate rabbitTemplate() { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); return rabbitTemplate; } } ``` 4. 创建一个消息发送端,如下所示: ``` @Component public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void send(String message) { rabbitTemplate.convertAndSend("exchange", "routingKey", message); } } ``` 5. 创建一个消息接收端,如下所示: ``` @Component public class MessageReceiver { @RabbitListener(queues = "queue") public void receive(String message) { System.out.println("Received message: " + message); } } ``` 6. 创建一个Controller,如下所示: ``` @RestController public class MessageController { @Autowired private MessageSender messageSender; @PostMapping("/message") public void sendMessage(@RequestBody String message) { messageSender.send(message); } } ``` 7. 最后,启动多个应用程序实例,并通过发送POST请求来测试Controller和RabbitMQ集群是否正常工作。 注意:本示例中的exchange、routingKey和queue名称可以根据实际情况进行更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值