tomcat集群 php
httpd负载均衡 html
httpd -M node
确保有proxy_balancer_modeule模块 nginx
编辑httpd.conf web
注释DocumentRoot apache
编辑conf.d/mod_httpd.conf vim
定义虚拟主机 后端
两种方式都能实现负载均衡: tomcat
下面咱们就来分别演示一下, 服务器
1.基于mod_proxy实现负载均衡
定义虚拟主机
编辑conf.d/mod_httpd.conf
也就是在/etc/httpd/conf.d此目录下 建立mod_httpd.conf
定义虚拟主机
好了,你们能够看到基于mod_proxy模块的tomcat负载均衡配置完成。下面咱们来配置一下基于mod_jk的模块的负载均衡!
#关闭httpd
2.#mod_jk负载均衡
#安装httpd-devel
[root@node17 ~]# yum intsall httpd-devel
#安装tomcat-connectors
[root@node17 native]# pwd
/root/tomcat-connectors-1.2.40-src/native
[root@node17 native]# ./configure --with-apxs=`which apxs`
[root@node17 native]make && make install
/etc/httpd/conf.d此目录下
编辑workers.properties
[root@node17 conf.d]# vim workers.properties
/etc/httpd/conf.d此目录下
编辑mod_jk.conf
绑定session
编辑worker.propries,修改以下项
此外还得把模块mod_proxy 的配置文件mod_httpd.conf 改下
# mv mod_httpd.conf mod_httpd.conf.bak
分别编辑node1五、node16节点server.xml,分别修改以下项,第一行是节点1,第二行是节点2
上面建立的两个文件配置设置好之后,接下来是基于 mod_jk模块开启后端2个节点的tomcat
访问节点3 ,会话已经绑定
好了,到这里咱们状态信息查看就到这里了,下面咱们来配置会话共享集群。
.DeltaManager具体实现过程
(1).修改server.xml配置文件
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.18.201" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="/"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
以上内容定义在Engine容器中,则表示对全部主机均启动用集群功能。若是定义在某Host中,则表示仅对此主机启用集群功能。(注,节点node15与node16的tomcat的配置文件都要修改!)
以下是具体的配置:
节点node15:
[root@node15 ~]# vim /usr/local/tomcat/conf/server.xml
#在Engine组件内增长下面这一段
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.110.100.10" port="45564" frequency="500" dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.16.21.15" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="/"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
节点node16:
[root@node16 ~]# vim /usr/local/tomcat/conf/server.xml
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.110.100.10" port="45564" frequency="500" dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.16.21.16" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="/"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
此外,全部启用集群功能的web应用程序,其web.xml中都须添加<distributable/>才能实现集群功能。若是某web应用程序没有本身的web.xml,也能够经过复制默认的web.xml至其WEB-INF目录中实现。
(2).修改web.xml
node15:
[root@node15 ~]# cd /webapps/ROOT/
[root@node15 ROOT]# ls
index.jsp
[root@node15 ROOT]# mkdir WEB-INF
[root@node15 ROOT]# # ls
index.jsp WEB-INF
[root@node15 ROOT]# # cp /usr/local/tomcat/conf/web.xml WEB-INF/
[root@node15 ~]# vim /usr/local/tomcat/conf/web.xml
#增长一行<distributable/>
node16:
[root@node16 ~]# cd /webapps/ROOT/
[root@node16 ROOT]# ls
index.jsp
[root@node16 ROOT]# mkdir WEB-INF
[root@node16 ROOT]# # ls
index.jsp WEB-INF
[root@node16 ROOT]# # cp /usr/local/tomcat/conf/web.xml WEB-INF/
[root@node16 ~]# vim /usr/local/tomcat/conf/web.xml
#增长一行<distributable/>
(3).启动tomcat服务器
node15:
[root@node15 ~]# catalina.sh start
node16:
[root@node16 ~]# catalina.sh start
(4).查看一下tomcat集群日志
[root@node15 ~]#tail -f /usr/local/tomcat/logs/catalina.xxxx-xx-xx.log
最后我是基于mod_jk模块实现负载均衡的apache(步骤前面已经实现)
Nginx实现Tomcat负载均衡:
安装nginx
[root@node17 ~]# yum install -y nginx
[root@node17 ~]# vim /etc/nginx/conf.d/default.conf
配置nginx负载均衡
#
# The default server
#
upstream tomcat {
server 192.168.18.201;
server 192.168.18.202;
}
server {
listen 80 default_server;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
proxy_pass http://tomcat;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
检查配置文件并启动服务器
[root@node17 ~]# nginx –t
[root@node17 ~]# service nginx start
好了,到这里Nginx实现tomcat的负载均衡与会话共享配置完成。