首页 > 代码库 > haproxy+keepalive 实现jboss负载均衡和高可用
haproxy+keepalive 实现jboss负载均衡和高可用
系统为centos 6.4 64位
主:192.168.1.61
备:192.168.1.62
浮动ip:192.168.1.64
realserver:192.168.1.60和192.168.1.63
第一:jboss的安装配置:
yum install java-1.7.0* -y
unzip jboss-5.1.0.GA.zip -d /usr/local
ln -sv jboss-5.1.0.GA jboss
cd /usr/local/jboss/server
cp -r default default_init
cp -r default jboss-gh1(项目的目录)
把项目的包放在 jboss-gh1目录下
修改配置文件让其能用域名访问
[root@localhost WEB-INF]# pwd
/usr/local/jboss/server/jboss-ghl/deploy/ghn.war/WEB-INF
[root@localhost WEB-INF]# cat jboss-web.xml
<?xml version=‘1.0‘ encoding=‘UTF-8‘ ?>
<!DOCTYPE jboss-web
PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>
<context-root>/</context-root>
<virtual-host>你的域名</virtual-host>
<virtual-host>192.168.1.63</virtual-host>
</jboss-web
/usr/local/jboss/server/jboss-ghl/deploy/jbossweb.sar/server.xml
<Host name="你的域名" autoDeploy="false"
deployOnStartup="false" deployXML="false">
<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log."
suffix=".log"
pattern="common"
directory="${jboss.server.log.dir}"
resolveHosts="false" />
</Host>
</Engine>
</Service>
</Server>
然后写启动的脚本了
为了方便测试,写了一测试页
[root@localhost ghn.war]# cat index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
information from 192.168.1.60(63上面改成63的ip)
</body>
</html>
[root@localhost ghn.war]# pwd
/usr/local/jboss/server/jboss-ghl/deploy/ghn.war
[root@localhost bin]# cat jboss-gh1.sh
#!/bin/bash
/usr/local/jboss/bin/run.sh -c jboss-ghl -b 0.0.0.0 -Djboss.service.binding.set=ports-01 -Djboss.messaging.ServerPeerID=1
[root@localhost bin]# pwd
/usr/local/jboss/bin
./jboss-gh1.sh &启动进程
到此jboss的配置完成。
第二:haproxy和keepalive的配置
haproxy和keepalived都是直接yum安装的
haproxy的配置(主从都是一样的)
[root@localhost haproxy]# cat haproxy.cfg
global
log 127.0.0.1 local0 info
maxconn 65536
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
nbproc 4
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
clitimeout 30000
srvtimeout 30000
contimeout 30000
frontend 60_ha
bind *:80
mode http
log global
option httplog
option dontlognull
option forwardfor
option httpclose
clitimeout 60000
acl test path_beg /ghn
acl test1 hdr_dom(host) -i 你的域名
use_backend istest if test or test1
backend istest
balance roundrobin
mode http
log global
option httplog
option httpclose
option forwardfor
contimeout 30000
srvtimeout 30000
fullconn 4000
server jbosstest1 192.168.1.60:8180 cookie c1 check inter 9000 rise 2 fall 3 weight 1 maxconn 2000
server jbosstest2 192.168.1.63:8180 cookie c1 check inter 9000 rise 2 fall 3 weight 1 maxconn 2000
service haproxy start 直接启动即可
keepalived的配置
主配置
[root@localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.64
}
}
备的配置
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state backup
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.64
}
}
service keepalived start直接启动即可
启动之后 在主的上面 查看ip地址
[root@localhost keepalived]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:3b:2d:8c brd ff:ff:ff:ff:ff:ff
inet 192.168.1.61/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.64/32 scope global eth0
inet6 fe80::5054:ff:fe3b:2d8c/64 scope link
valid_lft forever preferred_lft forever
备的ip地址
root@localhost keepalived]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:6f:b9:8d brd ff:ff:ff:ff:ff:ff
inet 192.168.1.62/24 brd 192.168.1.255 scope global eth0
inet6 fe80::5054:ff:fe6f:b98d/64 scope link
valid_lft forever preferred_lft forever
第三:测试
绑定hosts文件
192.168.1.64 你的域名
down掉主的keeplived,从的会立即接管浮动ip。实现无缝连接
本文出自 “路由汗水交换成功” 博客,请务必保留此出处http://ghnhl.blog.51cto.com/1630025/1546620
haproxy+keepalive 实现jboss负载均衡和高可用