tomcat

在centos7.3虚拟机环境下

nginx服务器
www      172.18.70.60

tomcat服务器
node1    172.18.70.40
node2    172.18.70.50

同步时间
dns解析

yum 源安装: 端口号(http协议端口:8080、管理接口:8005、AJP协议端口:8009)

[root@node1 ~]#yum -y install java-1.8.0-openjdk-devel tomcat tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp

手动提供一测试类应用,并冷部署:

[root@node1 ~]#mkdir -pv /usr/share/tomcat/webapps/myapp/{lib,classes,WEB-INF}
[root@node1 ~]#vim /usr/share/tomcat/webapps/myapp/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@node2 ~]#mkdir -pv /usr/share/tomcat/webapps/myapp/{lib,classes,WEB-INF}
[root@node2 ~]#vim /usr/share/tomcat/webapps/myapp/index.jsp

<%@ page language="java" %>
<html>
        <head><title>TomcatA</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>

配置调度器nginx
配置nginx

[root@www /etc/nginx]#vim nginx.conf

 default_type        application/octet-stream;
    upstream tcsrvs {      定义服务器组
        server 172.18.70.40:8080;
        server 172.18.70.50:8080;
        }

        location / {
                proxy_pass http://tcsrvs;   传递后端服务器地址
        }
[root@www /etc/nginx]#nginx -t
[root@www /etc/nginx]#systemctl start nginx
由于网页有缓存,有时刷不出来最好使用curl 命令看到更清楚
[root@www /etc/nginx]#for i in {1..10}; do curl -s http://172.18.70.60/myapp/index.jsp |grep -i tomcat ;done

负载均衡效果没有问题了
nginx转换成http测试下效果

httpd + tomcat集群基于balancer实现

[root@www /etc/nginx]#yum install httpd
[root@www /etc/httpd/conf.d]#vim http-tomcat.conf     基于http模块反向代理的负载均衡配置
<proxy balancer://tcsrvs>                                          
                                BalancerMember http://172.18.70.40:8080      # loadfactor=1  添加权重
                                BalancerMember http://172.18.70.50:8080      # loadfactor=2
                                ProxySet lbmethod=byrequests    
                        </Proxy>

                        <VirtualHost *:80>
                                ServerName www.centos.com
                                ProxyVia On
                                ProxyRequests Off
                                ProxyPreserveHost On
                                <Proxy *>
                                        Require all granted
                                </Proxy>
                                ProxyPass / balancer://tcsrvs/
                                ProxyPassReverse / balancer://tcsrvs/
                                <Location />
                                        Require all granted
                                </Location>
                        </VirtualHost>
[root@www /etc/httpd/conf.d]#httpd -t
[root@www /etc/httpd/conf.d]#systemctl start httpd
[root@www /etc/httpd/conf.d]#ss -ntl
跟之前测试一样用curl测试相对准确点,页面访问有缓存不容易出效果
[root@www /etc/httpd/conf.d]#for i in {1..10}; do curl -s http://172.18.70.60/myapp/index.jsp |grep -i tomcat ;done

基于ajp模块实现会话粘性:

[root@www /etc/httpd/conf.d]#killall httpd
[root@www /etc/httpd/conf.d]#cp http-tomcat.conf ajp-tomcat.conf
[root@www /etc/httpd/conf.d]#mv http-tomcat.conf{,.bak}
[root@www /etc/httpd/conf.d]#vim ajp-tomcat.conf 

<proxy balancer://tcsrvs>
        BalancerMember ajp://172.18.70.40:8009      # status=D 标记某个服务不可用   在A主机出现问题的时候,访问信息会调度到B主机
        BalancerMember ajp://172.18.70.50:8009       
        ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
        ServerName www.centos.com
        ProxyVia On
        ProxyRequests Off
        ProxyPreserveHost On
        <Proxy *>
                Require all granted
        </Proxy>
        ProxyPass / balancer://tcsrvs/
        ProxyPassReverse / balancer://tcsrvs/
        <Location />
                Require all granted
        </Location>
</VirtualHost>
[root@www /etc/httpd/conf.d]#systemctl restart httpd
[root@www /etc/httpd/conf.d]#for i in {1..10}; do curl -s http://172.18.70.60/myapp/index.jsp |grep -i tomcat ;done

启用管理接口:內建管理接口,专用于bakancer ,不要开放此端口给别人,http也可以使用,效果一样

[root@www /etc/httpd/conf.d]#vim ajp-tomcat.conf 

<proxy balancer://tcsrvs>
        BalancerMember ajp://172.18.70.40:8009
        BalancerMember ajp://172.18.70.50:8009
        ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
        ServerName www.centos.com
        ProxyVia On
        ProxyRequests Off
        ProxyPreserveHost On
        <Proxy *>
                Require all granted
        </Proxy>
        ProxyPass / balancer://tcsrvs/
        ProxyPassReverse / balancer://tcsrvs/
        <Location />
                Require all granted
        </Location>
</VirtualHost>
                  #管理接口设置
<Location /balancer-manager>
        SetHandler balancer-manager
        ProxyPass !
        Require all granted
</Location>
[root@www /etc/httpd/conf.d]#httpd -t
[root@www /etc/httpd/conf.d]#systemctl reload httpd

可以看到有两个后端主机状态

LoadBalancer Status for

被访问次数

修改调度算法

 LoadBalancer Status for [balancer://tcsrvs] [p9f66a393_tcsrvs]
[root@node1 /etc/tomcat]#vim server.xml
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tcA">        #jvmRoute="ctA"

[root@node1 ~]#systemctl restart tomcat

[root@node2 ~]#vim /etc/tomcat/server.xml
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tcB">        #jvmRoute="ctB"

[root@node2 ~]#systemctl restart tomcat

基于Cookie会话源地址绑定
修改虚拟主机配置

[root@www /etc/httpd/conf.d]#vim ajp-tomcat.conf

添加响应报文首部,额外附加键值ROUTEID替换挑选出来的服务器 ,path=/是Cookie适用范围    什么时候修改对应值生效
env=如果调度的后端服务器改变了,routeid就会发生改变,否则就不变就不做重新调度,后端服务器宕机了需要重新调度,
后端服务器没有宕机ROUTEID值就不会变

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://tcsrvs>
        BalancerMember ajp://172.18.70.40:8009 route=tcA        #route=tcA映射标识符
        BalancerMember ajp://172.18.70.50:8009 route=tcB
        ProxySet lbmethod=byrequests
        ProxySet stickysession=ROUTEID      定义基于什么绑定会话
</Proxy>

<VirtualHost *:80>
        ServerName www.centos.com
        ProxyVia On
        ProxyRequests Off
        ProxyPreserveHost On
        <Proxy *>
                Require all granted
        </Proxy>
        ProxyPass / balancer://tcsrvs/
        ProxyPassReverse / balancer://tcsrvs/
        <Location />
                Require all granted
        </Location>
</VirtualHost>

<Location /balancer-manager>
        SetHandler balancer-manager
        ProxyPass !
        Require all granted
</Location>
[root@www /etc/httpd/conf.d]#httpd -t
[root@www /etc/httpd/conf.d]#systemctl reload httpd

网页测试基于Cookie绑定成功 F12

delta-manager实现会话复制集群实现
配置启用集群,将下列配置放置于或中;

[root@node1 ~]#vim /etc/tomcat/server.xml 

 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <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.70.40"      修改默认多播地址
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="172.18.70.40"        设置后端服务器地址 
 #获取本机地址,建议写固定IP,绑定的地址为auto时,会自动解析本地主机名,并解析得出的IP地址作为使用的地址
                      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>
[root@node1 ~]#scp /etc/tomcat/server.xml node2:/etc/tomcat/

[root@node2 ~]#vim /etc/tomcat/server.xml

 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <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.70.40"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="172.18.70.50"
                      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>
官方配置示例有语法问题,倒数2,3行行尾>前没有“/”符号,添加即可
http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
配置webapps:将/etc/tomcat/WEB-INF/web.xml(tomcat yum安装路径)
拷贝到当前使用的根目录下的WEB-INF下,
编辑在<web-app配置段内添加<distributable/>元素;
以上内容两台RS-tomcat配置相同,区别在与Receiver className下的address的ip需写RS本机地址。
[root@node1 ~]#cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/myapp/WEB-INF/
[root@node1 ~]#vim /usr/share/tomcat/webapps/myapp/WEB-INF/web.xml
[root@node1 ~]#systemctl restart tomcat
[root@node2 ~]#cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/myapp/WEB-INF/
[root@node2~]#vim /usr/share/tomcat/webapps/myapp/WEB-INF/web.xml
[root@node2 ~]#systemctl restart tomcat、

重启tomcat服务
测试发现尽管负载均衡切换了主机,但是session不会改变


使用msm实现session服务器实现:
http+tomcat7+memcached集群

memcached

安装memcached
[root@node1 ~]#yum install memcached -y
[root@node1 ~]#systemctl start memcached.service
[root@node1 ~]#yum -y install libmemcached    专用克隆工具
[root@node1 ~]#lftp 172.18.0.1/pub    需要安装的jar    
cd ok, cwd=/pub
lftp 172.18.0.1:/pub> cd Sources/7.x86_64/msm/
lftp 172.18.0.1:/pub/Sources/7.x86_64/msm> ls
-rw-r--r--    1 0        0          452748 Aug 21 09:14 javolution-5.4.3.1.jar
-rw-r--r--    1 0        0          147025 Aug 21 09:14 memcached-session-manager-1.8.3.jar    
-rw-r--r--    1 0        0           11284 Aug 21 09:14 memcached-session-manager-tc7-1.8.3.jar    
-rw-r--r--    1 0        0           10407 Aug 21 09:14 memcached-session-manager-tc8-1.8.3.jar
-rw-r--r--    1 0        0           71051 Aug 21 09:14 msm-javolution-serializer-1.8.3.jar
-rw-r--r--    1 0        0          459447 Aug 21 09:14 spymemcached-2.11.1.jar   
lftp 172.18.0.1:/pub/Sources/7.x86_64/msm> mget *
[root@node1 ~]#cp *.jar /usr/share/java/tomcat/    添加缓存会话管理器到Tomcat
[root@node1 ~]#scp *.jar node2:/usr/share/java/tomcat/
[root@node1 ~]#systemctl stop tomcat
[root@node1 ~]#vim /etc/tomcat/server.xml        删除原有的Cluster

 <Host name="localhost"  appBase="webapps"
             unpackWARs="true" autoDeploy="true">
 编辑server.xml文件Host上下文中定义context
 <Context path="/myapp" docBase="myapp" reloadable="ture" >
   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
     memcachedNodes="m1:172.18.70.40:11211,m2:172.18.70.50:11211"
     failoverNodes="m2"    node1宕机了node2就接替工作
     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
     transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
     />
 </Context>

搭建Java 开源博客 Solo
下载安装包

将war包发布到tomcat服务器的webapps目录下    solo-2.4.0.war
[root@node1 ~]#cp solo-2.4.0.war /usr/share/tomcat/webapps/      自动解压
[root@node1 ~]#scp /etc/tomcat/solo-2.4.0.war node2:/usr/share/tomcat/webapps/      复制到node2 tomcat机器上面
[root@node1 ~]#vim /etc/tomcat/server.xml
<Context path="/solo-2.4.0" docBase="solo-2.4.0" reloadable="ture" >    修改路径文件名字    node2也做修改
    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
      memcachedNodes="m1:172.18.70.40:11211,m2:172.18.70.50:11211"
      failoverNodes="m2"
      requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
      transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
     />
 </Context>
[root@node1 ~]#vim /usr/share/tomcat/webapps/solo-2.4.0/WEB-INF/classes/latke.properties

#### Server ####
 # Browser visit protocol
 serverScheme=http
 # Browser visit domain name
 serverHost=172.18.70.40          修改成本主机ip       node2也进行修改
 # Browser visit port, 80 as usual, THIS IS NOT SERVER LISTEN PORT!
 serverPort=8080
 
#### Cookie ####
cookieName=solo
cookieSecret=Beyond
 
#### Runtime Mode ####
#runtimeMode=DEVELOPMENT
 runtimeMode=PRODUCTION

[root@node1 ~]#systemctl restart tomcat
测试输入 tomcat 主机ip,文件存放路径

测试www代理服务器

测试成功
OK

推荐阅读更多精彩内容