• Tomcat8.5 基于redis配置session(non-stick)共享


    架构图:

    一、环境准备

    • 时间同步,确保NTP或Chrony服务正常运行。
    • 防火墙规则
    • 禁用SELinux
    • 四台主机
    IP 主机名 服务 软件
    10.0.1.8 proxy 调度器 Nginx
    10.0.1.17 redis redis Redis
    10.0.1.18 t1 tomcat1   Tomcat8、JDK8
    10.0.1.28 t2 Tomcat2 Tomcat8、JDK8

     二、一键安装Tomcat

    #!/bin/bash
    #Date 2020-12-7
    #Author yds
    #源码包放在/root目录下
    . /etc/init.d/functions
    set -e
    JDK_DIR="/usr/local"
    TOMCAT_DIR="/usr/local/src"
    JDK_PKG="jdk-8u271-linux-x64.tar.gz"
    TOMCAT_PKG="apache-tomcat-8.5.50.tar.gz"
    DIR=`pwd`
    
    
    install_jdk(){
    tar xvf $DIR/$JDK_PKG -C $JDK_DIR
    
    cd $JDK_DIR && ln -s jdk1.8.* jdk
    cat > /etc/profile.d/jdk.sh <<EOF
    export JAVA_HOME=$JDK_DIR/jdk
    export JRE_HOME=$JAVA_HOME/jre
    export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
    export PATH=$PATH:$JAVA_HOME/bin
    EOF
    . /etc/profile.d/jdk.sh
    }
    
    install_tomcat(){
    tar xvf $DIR/$TOMCAT_PKG -C $TOMCAT_DIR
    cd $TOMCAT_DIR && ln -s apache-tomcat-8.5.50 tomcat
    echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
    . /etc/profile.d/tomcat.sh
    id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat
    cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
    JAVA_HOME=$JDK_DIR/jdk
    EOF
    chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/
    cat > /lib/systemd/system/tomcat.service <<EOF
    [Unit]
    Description=Tomcat
    #After=syslog.target network.target remote-fs.target nss-lookup.target
    After=syslog.target network.target
    [Service]
    Type=forking
    EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
    ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
    ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
    RestartSec=3
    PrivateTmp=true
    User=tomcat
    Group=tomcat
    [Install]
    WantedBy=multi-user.target
    EOF
    systemctl daemon-reload
    systemctl enable --now tomcat.service && action "安装成功" || action "安装失败"
    }
    
    install_jdk
    install_tomcat

    三、安装并配置Redis(这里redis没有设置密码)

    [root@redis ~]#dnf -y install redis
    [root@redis ~]#sed -i.bak 's/^bind.*/bind 0.0.0.0/' /etc/redis.conf
    [root@redis ~]#systemctl enable --now redis

    四、修改tomcat配置指定redis服务器地址,并构建jsp测试页面

    [root@t1 ~]#vim /usr/local/src/tomcat/conf/context.xml

    <Context>
      ...
      <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="redis://10.0.1.17:6379"
        sticky="false"
        sessionBackupAsync="false"
        lockingMode="uriPattern:/path1|/path2"
        requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
        />
    </Context>

    [root@t1 ~]#cd /usr/local/src/tomcat/

    [root@t1 tomcat]#mkdir -p /data/webapps/ROOT

    [root@t1 tomcat]#vi /data/webapps/ROOT/index.jsp

    <%@ page import="java.util.*" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>tomcat test</title>
    </head>
    <body>
    <h1> tomcat website1 </h1>                                                                                                                                                                
    <div>On <%=request.getServerName() %></div>
    <div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
    <div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
    <%=new Date()%>
    </body>
    </html>

    [root@t1 tomcat]#vi conf/server.xml

    ...
    <Engine name="Catalina" defaultHost="t1.magedu.org" jvmRoute="Tomcat1">
    ...
    <Host name="t1.magedu.org"  appBase="/data/webapps"
                unpackWARs="true" autoDeploy="true">
    ...

    [root@t1 ~]#systemctl restart tomcat

    查看日志

    [root@t1 ~]#tail -f /usr/local/src/tomcat/logs/catalina.out

    - operation timeout: 1000
    - node ids: []
    - failover node ids: []
    - storage key prefix: null
    - locking mode: uriPattern:/path1|/path2 (expiration: 5s)
    --------
    14-Dec-2020 10:14:13.567 INFO [t1.magedu.org-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/webapps/ROOT] has finished in [888] ms
    14-Dec-2020 10:14:13.573 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
    14-Dec-2020 10:14:13.607 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
    14-Dec-2020 10:14:13.609 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1005 ms

    t2和t1配置相同

    [root@t2 ~]#vim /usr/local/src/tomcat/conf/context.xml

    <Context>
      ...
      <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="redis://10.0.1.17:6379"
        sticky="false"
        sessionBackupAsync="false"
        lockingMode="uriPattern:/path1|/path2"
        requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
        />
    </Context>

    [root@t2 tomcat]#mkdir -p /data/webapps/ROOT/

    [root@t2 tomcat]#vim /data/webapps/ROOT/index.jsp

    <%@ page import="java.util.*" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>tomcat test</title>
    </head>
    <body>
    <h1> tomcat website2 </h1>                                                                                                                                                                
    <div>On <%=request.getServerName() %></div>
    <div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
    <div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
    <%=new Date()%>
    </body>
    </html>

    [root@t2 tomcat]#chown -R tomcat.tomcat /data/webapps/ROOT/

    [root@t2 ~]#systemctl restart tomcat

    五、上传tomcat session管理工具包至/usr/local/src/tomcat/lib目录下

    六、修改nginx配置(Nginx安装过程比较简单,这里就省略了)

    [root@proxy ~]#vi /apps/nginx/conf/nginx.conf

    http {
    ......
    upstream tomcat-server {
    #ip_hash;
    server t1.magedu.org:8080;
    server t2.magedu.org:8080;
    }
    server {
    ......
    location / {
    }
    location ~* .(jsp|do)$ {
    proxy_pass http://tomcat-server;
    }

    修改之后重启Nginx

     最后分别重启Redis,Nginx,tomcat服务

    访问测试

     

     重复刷新页面,可以看到SessionID不变,说明配置成功。

    使用redis工具连接redis 查看SessionID

    [root@redis ~]#redis-cli 
    127.0.0.1:6379> keys *
    1) "70ACB6451F7564A4288D8242C8EF0179.Tomcat2"
    2) "validity:0E48B447DCA7E79634C19162A188269D.Tomcat2"
    3) "validity:70ACB6451F7564A4288D8242C8EF0179.Tomcat2"
    4) "5936E7DCEC7E62791C636B013D314DBF.Tomcat1"
    5) "0E48B447DCA7E79634C19162A188269D.Tomcat2"
    6) "validity:5936E7DCEC7E62791C636B013D314DBF.Tomcat1"
    127.0.0.1:6379> get 70ACB6451F7564A4288D8242C8EF0179.Tomcat2
    "x00x02x00\x00x00x01v_x06xf2Nx00x00x01v_x06xf2Nx00x00a11x00x00x01v_x06xf2Px00x00x01v_x06xf2x97x00(70ACB6451F7564A4288D8242C8EF0179.Tomcat2x00x00x00x00x00x00x00x00x01x00"
    127.0.0.1:6379> 

    备注tomcat-cluster-redis-session-manager相关jar包下载地址

    链接:https://pan.baidu.com/s/1sMzr-2YlLbq-MO4F0XmxCA
    提取码:knf9

  • 相关阅读:
    采用商业智能提升企业的数字营销策略
    采用商业智能提升企业的数字营销策略
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    《PostgreSQL服务器编程》一一1.3 超越简单函数
    2017 全球半导体预估跳增 11.5%,存储器最夯
    2017 全球半导体预估跳增 11.5%,存储器最夯
    如何从零学习PostgreSQL Page结构
    转成json必须是unicdoe字符
  • 原文地址:https://www.cnblogs.com/yds941268778/p/14132273.html
Copyright © 2020-2023  润新知