基于Apache和tomcat实现负载均衡

Posted maoyanqing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Apache和tomcat实现负载均衡相关的知识,希望对你有一定的参考价值。


坏境:192.168.200.112 tomcat1
192.168.200.103 tomcat2
192.168.200.111 Apache
Apache:yum -y install httpd*
systemctl start httpd
vim /etc/httpd/conf/httpd.conf
添加:<VirtualHost *:80>
ServerName www.ht.com
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / balancer://htcluster/
ProxyPassReverse / balancer://htcluster/
<Proxy balancer://htcluster>
Header add Set-Cookie "ROUTEID=.%BALANCER_WORKER_ROUTEe; path=/" env=BALANCER_ROUTE_CHANGED
BalancerMember http://192.168.200.103:8080/ loadfactor=5
BalancerMember http://192.168.200.112:8080/ loadfactor=5
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
</VirtualHost>
systemctl restart network
tomcat1服务器:
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# iptables -F
[root@localhost ~]# rpm -qa |grep -i openjdk
java-11-openjdk-headless-11.0.ea.28-7.el7.x86_64
java-11-openjdk-11.0.ea.28-7.el7.x86_64

[root@localhost ~]# rpm -e java-11-openjdk-11.0.ea.28-7.el7.x86_64
rpm -e java-11-openjdk-headless-11.0.ea.28-7.el7.x86_64 --nodeps ##--nodeps解除依赖包的安装
[root@localhost ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@localhost ~]# mv jdk1.8.0_191/ /usr/local/java
[root@localhost ~]# /usr/local/java/bin/java -version
cat /etc/profile

export JAVA=/usr/local/java
export PATH=$PATH:$JAVA/bin
[root@localhost ~]# source /etc/profile
[root@localhost ~]# tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.40 /usr/local/tomcat8
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
[root@localhost ~]# mkdir -pv /web/webapp
[root@localhost ~]# vim /web/webapp/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test page</title>
</head>
<body>
<% out.println("Welcome to test site,http://www.test1.com");%>
</body>
</html>
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml
148行添加:
<Context docBase="/web/webapp" path="" reloadable="flase">
</Context>
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
tomcat2服务器:
唯一不同的地方就是:
[root@localhost ~]# vim /web/webapp/index.jsp
里面的test1改成test2
修改主机hosts文件
192.168.200.111 www.ht.com
测试:www.ht.com

以上是关于基于Apache和tomcat实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

apache分别基于三种方案实现tomcat的代理负载均衡及会话绑定

Apache+Tomcat 实现负载均衡及seesion复制

Apache反向代理结合Tomcat集群来实现负载均衡概念理解

Tomcat 负责均衡及cluster session

apache+tomcat 负载均衡

用apache和tomcat搭建集群,实现负载均衡