Linux学习笔记
当前位置:Linux学习笔记 > Linux 软件 > 正文

Tomcat服务器搭建实验

bbs.yuanmawu.net.jpg

Tomcat服务器搭建实验

1.实现LNT

同主机实现

1、安装并启动tomcat
1)OpenJDK的安装
    yum install java-1.8.0-openjdk-devel.x86_64 
    确定JDK是否安装可以使用如下命令
        java -version    ##可以看到版本,说明安装成功
2)tomcat安装
    yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
    service tomcat start
    ss -ntl 
        监听的端口有三个:8080http协议的接口,8009为ajp协议的接口,8005为管理接口
2、安装nginx并配置
    vim /etc/nginx/nginx.conf
        在server段增加如下内容
        root     /usr/local/tomcat/webapps/ROOT;   ##表示访问静态文件就访问nginx的这个目录,此目录是tomcat默认根目录。
        index index.jsp;
        location ~* \.(jsp|do)$ {                        ##访问的是jsp或者do结尾的动态文件就调度到后端主机tomcat
                proxy_pass http://192.168.213.251:8080;
                }
    nginx -t
    service nginx restart  
    http://192.168.213.251/    ##这里直接访问80端口就可以了看到tomcat默认主页面了

2.实现LAT

同主机(静态网页)

1、安装并启动tomcat
1)OpenJDK的安装
    yum install java-1.8.0-openjdk-devel.x86_64 
    确定JDK是否安装可以使用如下命令
        java -version    ##可以看到版本,说明安装成功
2)tomcat安装
    yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
    service tomcat start
    ss -ntl 
        监听的端口有三个:8080http协议的接口,8009为ajp协议的接口,8005为管理接口
2、安装httpd服务并确保有ajp_module和http_module
    httpd -M |grep "proxy"     ##查看模块是否已经加载,如果没有加载,要在配置文件中加载这些模块
         proxy_module (shared)
         proxy_ajp_module (shared)    ##表示反向代理时后端服务器是aip协议
         proxy_balancer_module (shared)
         proxy_connect_module (shared)
         proxy_express_module (shared)
         proxy_fcgi_module (shared)
         proxy_fdpass_module (shared)
         proxy_ftp_module (shared)
         proxy_http_module (shared)   ##表示反向代理时后端主机时http协议
         proxy_scgi_module (shared)
         proxy_wstunnel_module (shared)
3、与后端tomcat使用http协议连接时配置
    vim /etc/httpd/conf.d/tomcat.conf
        <virtualhost *:80>
                documentroot "/usr/share/tomcat/webapps/ROOT"
                <directory "usr/local/tomcat/webapps/ROOT">
                require all granted
                </directory>
                proxyrequests off     ##关闭正向代理,不加可以
                proxyvia on           
                proxypreservehost on    
                proxypass "/" "http://127.0.0.1:8080/"  
                proxypassreverse "/" "http://127.0.0.1:8080/"  
        </virtualhost>
    httpd -t
    systemctl start httpd
    测试:http://172.18.21.107/
4、与后tomcat连接时使用的ajp协议时的配置
    vim /etc/httpd/conf.d/tomcat.conf
        <virtualhost *:80>
                documentroot "/usr/local/tomcat/webapps/ROOT"
                <directory "usr/local/tomcat/webapps/ROOT">
                          require all granted
                </directory>
                proxyrequests off
                proxyvia on
                proxypreservehost on
                proxypass "/" "ajp://127.0.0.1:8009/"
                proxypassreverse "/" "ajp://127.0.0.1:8009/"
        </virtualhost>
    httpd -t
    systemctl reload httpd
5、测试
    http://172.18.21.107/

3.实现lnmt的zrlog博客搭建

环境:
A主机:tomcat,nginx,ip地址为192.168.213.251
B主机:数据库,ip地址为192.168.213.253

1)在A上操作
    1、安装openjdk
        yum install java-1.8.0-openjdk-devel.x86_64 
        java -version   ---确认是否安装成功
    2、安装tomcat
        yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
        systemctl start tomcat
        http://192.168.213.251:8080   ##可以访问tomcat的默认网站
    3、将zrlog放到tomcat的根目录下
        mv  app/zrlog-1.7.1-release.war /usr/share/tomcat/webapps
        systemctl restart tomcat
        ls   ##发现文件被自动解开
        docs      host-manager  manager  zrlog-1.7.1-release
        examples  logs          ROOT     zrlog-1.7.1-release.war
        [root@CentOS7 webapps]#ln -s zrlog-1.7.1-release zrlog
    4、安装nginx并修改配置
        vim /etc/nginx/nginx.conf
            在server段增加如下内容
                 root     /usr/local/tomcat/webapps;  ##表示访问静态文件就访问nginx的这个目录,此目录是tomcat默认根目录。
                 index index.jsp;
                        location / {      ##访问的是jsp或者do结尾的动态文件就调度到后端主机tomcat,此处为本地
                        proxy_pass http://192.168.213.251:8080;
                        }
        nginx -t
        service nginx restart
        http://192.168.213.251/    ##这里直接访问80端口就可以了看到tomcat默认主页面了
2)在B上的设置
    安装数据库
        yum install mariadb-server
        systemctl start mariadb
        mysql
        create database zrlog;
        grant all on zrlog.* to zrloguser@'%' identified by "centos";
3)测试:
    登录网站
    http://192.168.213.251/zrlog/

4.实现nginx和httpd反代tomcat集群

环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254

1》nginx反代
    1、在B和C上的设置
        mkdir -pv /usr/share/tomcat/webapps/myapp/WEB-INF
        vim /usr/share/tomcat/webapps/myapp/index.jsp
            <%@ page language="java" %>
            <%@ page language="java" %>
            <html>
            <head><title>TomcatA</title></head>
            <body>
            <h1><font color="red">TomcatA.com</font></h1>   ##在C上将颜色改为green,Tomcat改为B
            <table align="centre" border="1">
            <tr>
            <td>Session ID</td>
            <% session.setAttribute("shen.com","shen.com"); %>
            <td><%= session.getId() %></td>
            </tr>
            <tr> 
            <td>Created on</td> 
            <td><%= session.getCreationTime() %></td> 
            </tr>   
            </table>
            </body> 
             </html> 
        http://172.18.213.251:8080/myapp/
        http://172.18.213.254:8080/myapp/
    2、A上设置
        vim /etc/nginx/nginx.conf
            在http中配置
                upstream tomcatsrv { 
                        server 192.168.213.251:8080 ;
                    server 192.168.213.254:8080 ;
                    }
                  server {
                        listen 80 default_server;
                    listen       [::]:80 default_server;
                    index index.jsp ;
                    root /usr/share/tomcat/webapps/app;
                    location / {
                        proxy_pass http://tomcatsrv ;
                    }
        service nginx restart
        http://172.18.213.253/myapp/
2》httpd反代
    1、在B和C上的设置同上
    2、在调度器上的设置
        vim /etc/httpd/conf.d/tomcat.conf
            <proxy balancer://tomcatsrvs>                  ##定义一个后端服务器组
                BalancerMember http://192.168.213.251:8080 ##如果和后端服务器连接的协议为ajp协议,把http改为ajp并且把端口改为8009即可
                BalancerMember http://192.168.213.254:8080 
                ProxySet lbmethod=byrequests        
            </proxy>
            namevirtualhost *:80
            <VirtualHost *:80>
                    documentroot /app
                    <Directory /app>
                        Require all granted
                    </Directory>
                    ProxyVia On
                    ProxyRequests Off
                    ProxyPass / balancer://tomcatsrvs/
                    ProxyPassReverse / balancer://tomcatsrvs/
            </VirtualHost>
        httpd -t
        service httpd start
        http://172.18.213.253/app/
        httpd的负载集群功能具有健康状态检查功能
        可以把后端的一个tomcat停掉
        然后访问http://172.18.213.253/app/
        会发现不往关闭的tomcat主机调度了

5.实现httpd和nginx的会话粘性绑定

环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254

1)httpd会话绑定
        在B和C上设置
            vim /etc/tomcat/server.xml 
                 <Engine name="Catalina" defaultHost="localhost" jvmroute="tomcatA">   ##在B上此行增加一个jvmroute="tomcatA,在C上此行增加jvmroute="tomcatB"
            systemctl restart tomcat
        在A上设置
            vim /etc/httpd/conf.d/tomcat.conf
                Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED 
                <proxy balancer://tomcatsrvs>
                    BalancerMember http://172.18.213.251:8080 route=tomcatA
                    BalancerMember http://172.18.213.254:8080 route=tomcatB
                    ProxySet lbmethod=byrequests
                    ProxySet stickysession=ROUTEID
                </Proxy>
                namevirtualhost *:80
                <VirtualHost *:80>
                    documentroot /app
                    <directory /app>
                        Require all granted
                    </directory>
                    ProxyVia On
                    ProxyRequests Off
                    ProxyPass / balancer://tomcatsrvs/
                    ProxyPassReverse / balancer://tomcatsrvs/
                </VirtualHost>
            service httpd reload
        http://172.18.213.253/app/

2)nginx会话绑定
    在B和C上操作同上
    在A上配置
            vim /etc/nginx/nginx.conf
                upstream tomcatsrv { 
                    server 192.168.213.251:8080 ;
                    server 192.168.213.254:8080 ;
                    hash $request_uri consistent;
                }
                server {
                    listen 80 default_server;
                    listen [::]:80 default_server;
                    index index.jsp ;
                    root /usr/share/tomcat/webapps;
                    location / {
                        proxy_pass http://tomcatsrv ;
                    }
                 }
            service nginx restart
    http://172.18.213.253/app
        发现只能调度至第一次访问的后端服务器,实现会话绑定。

6.两个tomcat服务器保存有相同的会话

环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254

1)在B和C上的设置
    访问tomcat的官方文档
            http://192.168.213.254:8080/docs/cluster-howto.html  ,Document---->Clustering
    将官方文档中的如下内容复制到tomcat配置文件的<engine><host>中,此实验放到Engine中
    vim /etc/tomcat/server.xml 
            <Engine name="Catalina" defaultHost="localhost">
            <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.74.74.74"  
                port="45564"
                frequency="500"        ##表示每0.5s发送一次心跳信息告诉其他成员自己还活着
                dropTime="3000"/>   ##表示3s没有发送信息就证明坏了
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                address="172.18.213.251"   ##另外一台主机修改为172.18.213.254
                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>
            
    cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/app/WEB-INF/   
    cd /usr/share/tomcat/webapps/app/WEB-INF/
    vim web.xml 
            在此文件内部没有注释的地方加如下内容
            <distributable/>   ##注意一定要在这个文件的内部,就在<web.app下加上就可
    systemctl restart tomcat
    注意:
        CentOS 7上的tomcat自带的文档中的配置示例有语法错误;没有加最后的/
                <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
                <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
2)在A的设置
        vim /etc/httpd/conf.d/tomcat.conf
            <proxy balancer://tomcatsrvs>
                BalancerMember http://172.18.213.251:8080 
                BalancerMember http://172.18.213.254:8080
                ProxySet lbmethod=byrequests
            </Proxy>
            namevirtualhost *:80
            <VirtualHost *:80>
                documentroot /app
                <directory /app>
                    Require all granted
                </directory>
                ProxyVia On
                ProxyRequests Off
                ProxyPass / balancer://tomcatsrvs/
                ProxyPassReverse / balancer://tomcatsrvs/
            </VirtualHost>
        service httpd reload
    http://172.18.213.253/app/

6.实现session会话保持到memcache服务器

要想将会话保存至后端memcache中,并且每个memcache都保存相同的会话,需要一个管理项目memcached-session-manager,项目地址:https://github.com/magro/memcached-session-manager,找到SetupAndConfiguration wiki page.并点击进去。其中
在Add memcached-session-manager jars to tomcat处下载(根据所需进行下载)
    下载.jar文件至各tomcat的/usr/share/tomcat/lib/目录中,其中的${version}要换成你所需要的版本号,tc${6,7,8}要选与tomcat版本相同的版本号。
        memcached-session-manager-2.1.1.jar
        memcached-session-manager-tc7-2.1.1.jar   ##要根据tomcat的版本,实验时是7版本,所以这里要下载tc7
        spymemcached-2.9.1.jar
在Add custom serializers to your webapp (optional)处下载(根据所需进行下载)
    这里下载的是kryo-serializer,有如下jar文件需要下载
        msm-kryo-serializer-2.1.1.jar
        kryo-serializers-0.42.jar
        kryo-4.0.1.jar
        minlog-1.3.0.jar
        reflectasm-1.11.3-shaded.jar
        reflectasm-1.11.3.jar
        asm-5.2.jar
        objenesis-2.6.jar

实现过程如下

        1、在director上实现nginx或者httpd的反向代理至tomcat集群,本实验用的是httpd
                vim /etc/httpd/conf.d/tomcat.conf 
                    <proxy balancer://tomcatsrvs>
                        BalancerMember http://172.18.213.251:8080
                        BalancerMember http://172.18.213.254:8080
                        ProxySet lbmethod=byrequests
                    </Proxy>
                    namevirtualhost *:80
                    <VirtualHost *:80>
                        documentroot /app
                        <directory /app>
                            Require all granted
                        </directory>
                        ProxyVia On
                        ProxyRequests Off
                        ProxyPass / balancer://tomcatsrvs/
                        ProxyPassReverse / balancer://tomcatsrvs/
                    </VirtualHost>
                service httpd start
        2、在两个后端服务器上的设置
                安装tomcat和memcache并启动服务
                yum install memcached -y
                systemctl start memcached
                ls /usr/share/tomcat/lib/          ##复制.jar文件到此目录
                vim /etc/tomcat/server.xml  ##将官方文档中的此段内容复制到tomcat的配置文件中
                     <Context path="/app" docBase="/usr/share/tomcat/webapps/app" reloadable="true">
                     <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                        memcachedNodes="n1:172.18.213.254:11211,n2:172.18.213.254:11211"
                        failoverNodes="n1"
                             requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
                    </Context>
                systemctl restart tomcat
        3、测试
                安装客户端工具
                yum install -y libmemcached    ##不安装此软件包无法使用memdump等客户端工具
                http://172.18.213.253/app/      ##访问发现被调度到不同的tomcat主机,但会话是一样的
                memdump --server 172.18.21.107:11211     ##此命令可以查看到memcache中缓存的值
                systemctl stop memcached     ##关闭一台memcached
                http://172.18.213.253/app/      ##继续访问发现会话仍然不变,说明会话在两台memcache中都缓存了

更多Tomcat相关教程见以下内容

CentOS 6.6下安装配置Tomcat环境  http://www.searu.org/Linux/2015-08/122234.htm

RedHat Linux 5.5安装JDK+Tomcat并部署Java项目  http://www.searu.org/Linux/2015-02/113528.htm 

Tomcat权威指南(第二版)(中英高清PDF版+带书签)  http://www.searu.org/Linux/2015-02/113062.htm 

Tomcat 安全配置与性能优化 http://www.searu.org/Linux/2015-02/113060.htm 

Linux下使用Xshell查看Tomcat实时日志中文乱码解决方案 http://www.searu.org/Linux/2015-01/112395.htm 

CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤 http://www.searu.org/Linux/2015-01/111485.htm 

Ubuntu 16.04下安装Tomcat 8.5.9  http://www.searu.org/Linux/2017-06/144809.htm

Ubuntu 16.04安装Tomcat 8 图解  http://www.searu.org/Linux/2017-10/147773.htm

Tomcat配置文件server.xml详解  http://www.searu.org/Linux/2017-10/148003.htm

Tomcat单机多实例部署-多项目部署  http://www.searu.org/Linux/2017-10/147259.htm

Tomcat 的详细介绍请点这里
Tomcat 的下载地址请点这里

未经允许不得转载:Linux学习笔记 » Tomcat服务器搭建实验

赞 (0)
分享到:更多 ()