apache+tomcat的架构
–>
===================================
keepalived实现apache的高可用
在tomcat上搭建JspRun论坛
apache三种方式实现tomcat的反向代理
apache三种方式实现tomcat的负载均衡
===================================
环境:
OS:Centos 6.x(redhat 6.x)
yum源:
[centos] name=sohu-centos baseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearch gpgcheck=1 enable=0 gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6 [epel] name=sohu-epel baseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/ enable=1 gpgcheck=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====================================== [[email protected] ~]# yum -y install keepalived #================源码编译安装httpd-2.4.6================================ [[email protected] ~]# tar xf apr-1.4.6.tar.bz2 [[email protected] ~]# tar xf apr-util-1.5.2.tar.bz2 [[email protected] ~]# tar xf httpd-2.4.6.tar.bz2 #===========安装开发库,和依赖性包====================================== [[email protected] ~]# yum -y groupinstall "Development tools" "Server Platform Development" [[email protected] ~]# yum -y install pcre-devel #=============编译安装apr软件=========================================== [[email protected] ~]# cd apr-1.4.6 [[email protected] apr-1.4.6]# ./configure --prefix=/usr/local/apr && make && make install #================编译安装apr-util软件包================================= [[email protected] ~]# cd apr-util-1.5.2 [[email protected] apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install #=================编译安装httpd的软件包================================ [[email protected] ~]# cd httpd-2.4.6 [[email protected] 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脚本===================== [[email protected] ~]# cp httpd-2.4.6/build/rpm/httpd.init /etc/rc.d/init.d/httpd [[email protected] ~]# vim /etc/rc.d/init.d/httpd #==============vim /etc/rc.d/init.d/httpd ============================= prog=httpd httpd=${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 configuration check13 () { CONFFILE=/usr/local/apache/conf/httpd.conf #====================================================================== [[email protected] ~]# service httpd start Starting httpd: [ OK ] [[email protected] ~]# chkconfig --add httpd [[email protected] ~]# chkconfig httpd on #==============编译安装使用mod_jk模块连接tomcat的连接器================= [[email protected] ~]# tar xf tomcat-connectors-1.2.37-src.tar.gz [[email protected] ~]# cd tomcat-connectors-1.2.37-src/native/ [[email protected] native]# ./configure --with-apxs=/usr/local/apache/bin/apxs && make && make install
apache2:与apache1要安装相关的软件、安装方法都是一样的,这里不再赘述
tomcat1上相关软件的安装:
[[email protected] ~]# ls anaconda-ks.cfg jdk-7u9-linux-x64.rpm apache-tomcat-7.0.42.tar.gz JspRun!_6.0.0_GBK.zip install.log mysql-connector-java-5.1.16.tar.gz install.log.syslog #===================安装jdk,修改环境变量========================= [[email protected] ~]# rpm -ivh jdk-7u9-linux-x64.rpm [[email protected] ~]# vim/etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH [[email protected] ~]# source /etc/profile.d/java.sh #===================安装tomcat,修改环境变量===================== [[email protected] ~]# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/ [[email protected] ~]# cd /usr/local/ [[email protected] local]#ln -sv apache-tomcat-7.0.42 tomcat [[email protected] local]# vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH [[email protected] local]# source /etc/profile.d/tomcat.sh #==================安装连接mysql的驱动=========================== [[email protected] local]# cd /root/ [[email protected] ~]# tar xf mysql-connector-java-5.1.16.tar.gz [[email protected] ~]# cd mysql-connector-java-5.1.16 [[email protected] 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的高可用,这里不做详细的安装 请点击这里(详解mysql的高可用)
二、keepalived实现apache的高可用
apache1的配置:
#=====================修改keepalived的配置实现httpd的高可用=========== [[email protected] ~]# cd /etc/keepalived/ [[email protected] keepalived]# vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] 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上面======================== [[email protected] keepalived]#scp keepalived.conf 172.16.22.2:/etc/keepalived [[email protected] keepalived]# service keepalived start Starting keepalived: [ OK ] [[email protected] keepalived]# chkconfig --add keepalived [[email protected] keepalived]# chkconfig keepalived on
apache2的相关配置:
#=====================修改来着apache1的keepalived的配置文件=========== [[email protected] ~]# cd /etc/keepalived/ [[email protected] keepalived]# vim keepalived.conf state BACKUP priority 99 [[email protected] keepalived]# service keepalived start Starting keepalived: [ OK ] [[email protected] keepalived]# chkconfig --add keepalived [[email protected] keepalived]# chkconfig keepalived on
测试apache的高可用:
#=============查看vip在apache1节点上================================= [[email protected] conf]# ip addr show | grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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已经漂移================= [[email protected] conf]# killall httpd [[email protected] conf]# ip addr show | grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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节点上======================= [[email protected] ~]# ip addr show | grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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上===== [[email protected] conf]# service httpd start Starting httpd: [ OK ] [[email protected] conf]# ip addr show | grep eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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 [[email protected] conf]#
成功测试,当apache服务的进程死掉后,vip可以漂移
三、tomcat服务器测试与mysql的连接
tomcat1的配置:
[[email protected] ~]# cd /usr/local/tomcat/conf/ [[email protected] conf]# vim server.xml #=================tomcat,server.xml的部分相关配置=================== <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> #监听的端口改为了80 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="www.test.com" > #defaultHost把默认的localhost改为了新建的主机名 <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="www.test.com" appBase="/tomcat/test" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/tomcat/test" /> #新建了一个虚拟主机,站点和网页文件存放在/tomcat/test目录下 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="test_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> ================================部分配置文件========================== #=========利用catalina重启tomcat=========================== [[email protected] conf]# catalina.sh stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [[email protected] conf]# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar #==============创建网页根目录和测试连接数据库的网页文件=============== #===============博主只是测试了tomcat1上的,各位朋友也可以测试tomcat2上 [[email protected] conf]#mkdir -pv /tomcat/test [[email protected] conf]# cd /tomcat/test/ [[email protected] 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上授权能访问数据库的网段============== [[email protected] ~]# mysql mysql> grant all on *.* to 'jie'@'172.16.%.%' identified by 'jie123'; mysql> flush privileges;
测试新建的test.jsp测试文件是否可以mysql的连接
测试tomcat连接数据库is ok!
在tomcat安装Jsprun论坛
#=================解压论坛,把论坛的文件存放到网页存放路径中============= [[email protected] ~]# unzip JspRun\!_6.0.0_GBK.zip [[email protected] ~]#mkdir -pv /tomcat/bbs [[email protected] ~]#cp -rp upload/* /tomcat/bbs/ [[email protected] ~]# cd /usr/local/tomcat/conf/ [[email protected] conf]# vim server.xml #=================tomcat,server.xml的部分相关配置=================== <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> #之前已经把监听的端口改为了80 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="www.bbs.com" > #defaultHost改为新建的论坛的虚拟主机名 <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="www.test.com" appBase="/tomcat/bbs" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/tomcat/bbs" /> #再新建了一个虚拟主机,站点和网页文件存放在/tomcat/bbs目录下 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="test_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> #===默认的虚拟主机还有www.test.com的虚拟主机的配置文件没给予显示 ================================部分配置文件========================== #=========利用catalina重启tomcat=========================== [[email protected] conf]# catalina.sh stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [[email protected] conf]# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar ======================================================================= #==================在数据库上面创建,Jsprun论坛的数据库============= [[email protected] ~]# mysql mysql> 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)
用浏览器安装Jsprun论坛
论坛已经安装成功
四、apache三种方式实现tomcat的反向代理
1)mod_proxy
apache1&apache2上的配置:两边的配置都是一样
[[email protected] ~]# vim /usr/local/apache/conf/httpd.conf #==========开启相应的模块,然后添加以下参数============================ 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.so ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://172.16.22.3:80/ ProxyPa***everse / http://172.16.22.3:80/ ====================================================================== [[email protected] ~]# httpd -t Syntax OK [[email protected] ~]# service httpd reload Reloading httpd: [ OK ] [[email protected] ~]#scp /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/ #把配置文件copy到apache2上
tomcat1&tomcat2上的配置:两边的配置都是一样
两边都需要创建/tomcat/test目录和index.jsp文件
两边的配置文件都必须一样
[[email protected] ~]# cd /usr/local/tomcat/conf/ [[email protected] conf]# vim server.xml #=================tomcat,server.xml的部分相关配置=================== <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> #监听的端口改为了80 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="www.test.com" > #defaultHost把默认的localhost改为了新建的主机名 <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="www.test.com" appBase="/tomcat/test" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/tomcat/test" /> #新建了一个虚拟主机,站点和网页文件存放在/tomcat/test目录下 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="test_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> ================================部分配置文件========================== #=========利用catalina重启tomcat=========================== [[email protected] conf]# catalina.sh stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [[email protected] conf]# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar #==============创建网页根目录和测试连接数据库的网页文件=============== [[email protected] conf]#mkdir -pv /tomcat/test [[email protected] conf]# cd /tomcat/test/ [[email protected] test]# vim index.jsp #=============测试页面============================================== <%@ page language="java" %> <%@ page import="java.util.*" %> <html> <head> <title>JSP test page.</title> </head> <body> <% out.println("Hello,tomcat1,jie3!"); %> </body> </html>
博主只测试了tomcat1的反向代理
2)mod_proxy_ajp
apache1&apache2的配置:
只需小小修改apache的配置文件中
[[email protected] ~]# vim /usr/local/apache/conf/httpd.conf #==========开启相应的模块,然后添加以下参数============================ 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.so ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / ajp://172.16.22.4:8009/ ProxyPa***everse / ajp://172.16.22.4:8009/ ====================================================================== [[email protected] ~]# httpd -t Syntax OK [[email protected] ~]# service httpd reload Reloading httpd: [ OK ] [[email protected] ~]#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的配置:
[[email protected] ~]# cd /usr/local/apache/conf/ [[email protected] 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.so Include /usr/local/apache/conf/extra/httpd-jk.conf [[email protected] extra]# cat /usr/local/apache/conf/extra/httpd-jk.conf LoadModule jk_module modules/mod_jk.so JkWorkersFile /usr/local/apache/conf/extra/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* TomcatA JkMount /status/ stat1 [[email protected] extra]# cat /usr/local/apache/conf/extra/workers.properties worker.list=TomcatA,stat1 worker.TomcatA.port=8009 worker.TomcatA.host=172.16.22.3 worker.TomcatA.type=ajp13 worker.TomcatA.lbfactor=1 worker.stat1.type = status [[email protected] extra]#
[[email protected] ~]# cd /usr/local/apache/conf/ [[email protected] conf]# vim /usr/local/apache/conf/httpd.conf 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.so #========= Include /usr/local/apache/conf/extra/httpd-jk.conf [[email protected] extra]# cat /usr/local/apache/conf/extra/httpd-jk.conf LoadModule jk_module modules/mod_jk.so JkWorkersFile /usr/local/apache/conf/extra/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* TomcatA JkMount /status/ stat1 [[email protected] extra]# cat /usr/local/apache/conf/extra/workers.properties worker.list=TomcatA,stat1 worker.TomcatA.port=8009 worker.TomcatA.host=172.16.22.3 worker.TomcatA.type=ajp13 worker.TomcatA.lbfactor=1 worker.stat1.type = status [[email protected] extra]#
tomcat1&tomcat2不需要修改
测试mod_jk模式为tomcat1反向代理
自此apache三种方式实现反向代理已经ok了
五、apache三种方式实现tomcat的负载均衡
三种反向代理与三种负载均衡都是以这三种模块mod_proxy、mod_proxy_ajp、mod_jk,只不过每种方式都需要用到负载均衡的模块
1)mod_proxy
apache1&apache2上的配置:两边的配置都是一样
[[email protected] ~]# cd /usr/local/apache/conf/ [[email protected] conf]# vim /usr/local/apache/conf/httpd.conf #=======================开启这些模块,和添加以下内容=================== 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.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_express_module modules/mod_proxy_express.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so ProxyRequests Off ProxyVia On ProxyPreserveHost On ProxyRequests Off <proxy balancer://jie> BalancerMember ajp://172.16.22.3:8009/ loadfactor=1 route=TomcatA BalancerMember ajp://172.16.22.4:8009/ loadfactor=1 route=TomcatA ProxySet lbmethod=bytraffic </proxy> ProxyPass / balancer://jie/ stickysession=jsessionid ProxyPa***everse / balancer://jie/ ======================================================================= [[email protected] ~]# httpd -t Syntax OK [[email protected] ~]# service httpd reload Reloading httpd: [ OK ] [[email protected] ~]#scp /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/ #把配置文件copy到apache2上
tomcat1&tomcat2上的配置:两边的配置基本都是一样
#==================tomcat1的配置文件修改================================= [[email protected] conf]# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.test.com" jvmRoute="TomcatA"> #==================tomcat2的配置文件修改================================ [[email protected] conf]# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.test.com" jvmRoute="TomcatB">
测试mod_proxy的负载均衡
1)mod_proxy_ajp
apache1&apache2上的配置:两边的配置都是一样
[[email protected] ~]# cd /usr/local/apache/conf/ [[email protected] conf]# vim /usr/local/apache/conf/httpd.conf #=======================开启这些模块,和添加以下内容=================== 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.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_express_module modules/mod_proxy_express.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so ProxyRequests Off ProxyVia On ProxyPreserveHost On <Proxy balancer://jie> BalancerMember http://172.16.22.3:80/ loadfactor=1 route=TomcatA BalancerMember http://172.16.22.4:80/ loadfactor=1 route=TomcatB ProxySet lbmethod=bytraffic </Proxy> ProxyPass / balancer://jie/ ProxyPa***everse / balancer://jie/ <Proxy *> Require all granted </Proxy> <Location / > Require all granted </Location> ======================================================================= [[email protected] ~]# httpd -t Syntax OK [[email protected] ~]# service httpd reload Reloading httpd: [ OK ] [[email protected] ~]#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上的配置:两边的配置都是一样
[[email protected] ~]# cd /usr/local/apache/conf/ [[email protected] 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.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_express_module modules/mod_proxy_express.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so Include /usr/local/apache/conf/extra/httpd-jk.conf [[email protected] extra]# vim /usr/local/apache/conf/extra/httpd-jk.conf ======================================== LoadModule jk_module modules/mod_jk.so JkWorkersFile /usr/local/apache/conf/extra/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* jie JkMount /jkstatus/ stat1 ======================================== [[email protected] extra]# vim /usr/local/apache/conf/extra/workers.properties ======================================== worker.list = jie,stat1 worker.TomcatA.type = ajp13 worker.TomcatA.host = 172.16.22.3 worker.TomcatA.port = 8009 worker.TomcatA.lbfactor = 5 worker.TomcatB.type = ajp13 worker.TomcatB.host = 172.16.22.4 worker.TomcatB.port = 8009 worker.TomcatB.lbfactor = 5 worker.jie.type = lb worker.jie.method = R worker.jie.sticky_session = 0 worker.jie.balance_workers = TomcatA, TomcatB worker.stat1.type = status ======================================== [[email protected] ~]# httpd -t Syntax OK [[email protected] ~]# service httpd reload Reloading httpd: [ OK ] #==================可以把修改的这些文件复制到apache2上=================
tomcat1&tomcat2上的配置文件不必修改,在新建一个测试页
[[email protected] conf]# cd /tomcat/test/ [[email protected] test]# vim session.jsp #=======================session的测试页面============================== <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="green">TomcatA </font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("abc","abc"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> ========================================================================
测试mod_jk模块的负载均衡
补充:
在apache1&apache2修改配置文件的这个参数
[[email protected] extra]# vim /usr/local/apache/conf/extra/workers.properties
worker.jie.sticky_session = 0 #实现将用户session与某worker绑定
此参数设置以后,来着同一个IP的访问一段时间内始终代理给后端的同一个tomcat上
测试结果:
此致,apache+tomcat的架构已经完成,请各位博友多多指点
转载于:https://blog.51cto.com/litaotao/1303981
本文来源 互联网收集,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源,如您发现有涉嫌抄袭侵权的内容,请联系本站核实处理。