apache+tomcat的架构
===================================
keepalived实现apache的高可用
在tomcat上搭建JspRun论坛
apache三种方式实现tomcat的反向代理
apache三种方式实现tomcat的负载均衡
===================================
环境:
OS:Centos 6.x(redhat 6.x)
yum源:
[centos]name=sohu-centosbaseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearchgpgcheck=1enable=0gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6[epel]name=sohu-epelbaseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/enable=1gpgcheck=0
拓扑图:
拓扑图的规划:
IP 地址 | 所需安装软件 | |
apache1 | Eth0:172.16.22.1 VIP1:172.16.22.100 | Keepalived+apache+tomcat-connectors |
apache2 | Eth0:172.16.22.2 VIP1:172.16.22.100 | Keepalived+apache+tomcat-connectors |
tomcat1 | Eth0:172.16.22.3 | Jdk+tomcat+mysql-connector-java |
tomcat2 | Eth0:172.16.22.4 | Jdk+tomcat+mysql-connector-java |
mysql1 | Eth0:172.16.22.5 VIP2:172.16.22.200 | Corosync+pacemaker+Mysql+drbd |
mysql2 | Eth0:172.16.22.6 VIP2:172.16.22.200 | Corosync+pacemaker+Mysql+drbd |
部分软件下载
需解决的问题:
防止apache单点故障?
keepalived+apache实现apache的高可用
防止mysql单点故障?
corosync+pacemaker+mysql+drbd实现mysql的高可用
apache做tomcat反向代理的三种方式?
用三种模块mod_proxy、mod_proxy_ajp、mod_jk,需要用哪种就开启哪种的模块即可
apache做tomcat负载均衡的三种方式?
用三种模块mod_proxy、mod_proxy_ajp、mod_jk,需要用哪种就开启哪种的模块且还要开启负载均衡的模块,以及负载均衡算法的模块
apache怎么连接tomcat?
mod_proxy模块
mod_proxy_ajp模块
前面两种模块直接在配置文件里面加载即可,就可以连接tomcat
mod_jk模块 用这个模块还要安装apache连接tomcat的连接器,tomcat-connectors这个软件包
tomcat怎么连接mysql?
每个连接mysql的软件包都要安装相应的连接mysql的驱动,tomcat连接mysql的驱动的软件包是mysql-connector-java
怎么实现session的会话保持?
实现session会话保持的方法有很多,tomcat自身也可以保持session,这种保持会话就是运维人员的工作了,运维人员直接在tomcat的配置文件中实现,用memcached或者redis保持session,memcached保持session有一定的缺陷,即memcached服务挂了,会话还是丢失了,但是memcached直接把session保存在内存中,所以速度可想而知,redis可以永久的保存session,这种保存session需要开发人员在开发中程序中自己定义,还有可以把session保存在数据库中,这种也是需要用程序定义,session如何保存,不同公司有不同的方法,保存在哪里还是需要运维人员和开发人员共同探讨合作。
一、每个服务器相关软件的安装
apache1上相关软件的安装:
#================yum安装keepalived======================================[root@jie1 ~]# yum -y install keepalived#================源码编译安装httpd-2.4.6================================[root@jie1 ~]# tar xf apr-1.4.6.tar.bz2[root@jie1 ~]# tar xf apr-util-1.5.2.tar.bz2[root@jie1 ~]# tar xf httpd-2.4.6.tar.bz2#===========安装开发库,和依赖性包======================================[root@jie1 ~]# yum -y groupinstall "Development tools" "Server Platform Development"[root@jie1 ~]# yum -y install pcre-devel#=============编译安装apr软件===========================================[root@jie1 ~]# cd apr-1.4.6[root@jie1 apr-1.4.6]# ./configure --prefix=/usr/local/apr && make && make install#================编译安装apr-util软件包=================================[root@jie1 ~]# cd apr-util-1.5.2[root@jie1 apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install#=================编译安装httpd的软件包================================[root@jie1 ~]# cd httpd-2.4.6[root@jie1 httpd-2.4.6]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event && make && make install#============复制源码包里面提供的httpd的SystemV脚本=====================[root@jie1 ~]# cp httpd-2.4.6/build/rpm/httpd.init /etc/rc.d/init.d/httpd [root@jie1 ~]# vim /etc/rc.d/init.d/httpd#==============vim /etc/rc.d/init.d/httpd =============================prog=httpdhttpd=${HTTPD-/usr/local/apache/bin/httpd}pidfile=${PIDFILE-/usr/local/apache/logs/${prog}.pid}lockfile=${LOCKFILE-/var/lock/subsys/${prog}}RETVAL=0# check for 1.3 configurationcheck13 () { CONFFILE=/usr/local/apache/conf/httpd.conf#======================================================================[root@jie1 ~]# service httpd startStarting httpd: [ OK ][root@jie1 ~]# chkconfig --add httpd[root@jie1 ~]# chkconfig httpd on#==============编译安装使用mod_jk模块连接tomcat的连接器=================[root@jie1 ~]# tar xf tomcat-connectors-1.2.37-src.tar.gz[root@jie1 ~]# cd tomcat-connectors-1.2.37-src/native/ [root@jie1 native]# ./configure --with-apxs=/usr/local/apache/bin/apxs && make && make install
apache2:与apache1要安装相关的软件、安装方法都是一样的,这里不再赘述
tomcat1上相关软件的安装:
[root@jie3 ~]# lsanaconda-ks.cfg jdk-7u9-linux-x64.rpmapache-tomcat-7.0.42.tar.gz JspRun!_6.0.0_GBK.zipinstall.log mysql-connector-java-5.1.16.tar.gzinstall.log.syslog #===================安装jdk,修改环境变量=========================[root@jie3 ~]# rpm -ivh jdk-7u9-linux-x64.rpm[root@jie3 ~]# vim/etc/profile.d/java.shexport JAVA_HOME=/usr/java/latestexport PATH=$JAVA_HOME/bin:$PATH[root@jie3 ~]# source /etc/profile.d/java.sh#===================安装tomcat,修改环境变量=====================[root@jie3 ~]# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/[root@jie3 ~]# cd /usr/local/[root@jie3 local]#ln -sv apache-tomcat-7.0.42 tomcat[root@jie3 local]# vim /etc/profile.d/tomcat.shexport CATALINA_HOME=/usr/local/tomcatexport PATH=$CATALINA_HOME/bin:$PATH[root@jie3 local]# source /etc/profile.d/tomcat.sh#==================安装连接mysql的驱动===========================[root@jie3 local]# cd /root/[root@jie3 ~]# tar xf mysql-connector-java-5.1.16.tar.gz[root@jie3 ~]# cd mysql-connector-java-5.1.16[root@jie3 mysql-connector-java-5.1.16]# cp mysql-connector-java-5.1.16-bin.jar /usr/local/tomcat/lib/
tomcat2:与tomcat1安装相关的软件、安装方法都是一样的,这里也不再赘述
mysql1与mysql2就是用corosync+pacemaker+mysql+drbd实现mysql的高可用,这里不做详细的安装
二、keepalived实现apache的高可用
apache1的配置:
#=====================修改keepalived的配置实现httpd的高可用===========[root@jie1 ~]# cd /etc/keepalived/[root@jie1 keepalived]# vim keepalived.conf! Configuration File for keepalivedglobal_defs { notification_email { sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_script chk_httpd { script "killall -0 httpd" interval 0 weghit -2}vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 22 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.22.100 } track_script { chk_httpd}}#====================copy配置文件到apache2上面========================[root@jie1 keepalived]#scp keepalived.conf 172.16.22.2:/etc/keepalived[root@jie1 keepalived]# service keepalived startStarting keepalived: [ OK ][root@jie1 keepalived]# chkconfig --add keepalived[root@jie1 keepalived]# chkconfig keepalived on
apache2的相关配置:
#=====================修改来着apache1的keepalived的配置文件===========[root@jie2 ~]# cd /etc/keepalived/[root@jie2 keepalived]# vim keepalived.conf state BACKUP priority 99[root@jie2 keepalived]# service keepalived startStarting keepalived: [ OK ][root@jie2 keepalived]# chkconfig --add keepalived[root@jie2 keepalived]# chkconfig keepalived on
测试apache的高可用:
#=============查看vip在apache1节点上=================================[root@jie1 conf]# ip addr show | grep eth02: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 172.16.22.1/16 brd 172.16.255.255 scope global eth0 inet 172.16.22.100/32 scope global eth0 #===vip为172.16.22.100==#===========杀死httpd的服务进程,发现vip已经漂移=================[root@jie1 conf]# killall httpd[root@jie1 conf]# ip addr show | grep eth02: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 172.16.22.1/16 brd 172.16.255.255 scope global eth0#=============查看vip已经漂移到apache2节点上=======================[root@jie2 ~]# ip addr show | grep eth02: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 172.16.22.1/16 brd 172.16.255.255 scope global eth0 inet 172.16.22.100/32 scope global eth0#=======在apache1上面重新开启http服务,vip已经重新漂移到apache1上=====[root@jie1 conf]# service httpd startStarting httpd: [ OK ][root@jie1 conf]# ip addr show | grep eth02: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 inet 172.16.22.1/16 brd 172.16.255.255 scope global eth0 inet 172.16.22.100/32 scope global eth0[root@jie1 conf]#
成功测试,当apache服务的进程死掉后,vip可以漂移
三、tomcat服务器测试与mysql的连接
tomcat1的配置:
[root@jie3 ~]# cd /usr/local/tomcat/conf/[root@jie3 conf]# vim server.xml#=================tomcat,server.xml的部分相关配置===================#监听的端口改为了80 #defaultHost把默认的localhost改为了新建的主机名 ================================部分配置文件==========================#=========利用catalina重启tomcat===========================[root@jie3 conf]# catalina.sh stopUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar[root@jie3 conf]# catalina.sh startUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar#==============创建网页根目录和测试连接数据库的网页文件===============#===============博主只是测试了tomcat1上的,各位朋友也可以测试tomcat2上[root@jie3 conf]#mkdir -pv /tomcat/test[root@jie3 conf]# cd /tomcat/test/[root@jie3 test]# vim test.jsp<%@ page language="java" %><%@ page import="com.mysql.jdbc.Driver" %><%@ page import="java.sql.*" %><%String driverName="com.mysql.jdbc.Driver";String userName="jie";String userPasswd="jie123";String dbName="test";String url="jdbc:mysql://172.16.22.200/"+dbName+"?user="+userName+"&password="+userPasswd;Class.forName("com.mysql.jdbc.Driver").newInstance();try{ Connection connection=DriverManager.getConnection(url); out.println("TomcatA, tomcat connect mysql is ok!"); connection.close();}catch( Exception e ){ out.println( "connent mysql error:" + e );}%>======================================================================#==============在mysql上授权能访问数据库的网段==============[root@jie4 ~]# mysqlmysql> grant all on *.* to 'jie'@'172.16.%.%' identified by 'jie123';mysql> flush privileges;#新建了一个虚拟主机,站点和网页文件存放在/tomcat/test目录下
测试新建的test.jsp测试文件是否可以mysql的连接
测试tomcat连接数据库is ok!
在tomcat安装Jsprun论坛
#=================解压论坛,把论坛的文件存放到网页存放路径中=============[root@jie3 ~]# unzip JspRun\!_6.0.0_GBK.zip[root@jie3 ~]#mkdir -pv /tomcat/bbs[root@jie3 ~]#cp -rp upload/* /tomcat/bbs/[root@jie3 ~]# cd /usr/local/tomcat/conf/[root@jie3 conf]# vim server.xml#=================tomcat,server.xml的部分相关配置===================#之前已经把监听的端口改为了80 #defaultHost改为新建的论坛的虚拟主机名 #===默认的虚拟主机还有www.test.com的虚拟主机的配置文件没给予显示================================部分配置文件==========================#=========利用catalina重启tomcat===========================[root@jie3 conf]# catalina.sh stopUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar[root@jie3 conf]# catalina.sh startUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar=======================================================================#==================在数据库上面创建,Jsprun论坛的数据库=============[root@jie4 ~]# mysqlmysql> create database jsprun;Query OK, 1 row affected (0.03 sec)mysql> grant all on jsprun.* to 'jspuser'@'172.16.%.%' identified by 'jspmypass';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)#再新建了一个虚拟主机,站点和网页文件存放在/tomcat/bbs目录下
用浏览器安装Jsprun论坛
论坛已经安装成功
四、apache三种方式实现tomcat的反向代理
1)mod_proxy
apache1&apache2上的配置:两边的配置都是一样
[root@jie1 ~]# vim /usr/local/apache/conf/httpd.conf#==========开启相应的模块,然后添加以下参数============================LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soProxyVia OnProxyRequests OffProxyPreserveHost OnOrder deny,allow Allow from all ProxyPass / http://172.16.22.3:80/ProxyPa***everse / http://172.16.22.3:80/======================================================================[root@jie1 ~]# httpd -tSyntax OK[root@jie1 ~]# service httpd reloadReloading httpd: [ OK ][root@jie1 ~]#scp /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/ #把配置文件copy到apache2上
tomcat1&tomcat2上的配置:两边的配置都是一样
两边都需要创建/tomcat/test目录和index.jsp文件
两边的配置文件都必须一样
[root@jie3 ~]# cd /usr/local/tomcat/conf/[root@jie3 conf]# vim server.xml#=================tomcat,server.xml的部分相关配置===================#监听的端口改为了80 #defaultHost把默认的localhost改为了新建的主机名 ================================部分配置文件==========================#=========利用catalina重启tomcat===========================[root@jie3 conf]# catalina.sh stopUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar[root@jie3 conf]# catalina.sh startUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar#==============创建网页根目录和测试连接数据库的网页文件===============[root@jie3 conf]#mkdir -pv /tomcat/test[root@jie3 conf]# cd /tomcat/test/[root@jie3 test]# vim index.jsp#=============测试页面==============================================<%@ page language="java" %><%@ page import="java.util.*" %>#新建了一个虚拟主机,站点和网页文件存放在/tomcat/test目录下 JSP test page. <% out.println("Hello,tomcat1,jie3!"); %>
博主只测试了tomcat1的反向代理
2)mod_proxy_ajp
apache1&apache2的配置:
只需小小修改apache的配置文件中
[root@jie1 ~]# vim /usr/local/apache/conf/httpd.conf#==========开启相应的模块,然后添加以下参数============================LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soProxyVia OnProxyRequests OffProxyPreserveHost OnOrder deny,allow Allow from all ProxyPass / ajp://172.16.22.4:8009/ProxyPa***everse / ajp://172.16.22.4:8009/======================================================================[root@jie1 ~]# httpd -tSyntax OK[root@jie1 ~]# service httpd reloadReloading httpd: [ OK ][root@jie1 ~]#scp /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/ #把配置文件copy到apache2上
tomcat1&tomcat2不需要修改
博主只测试了tomcat2的反向代理
3)mod_jk
之前已经说过,apache要使用mod_jk模块做tomcat的反向代理则必须安装连接tomcat的连接器,连接器已经安装
apache1&apache2的配置:
[root@jie1 ~]# cd /usr/local/apache/conf/[root@jie1 conf]# vim /usr/local/apache/conf/httpd.conf#=========注释掉之前开启的模块,然后添加把之前用mod_proxy的模块添加的内容也注释掉,只添加一句=====================#LoadModule proxy_module modules/mod_proxy.so#LoadModule proxy_connect_module modules/mod_proxy_connect.so#LoadModule proxy_http_module modules/mod_proxy_http.so#LoadModule proxy_ajp_module modules/mod_proxy_ajp.soInclude /usr/local/apache/conf/extra/httpd-jk.conf[root@jie1 extra]# cat /usr/local/apache/conf/extra/httpd-jk.confLoadModule jk_module modules/mod_jk.soJkWorkersFile /usr/local/apache/conf/extra/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel debugJkMount /* TomcatAJkMount /status/ stat1[root@jie1 extra]# cat /usr/local/apache/conf/extra/workers.propertiesworker.list=TomcatA,stat1worker.TomcatA.port=8009worker.TomcatA.host=172.16.22.3worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.stat1.type = status[root@jie1 extra]#
[root@jie1 ~]# cd /usr/local/apache/conf/[root@jie1 conf]# vim /usr/local/apache/conf/httpd.confLoadModule proxy_module modules/mod_proxy.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.so#=========Include /usr/local/apache/conf/extra/httpd-jk.conf[root@jie1 extra]# cat /usr/local/apache/conf/extra/httpd-jk.confLoadModule jk_module modules/mod_jk.soJkWorkersFile /usr/local/apache/conf/extra/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel debugJkMount /* TomcatAJkMount /status/ stat1[root@jie1 extra]# cat /usr/local/apache/conf/extra/workers.propertiesworker.list=TomcatA,stat1worker.TomcatA.port=8009worker.TomcatA.host=172.16.22.3worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.stat1.type = status[root@jie1 extra]#
tomcat1&tomcat2不需要修改
测试mod_jk模式为tomcat1反向代理
自此apache三种方式实现反向代理已经ok了
五、apache三种方式实现tomcat的负载均衡
三种反向代理与三种负载均衡都是以这三种模块mod_proxy、mod_proxy_ajp、mod_jk,只不过每种方式都需要用到负载均衡的模块
1)mod_proxy
apache1&apache2上的配置:两边的配置都是一样
[root@jie1 ~]# cd /usr/local/apache/conf/[root@jie1 conf]# vim /usr/local/apache/conf/httpd.conf#=======================开启这些模块,和添加以下内容===================LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule proxy_express_module modules/mod_proxy_express.soLoadModule slotmem_shm_module modules/mod_slotmem_shm.soProxyRequests OffProxyVia OnProxyPreserveHost OnProxyRequests OffBalancerMember ajp://172.16.22.3:8009/ loadfactor=1 route=TomcatABalancerMember ajp://172.16.22.4:8009/ loadfactor=1 route=TomcatAProxySet lbmethod=bytraffic ProxyPass / balancer://jie/ stickysession=jsessionidProxyPa***everse / balancer://jie/=======================================================================[root@jie1 ~]# httpd -tSyntax OK[root@jie1 ~]# service httpd reloadReloading httpd: [ OK ][root@jie1 ~]#scp /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/ #把配置文件copy到apache2上
tomcat1&tomcat2上的配置:两边的配置基本都是一样
#==================tomcat1的配置文件修改=================================[root@jie3 conf]# vim /usr/local/tomcat/conf/server.xml#==================tomcat2的配置文件修改================================[root@jie4 conf]# vim /usr/local/tomcat/conf/server.xml
测试mod_proxy的负载均衡
1)mod_proxy_ajp
apache1&apache2上的配置:两边的配置都是一样
[root@jie1 ~]# cd /usr/local/apache/conf/[root@jie1 conf]# vim /usr/local/apache/conf/httpd.conf#=======================开启这些模块,和添加以下内容===================LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_connect_module modules/mod_proxy_connect.soLoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule proxy_express_module modules/mod_proxy_express.soLoadModule slotmem_shm_module modules/mod_slotmem_shm.soProxyRequests OffProxyVia OnProxyPreserveHost OnBalancerMember http://172.16.22.3:80/ loadfactor=1 route=TomcatABalancerMember http://172.16.22.4:80/ loadfactor=1 route=TomcatBProxySet lbmethod=bytraffic ProxyPass / balancer://jie/ProxyPa***everse / balancer://jie/Require all granted Require all granted =======================================================================[root@jie1 ~]# httpd -tSyntax OK[root@jie1 ~]# service httpd reloadReloading httpd: [ OK ][root@jie1 ~]#scp /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/ #把配置文件copy到apache2上
tomcat1&tomcat2上不用修改
测试mod_proxy_ajp模块的负载均衡
3)mod_jk
apache1&apache2上的配置:两边的配置都是一样
[root@jie1 ~]# cd /usr/local/apache/conf/[root@jie1 conf]# vim /usr/local/apache/conf/httpd.conf#=========注释掉一些之前开启的模块,然后添加把之前用mod_proxy的模块添加的内容也注释掉,只添加一句=====================#LoadModule proxy_module modules/mod_proxy.so#LoadModule proxy_connect_module modules/mod_proxy_connect.so#LoadModule proxy_http_module modules/mod_proxy_http.so#LoadModule proxy_ajp_module modules/mod_proxy_ajp.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule proxy_express_module modules/mod_proxy_express.soLoadModule slotmem_shm_module modules/mod_slotmem_shm.soLoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.soInclude /usr/local/apache/conf/extra/httpd-jk.conf[root@jie1 extra]# vim /usr/local/apache/conf/extra/httpd-jk.conf========================================LoadModule jk_module modules/mod_jk.soJkWorkersFile /usr/local/apache/conf/extra/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel debugJkMount /* jieJkMount /jkstatus/ stat1========================================[root@jie1 extra]# vim /usr/local/apache/conf/extra/workers.properties========================================worker.list = jie,stat1worker.TomcatA.type = ajp13worker.TomcatA.host = 172.16.22.3worker.TomcatA.port = 8009worker.TomcatA.lbfactor = 5worker.TomcatB.type = ajp13worker.TomcatB.host = 172.16.22.4worker.TomcatB.port = 8009worker.TomcatB.lbfactor = 5worker.jie.type = lbworker.jie.method = Rworker.jie.sticky_session = 0worker.jie.balance_workers = TomcatA, TomcatBworker.stat1.type = status========================================[root@jie1 ~]# httpd -tSyntax OK[root@jie1 ~]# service httpd reloadReloading httpd: [ OK ]#==================可以把修改的这些文件复制到apache2上=================
tomcat1&tomcat2上的配置文件不必修改,在新建一个测试页
[root@jie3 conf]# cd /tomcat/test/[root@jie3 test]# vim session.jsp#=======================session的测试页面==============================<%@ page language="java" %>TomcatA TomcatA
Session ID | <% session.setAttribute("abc","abc"); %><%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
测试mod_jk模块的负载均衡
补充:
在apache1&apache2修改配置文件的这个参数
[root@jie1 extra]# vim /usr/local/apache/conf/extra/workers.properties
worker.jie.sticky_session = 0 #实现将用户session与某worker绑定
此参数设置以后,来着同一个IP的访问一段时间内始终代理给后端的同一个tomcat上
测试结果:
此致,apache+tomcat的架构已经完成,请各位博友多多指点