先做一个简单的配置并测试。新建haproxy配置文件${haproxy.home}/conf/haproxy.cfg,输入上述配置内容后保存(配置文件可以任意命名,只要启动时指定配置文件路径即可 [-f ${cfgFilePath}])。执行${haproxy.home}/sbin/haproxy –f ${haproxy.home}/conf/haproxy.cfg启动haproxy进程。如果不知道命令参数,可以输入-h查看帮助信息。
执行${haproxy.home}/sbin/haproxy -f ${haproxy.home}/conf/haproxy.cfg启动haproxy进程;
--------------------------------------分割线 --------------------------------------
Haproxy+Keepalived搭建Weblogic高可用负载均衡集群
CentOS 6.3下Haproxy+Keepalived+Apache配置笔记
Haproxy + KeepAlived 实现WEB群集 on CentOS 6
--------------------------------------分割线 --------------------------------------
编写一个JSP文件放入tomcat自带的example应用中(每个server里都放置一份),查看每次访问的server信息(可以在每个tomcat启动参数中加入不同的值-D${key}=${value},方便测试),JSP文件内容如下:
<%@pageimport="java.util.Properties"%>
<%@pageimport="java.net.InetAddress"%>
<%@pagelanguage="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
InetAddress address = InetAddress.getLocalHost();
String hostAddress = address.getHostAddress();
String hostName = address.getHostName();
Properties props = System.getProperties();
%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Test HAProxy HTTP mode</title>
</head>
<body>
<spanstyle="color: blue;">ZhongWen.Li (lizw@primeton.com)</span>
<tablewidth="100%"border="0.1">
<thead>
<tr>
<thcolspan="2"align="center">Server information</th>
</tr>
</thead>
<tbody>
<tr>
<td>SERVER ADDRESS</td>
<td><%=hostAddress %></td>
</tr>
<tr>
<td>SERVER NAME</td>
<tdstyle="color: red;"><%=hostName %></td>
</tr>
<tr>
<td>Session ID</td>
<td><%=session.getId() %></td>
</tr>
<tr>
<tdcolspan="2"align="center">System information</td>
</tr>
<%for (Object key : props.keySet()) { %>
<tr>
<td><%=key %></td>
<td><%=props.get(key) %></td>
</tr>
<% } %>
</tbody>
</table>
</body>
</html>
直接访问应用容器1
直接访问应用容器2