apache+tomcat的架构,apachetomcat是什么


apache+tomcat的架构

===================================

keepalived实现apache的高可用

在tomcat上搭建JspRun论坛

apache三种方式实现tomcat的反向代理

apache三种方式实现tomcat的负载均衡

===================================

环境:

OS:Centos 6.x(redhat 6.x)

yum源:

1 2 3 4 5 6 7 8 9 10 11 [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


拓扑图:

205910806.png


拓扑图的规划:


IP 地址

所需安装软件

apache1

Eth0:172.16.22.1

VIP1172.16.22.100

Keepalived+apache+tomcat-connectors

apache2

Eth0:172.16.22.2

VIP1172.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

VIP2172.16.22.200

Corosync+pacemaker+Mysql+drbd

mysql2

Eth0:172.16.22.6

VIP2172.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上相关软件的安装:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #================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=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 #====================================================================== [root@jie1 ~]# service httpd start Starting 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上相关软件的安装:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [root@jie3 ~]# 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,修改环境变量========================= [root@jie3 ~]# rpm -ivh jdk-7u9-linux-x64.rpm [root@jie3 ~]# vim/etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export 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.sh export CATALINA_HOME=/usr/local/tomcat export 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的高可用,这里不做详细的安装 请点击这里(详解mysql的高可用)


二、keepalived实现apache的高可用

apache1的配置:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #=====================修改keepalived的配置实现httpd的高可用=========== [root@jie1 ~]# cd /etc/keepalived/ [root@jie1 keepalived]# vim keepalived.conf ! Configuration File for keepalived global_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 start Starting keepalived:                                       [  OK  ] [root@jie1 keepalived]# chkconfig --add keepalived [root@jie1 keepalived]# chkconfig keepalived on


apache2的相关配置:

1 2 3 4 5 6 7 8 9 #=====================修改来着apache1的keepalived的配置文件=========== [root@jie2 ~]# cd /etc/keepalived/ [root@jie2 keepalived]# vim keepalived.conf     state BACKUP     priority 99 [root@jie2 keepalived]# service keepalived start Starting keepalived:                                       [  OK  ] [root@jie2 keepalived]# chkconfig --add keepalived [root@jie2 keepalived]# chkconfig keepalived on


测试apache的高可用:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #=============查看vip在apache1节点上================================= [root@jie1 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已经漂移================= [root@jie1 conf]# killall httpd [root@jie1 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节点上======================= [root@jie2 ~]# 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上===== [root@jie1 conf]# service httpd start Starting httpd:                                            [  OK  ] [root@jie1 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 [root@jie1 conf]#

成功测试,当apache服务的进程死掉后,vip可以漂移


三、tomcat服务器测试与mysql的连接

tomcat1的配置:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 [root@jie3 ~]# cd /usr/local/tomcat/conf/ [root@jie3 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 &quot;%r&quot; %s %b" />      </Host>    </Engine> ================================部分配置文件========================== #=========利用catalina重启tomcat=========================== [root@jie3 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 [root@jie3 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上 [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 ~]# mysql mysql> grant all on *.* to 'jie'@'172.16.%.%' identified by 'jie123'; mysql> flush privileges;

测试新建的test.jsp测试文件是否可以mysql的连接

230005209.png

测试tomcat连接数据库is ok!


在tomcat安装Jsprun论坛

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #=================解压论坛,把论坛的文件存放到网页存放路径中============= [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的部分相关配置=================== <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 &quot;%r&quot; %s %b" />      </Host>    </Engine> #===默认的虚拟主机还有www.test.com的虚拟主机的配置文件没给予显示 ================================部分配置文件========================== #=========利用catalina重启tomcat=========================== [root@jie3 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 [root@jie3 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论坛的数据库============= [root@jie4 ~]# 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论坛

231342220.png

232051632.png

232153662.png


232222909.png

232326140.png论坛已经安装成功


四、apache三种方式实现tomcat的反向代理

1)mod_proxy

apache1&apache2上的配置:两边的配置都是一样

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@jie1 ~]# 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/ ProxyPassReverse / http://172.16.22.3:80/ ====================================================================== [root@jie1 ~]# httpd -t Syntax OK [root@jie1 ~]# service httpd reload Reloading 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文件

两边的配置文件都必须一样

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 [root@jie3 ~]# cd /usr/local/tomcat/conf/ [root@jie3 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 &quot;%r&quot; %s %b" />      </Host>    </Engine> ================================部分配置文件========================== #=========利用catalina重启tomcat=========================== [root@jie3 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 [root@jie3 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 #==============创建网页根目录和测试连接数据库的网页文件=============== [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.*" %> <html>   <head>     <title>JSP test page.</title>   </head>   <body>     <% out.println("Hello,tomcat1,jie3!"); %>   </body> </html>

博主只测试了tomcat1的反向代理

235626111.png


2)mod_proxy_ajp

apache1&apache2的配置:

只需小小修改apache的配置文件中

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [root@jie1 ~]# 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/ ProxyPassReverse / ajp://172.16.22.4:8009/ ====================================================================== [root@jie1 ~]# httpd -t Syntax OK [root@jie1 ~]# service httpd reload Reloading httpd:                                           [  OK  ] [root@jie1 ~]#scp  /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/    #把配置文件copy到apache2上

tomcat

博主只测试了tomcat2的反向代理

000150238.png

3)mod_jk

之前已经说过,apache要使用mod_jk模块做tomcat的反向代理则必须安装连接tomcat的连接器,连接器已经安装

apache1&apache2的配置:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [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.so Include /usr/local/apache/conf/extra/httpd-jk.conf [root@jie1 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 [root@jie1 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 [root@jie1 extra]#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@jie1 ~]# cd /usr/local/apache/conf/ [root@jie1 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 [root@jie1 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 [root@jie1 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 [root@jie1 extra]#

tomcat

测试mod_jk模式为tomcat1反向代理

094736522.png

自此apache三种方式实现反向代理已经ok了


五、apache三种方式实现tomcat的负载均衡

三种反向代理与三种负载均衡都是以这三种模块mod_proxy、mod_proxy_ajp、mod_jk,只不过每种方式都需要用到负载均衡的模块

1)mod_proxy

apache1&apache2上的配置:两边的配置都是一样


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@jie1 ~]# cd /usr/local/apache/conf/ [root@jie1 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 ProxyPassReverse / balancer://jie/ ======================================================================= [root@jie1 ~]# httpd -t Syntax OK [root@jie1 ~]# service httpd reload Reloading httpd:                                           [  OK  ] [root@jie1 ~]#scp  /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/    #把配置文件copy到apache2上

tomcat

1 2 3 4 5 6 #==================tomcat1的配置文件修改================================= [root@jie3 conf]# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.test.com" jvmRoute="TomcatA"> #==================tomcat2的配置文件修改================================ [root@jie4 conf]# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.test.com" jvmRoute="TomcatB">

测试mod_proxy的负载均衡

100050706.png

100114870.png


1)mod_proxy_ajp

apache1&apache2上的配置:两边的配置都是一样


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [root@jie1 ~]# cd /usr/local/apache/conf/ [root@jie1 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/ ProxyPassReverse / balancer://jie/ <Proxy *> Require all granted </Proxy> <Location  / > Require all granted </Location> ======================================================================= [root@jie1 ~]# httpd -t Syntax OK [root@jie1 ~]# service httpd reload Reloading httpd:                                           [  OK  ] [root@jie1 ~]#scp  /usr/local/apache/conf/httpd.conf 172.16.22.2:/ /usr/local/apache/conf/    #把配置文件copy到apache2上

tomcat

测试mod_proxy_ajp模块的负载均衡

101308487.png

101329775.png


3)mod_jk

apache1&apache2上的配置:两边的配置都是一样

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 [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.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 [root@jie1 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 ======================================== [root@jie1 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 ======================================== [root@jie1 ~]# httpd -t Syntax OK [root@jie1 ~]# service httpd reload Reloading httpd:                                           [  OK  ] #==================可以把修改的这些文件复制到apache2上=================

tomcat

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@jie3 conf]# cd /tomcat/test/ [root@jie3 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模块的负载均衡

103323243.png

103339584.png


补充:

在apache1&apache2修改配置文件的这个参数


[root@jie1 extra]# vim /usr/local/apache/conf/extra/workers.properties

worker.jie.sticky_session = 0 #实现将用户session与某worker绑定


此参数设置以后,来着同一个IP的访问一段时间内始终代理给后端的同一个tomcat上

测试结果:

104342171.png










本文转自 jie783213507 51CTO博客,原文链接:http://blog.51cto.com/litaotao/1303981,如需转载请自行联系原作者

相关内容