首页 > 代码库 > 38 tomcat lb cluster、memcached和msm、msm及jvm虚拟机性能监控、tcpdump和nc工具的使用

38 tomcat lb cluster、memcached和msm、msm及jvm虚拟机性能监控、tcpdump和nc工具的使用

01 tomcat lb cluster


配置环境

node1 192.168.1.131 CentOS 7.2

node4 192.168.1.132 CentOS 7.2

node3 192.168.1.133 CentOS 7.2


1、如何部署多个tomcat实例向用户提供服务


#node4结点配置tomcat

[root@node4 ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/

[root@node4 ~]# cd /usr/local/

[root@node4 local]# ln -s apache-tomcat-8.0.38/ tomcat

[root@node4 local]# vim /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[root@node4 local]# . /etc/profile.d/tomcat.sh

[root@node4 local]# catalina.sh version

[root@node4 local]# mkdir -p /data/webapps/ROOT

[root@node4 local]# cd tomcat/conf/

[root@node4 conf]# cp server.xml{,.bak}

[root@node4 conf]# vim server.xml

修改

<Engine name="Catalina" defaultHost="localhost">

<Engine name="Catalina" defaultHost="node4.magedu.com">

添加Host主机

 <Host name="node4.magedu.com" appBase="/data/webapps" autoDeploy="true">

<Context path="" docBase="ROOT"  />

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"

  prefix="web1_access_log" suffix=".txt"

  pattern="%h %l %u %t &quot;%r&quot; %s %b" />

 </Host>

 

[root@node4 conf]# mkdir /data/logs

[root@node4 conf]# catalina.sh configtest

[root@node4 conf]# mkdir -p /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}

[root@node4 conf]# vim /data/webapps/ROOT/index.jsp

<%@ page language="java" %>

<%@ page import="java.util.*" %>

<html>

<head>

<title>JSP Test Page on Web1</title>

</head>

<body>

<% out.println("Hello,web1."); %>

</body>

</html>     

[root@node4 conf]# catalina.sh start

[root@node4 conf]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.131   node1   node1.magedu.com

192.168.1.132   node4   node4.magedu.com

192.168.1.133   node3   node3.magedu.com


[root@node4 conf]# curl node4.magedu.com:8080


#node3结点配置tomcat

[root@node4 ~]# scp apache-tomcat-8.0.38.tar.gz node3:/root

[root@node4 ~]# scp -rp /data/ node3:/data

[root@node3 ~]# tar xf apache-tomcat-8.0.38.tar.gz -C /usr/local/

[root@node3 ~]# cd /usr/local/

[root@node3 local]# ln -s apache-tomcat-8.0.38/ tomcat

[root@node4 ~]# cd /usr/local/tomcat/conf/

[root@node4 conf]# scp server.xml node3:/usr/local/tomcat/conf/

[root@node3 local]# cd tomcat/conf/

[root@node3 conf]# vim server.xml 

修改

<Engine name="Catalina" defaultHost="localhost">

<Engine name="Catalina" defaultHost="node3.magedu.com">

修改

node4.magedu.com

node3.magedu.com

[root@node4 conf]# scp /etc/profile.d/tomcat.sh node3:/etc/profile.d/

[root@node3 conf]# . /etc/profile.d/tomcat.sh

[root@node3 conf]# catalina.sh start

[root@node1 ~]# curl node3.magedu.com:8080

[root@node3 conf]# vim /data/webapps/ROOT/index.jsp

<%@ page language="java" %>

<%@ page import="java.util.*" %>

<html>

<head>

<title>JSP Test Page on Web1</title>

</head>

<body>

<% out.println("Hello,web2."); %>

</body>

</html>     



[root@node1 ~]# yum -y install nginx

[root@node1 ~]# cd /etc/nginx/

[root@node1 nginx]# cp nginx.conf{,.bak}

[root@node1 nginx]# vim nginx.conf

在server段前添加

upstream tcsrvs {

   ip_hash; #对客户端锁定服务器

server node4.magedu.com:8080;

server node3.magedu.com:8080;

}

在server段内添加

location ~* \.(jsp|do)$ {

proxy_pass http://tcsrvs;

}

[root@node1 nginx]# nginx -t

[root@node1 nginx]# systemctl start nginx.service


2、如何使用httpd反代用户请求

(1)方法一

apache:

mod_proxy

mod_proxy_http

mod_proxy_balancer

tomcat:

http connector

[root@node1 nginx]# systemctl stop nginx.service

[root@node1 nginx]# systemctl disable nginx.service

[root@node1 nginx]# yum -y install httpd

[root@node1 ~]# cd /etc/httpd/conf

[root@node1 conf]# vim httpd.conf 

注释掉

#DocumentRoot "/var/www/html"

[root@node1 conf]# cd ../conf.d/

[root@node1 conf.d]# vim vhost.conf

<proxy balancer://lbcluster1>

BalancerMember http://192.168.1.132:8080 loadfactor=10 route=TomcatA

BalancerMember http://192.168.1.133:8080 loadfactor=10 route=TomcatB

</proxy>

<VirtualHost *:80>

ServerName web1.magedu.com

ProxyVia On

ProxyRequests Off 

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / balancer://lbcluster1/

ProxyPassReverse / balancer://lbcluster1/

<Location />

Require all granted

</Location>

</VirtualHost>

如果需要会话绑定,可使用下面的内容:

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED

<proxy balancer://lbcluster1>

BalancerMember http://192.168.1.132:8080 loadfactor=10 route=TomcatA

BalancerMember http://192.168.1.133:8080 loadfactor=10 route=TomcatB

ProxySet stickysession=ROUTEID

</proxy>

<VirtualHost *:80>

ServerName web1.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / balancer://lbcluster1/

ProxyPassReverse / balancer://lbcluster1/

<Location />

Require all granted

</Location>

</VirtualHost>

[root@node1 conf.d]# systemctl start httpd.service 

[root@node4 conf]# vim server.xml

修改

<Engine name="Catalina" defaultHost="node4.magedu.com">

<Engine name="Catalina" defaultHost="node4.magedu.com" jvRoute="TomcatA">

[root@node3 conf]# vim server.xml

修改

<Engine name="Catalina" defaultHost="node3.magedu.com">

<Engine name="Catalina" defaultHost="node3.magedu.com" jvRoute="TomcatB">

[root@node4 conf]# catalina.sh stop

[root@node3 conf]# catalina.sh stop


[root@node4 conf]# vim /data/webapps/ROOT/index.jsp 

<%@ page language="java" %>

<html>

 <head><title>TomcatA</title></head>

 <body>

<h1><font color="red">TomcatA.magedu.com</font></h1>

<table align="centre" border="1">

 <tr>

<td>Session ID</td>

<% session.setAttribute("magedu.com","magedu.com"); %>

<td><%= session.getId() %></td>

 </tr>

 <tr>

<td>Created on</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

 </body>

</html>

[root@node3 conf]# vim /data/webapps/ROOT/index.jsp 

<%@ page language="java" %>

<html>

 <head><title>TomcatB</title></head>

 <body>

<h1><font color="blue">TomcatB.magedu.com</font></h1>

<table align="centre" border="1">

 <tr>

<td>Session ID</td>

<% session.setAttribute("magedu.com","magedu.com"); %>

<td><%= session.getId() %></td>

 </tr>

 <tr>

<td>Created on</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

 </body>

</html>

[root@node4 conf]# catalina.sh start

[root@node3 conf]# catalina.sh start

测试

http://192.168.1.131/index.jsp

结果:成功

(2)方法二

apache:

mod_proxy

mod_proxy_ajp

mod_proxy_balancer

tomcat:

http connector


[root@node1 conf.d]# cp vhost.conf vhost.ajp.conf 

[root@node1 conf.d]# mv vhost.conf vhost.http.conf.bak

[root@node1 conf.d]# vim vhost.ajp.conf 

#Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED

<proxy balancer://lbcluster1>

BalancerMember ajp://192.168.1.132:8009 loadfactor=10 route=TomcatA

BalancerMember ajp://192.168.1.133:8009 loadfactor=10 route=TomcatB

#ProxySet stickysession=ROUTEID

</proxy>

<VirtualHost *:80>

ServerName web1.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / balancer://lbcluster1/

ProxyPassReverse / balancer://lbcluster1/

<Location />

Require all granted

</Location>

</VirtualHost>

[root@node1 conf.d]# systemctl restart httpd.service


(3)方法3

apache

mod_jk

tomcat:

ajp connector

[root@node1 ~]# tar xf tomcat-connectors-1.2.42-src.tar.gz 

[root@node1 ~]# cd tomcat-connectors-1.2.42-src/native/

[root@node1 native]# yum -y install httpd-devel gcc glibc-devel

[root@node1 native]# which apxs

/usr/bin/apxs

[root@node1 native]# ./configure --with-apxs=/usr/bin/apxs

[root@node1 native]# make -j 4 && make install

[root@node1 native]# cd /etc/httpd/


02 tomcat lb cluster


1、jk_mod:额外编译安装


(1)反向代理

[root@node1 ~]# cd /etc/httpd/conf.d/

[root@node1 conf.d]# mv vhost.ajp.conf{,.bak}

[root@node1 conf.d]# vim mod_jk.conf

LoadModule jk_module modules/mod_jk.so

JkWorkersFile  /etc/httpd/conf.d/workers.properties

JkLogFile  logs/mod_jk.log

JkLogLevel  debug

JkMount  /*  TomcatA

JkMount  /status/  stat1

[root@node1 conf.d]# vim /etc/httpd/conf.d/workers.properties

worker.list=TomcatA,stat1

worker.TomcatA.port=8009

worker.TomcatA.host=192.168.1.132

worker.TomcatA.type=ajp13

worker.TomcatA.lbfactor=1

worker.stat1.type = status

[root@node1 conf.d]# httpd -t

[root@node1 conf.d]# systemctl restart httpd.service


(2)负载均衡

[root@node1 ~]# cd /etc/httpd/conf.d/

[root@node1 conf.d]# vim mod_jk.conf

LoadModule jk_module modules/mod_jk.so

JkWorkersFile  /etc/httpd/conf.d/workers.properties

JkLogFile  logs/mod_jk.log

JkLogLevel  debug

JkMount  /*  lbcluster1

JkMount  /status/  stat1

[root@node1 conf.d]# vim /etc/httpd/conf.d/workers.properties

worker.list = lbcluster1,stat1

worker.TomcatA.type = ajp13

worker.TomcatA.host = 192.168.1.132

worker.TomcatA.port = 8009

worker.TomcatA.lbfactor = 1

worker.TomcatB.type = ajp13

worker.TomcatB.host = 192.168.1.133

worker.TomcatB.port = 8009

worker.TomcatB.lbfactor = 1

worker.lbcluster1.type = lb

worker.lbcluster1.sticky_session = 0

worker.lbcluster1.balance_workers = TomcatA, TomcatB

worker.stat1.type = status

注:如果要实现会话绑定,可以设置worker.lbcluster1.sticky_session = 1

[root@node1 conf.d]# httpd -t

[root@node1 conf.d]# systemctl restart httpd.service

2、Balancer内置管理功能

[root@node1 conf.d]# systemctl stop httpd.service 

[root@node1 conf.d]# mv mod_jk.conf{,.bak}

[root@node1 conf.d]# cp vhost.ajp.conf.bak vhost.ajp.conf

[root@node1 conf.d]# vim vhost.ajp.conf

添加 

<location /balancer-manager>

SetHandler balancer-manager

ProxyPass !

Require all granted

</Location> 

[root@node1 conf.d]# systemctl restart httpd.service 


3、Session Cluster

[root@node4 conf]# catalina.sh stop

[root@node4 ~]# cd /usr/local/tomcat/conf/

[root@node4 conf]# vim server.xml

在Host内部添加

<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.1.132"

 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.ClusterSessionListener"/>

</Cluster>


[root@node3 conf]# catalina.sh stop

[root@node3 ~]# cd /usr/local/tomcat/conf/

[root@node3 conf]# vim server.xml

在Host内部添加

<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.1.133"

 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.ClusterSessionListener"/>

</Cluster>


[root@node4 conf]# cp web.xml /data/webapps/ROOT/WEB-INF/

[root@node4 conf]# vim /data/webapps/ROOT/WEB-INF/web.xml 

在web-app段内(</web-app>前)添加

<distributable/>

[root@node4 conf]# scp /data/webapps/ROOT/WEB-INF/web.xml node3:/data/webapps/ROOT/WEB-INF/

[root@node4 conf]# catalina.sh start

[root@node3 conf]# catalina.sh start

[root@node4 conf]# tail /usr/local/tomcat/logs/catalina.out

[root@node1 conf.d]# systemctl stop httpd.service

[root@node1 conf.d]# rm -f vhost.http.conf

[root@node1 conf.d]# cp vhost.ajp.conf.bak vhost.ajp.conf

[root@node1 conf.d]# systemctl start httpd.service 

[root@node1 conf.d]# vim vhost.ajp.conf

[root@node1 conf.d]# systemctl stop httpd.service 

[root@node1 conf.d]# rm -f vhost.ajp.conf

[root@node1 conf.d]# cp mod_jk.conf.bak mod_jk.conf

[root@node1 conf.d]# systemctl start httpd.service

[root@node1 conf.d]# vim workers.properties

[root@node1 conf.d]# systemctl stop httpd.service

[root@node1 conf.d]# cd /etc/nginx/

[root@node1 nginx]# vim nginx.conf

修改location段的内容为:

location / { 

proxy_pass http://tcsrvs;

}   

[root@node1 nginx]# systemctl start nginx.service



03 memcached和msm



[root@node1 ~]# yum -y install memcached

[root@node1 ~]# systemctl start memcached.service 

[root@node1 ~]# yum -y install telnet

[root@node1 ~]# telnet localhost 11211

[root@node1 ~]# systemctl stop memcached.service


#显示memcached详细信息

[root@node1 ~]# memcached -u memcached -vv


[root@node1 ~]# vim /etc/sysconfig/memcached 

修改OPTIONS的值为

OPTIONS="-f 1.2"


[root@node1 ~]# systemctl restart memcached.service 



[root@node3 lib]# yum -y install memcached

[root@node3 lib]# systemctl start memcached.service 

[root@node4 conf]# cd /usr/local/tomcat/lib/

[root@node3 conf]# cd /usr/local/tomcat/lib/

[root@sotre msm]# ls *

memcached-session-manager-1.8.3.jar

memcached-session-manager-tc8-1.8.3.jar

msm-javolution-serializer-1.8.3.jar

spymemcached-2.11.1.jar

[root@sotre msm]# scp * 192.168.1.132:/usr/local/tomcat/lib

[root@sotre msm]# scp * 192.168.1.133:/usr/local/tomcat/lib

[root@node4 lib]# cd ../conf/

[root@node4 conf]# vim server.xml

删除之前配置的Cluster

在Context内部添加

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="n1:192.168.1.131:11211,n3:192.168.1.133:11211"

failoverNodes="n1"

requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

/>

[root@node4 conf]# scp server.xml node3:/usr/local/tomcat/conf/

[root@node4 lib]# catalina.sh start

[root@node3 lib]# catalina.sh start

[root@node1 ~]# cd /etc/nginx/

[root@node4 conf]# rm -f /data/webapps/ROOT/WEB-INF/web.xml 

[root@node3 conf]# rm -f /data/webapps/ROOT/WEB-INF/web.xml 

[root@node4 conf]# catalina.sh stop

[root@node4 conf]# catalina.sh start

[root@node3 conf]# catalina.sh stop

[root@node3 conf]# catalina.sh start


05 tcpdump和nc工具的使用


tcp协议目标端口是80

tcpdump -i eth0 tcp dst port 80


[root@node3 ~]# yum -y install wireshark

[root@node3 ~]# yum -y install wireshark-gnome


[root@node3 ~]# yum -y install nmap


[root@node3 ~]# yum -y install nc


本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1865941

38 tomcat lb cluster、memcached和msm、msm及jvm虚拟机性能监控、tcpdump和nc工具的使用