且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

利用HAProxy取代nginx代理activemq

更新时间:2022-09-11 17:10:55

由于nginx默认仅支持http应用层协议的端口,对四层tcp端口支持不好,需要安装额外的nginx_tcp_proxy_module模块,因此在不方便重新编译nginx模块时,可以用HAProxy代理activemq的tcp端口,只需要6个步骤就可以完成(以Ubuntu为例,CentOS大同小异)。

步骤1:安装HAProxy

1
apt-get install haproxy

步骤2:配置HAProxy配置文件

vim编辑/etc/haproxy/haproxy.cfg,将defaults段中的mode     http和option     httplog注释掉,添加一个tcp代理,如下文所示:

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
global     
     log /dev/log     local0     
     log /dev/log     local1 notice     
     chroot /var/lib/haproxy     
     user haproxy     
     group haproxy     
     daemon     
defaults     
     log     global     
     #mode     http     
     #option     httplog     
     option     dontlognull     
        contimeout 5000     
        clitimeout 50000     
        srvtimeout 50000     
     errorfile 400 /etc/haproxy/errors/400.http     
     errorfile 403 /etc/haproxy/errors/403.http     
     errorfile 408 /etc/haproxy/errors/408.http     
     errorfile 500 /etc/haproxy/errors/500.http     
     errorfile 502 /etc/haproxy/errors/502.http     
     errorfile 503 /etc/haproxy/errors/503.http     
     errorfile 504 /etc/haproxy/errors/504.http     
listen activemq_cluster 0.0.0.0:61616     
    mode tcp     
    balance source     
    option tcpka     
    option tcplog     
    server  activemqnode1 192.168.100.81:61616 check inter 2000 rise 2 fall 3

步骤3:测试HAProxy配置文件是否正确

1
/usr/sbin/haproxy -c -f /etc/haproxy/haproxy.cfg

步骤4:启用HAProxy,允许init脚本启动HAProxy,如/etc/init.d/haproxy start或service haproxy start等

1
2
3
4
5
6
7
# /etc/default/haproxy ,ENABLED=1
sed -i 's/ENABLED=0/ENABLED=1/g' /etc/default/haproxy
cat /etc/default/haproxy
# Set ENABLED to 1 if you want the init script to start haproxy.      
ENABLED=1       
# Add extra flags here.       
#EXTRAOPTS="-de -m 16"

步骤5:启动HAProxy并检查运行结果

1
2
3
/etc/init.d/haproxy start或service haproxy start
/etc/init.d/haproxy status或service haproxy status
netstat –anop | grep haproxy或ps –ef | grep haproxy | grep v grep

步骤6:允许防火墙通过设定的tcp端口

1
ufw allow 61616/tcp

接下来就可以测试和使用activemq了。

--end--





本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1680034,如需转载请自行联系原作者