Tomcat+Nginx+Memcached

2020-08-27 00:29:32 浏览数 (1)

说明: 通过Nginx解析静态页面并将动态负载均衡调度给后面的多个Tomcat,Tomcat解析java动态程序。 由于http是无状态的协议,你访问了页面A,然后在访问B,http无法确定这2个访问来自一个人,因此要用cookie或session来跟踪用户,根据用户身份来显示不同的页面。比如用户A登陆了,那么能看到自己的个人信息,而B没登陆,无法看到个人信息。还有A可能在购物,把商品放入购物车,此时B也有这个过程,你无法确定A,B的身份和购物信息,所以需要一个session ID来维持这个过程。所以就用到了session管理。 实验用的全部工具: 链接:https://pan.baidu.com/s/1kV2xCOcncJKrQOWAWB0EzA 提取码:wewe

环境

代码语言:javascript复制
nginx 129.204.95.233
tomcat1 106.52.23.151
tomcat2 106.52.22.151

部署memcached

代码语言:javascript复制
[root@memcached /]# yum -y install memcached
[root@memcached /]# systemctl start memcached
[root@memcached /]# lsof -i:11211
COMMAND    PID      USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
memcached 4543 memcached   26u  IPv4  23285      0t0  TCP *:memcache (LISTEN)
memcached 4543 memcached   27u  IPv6  23286      0t0  TCP *:memcache (LISTEN)
memcached 4543 memcached   28u  IPv4  23289      0t0  UDP *:memcache 
memcached 4543 memcached   29u  IPv6  23290      0t0  UDP *:memcache 

部署nginx

安装nginx

代码语言:javascript复制
[root@nginx /]# yum -y install nginx
[root@nginx /]# nginx -v
nginx version: nginx/1.16.1

编辑配置文件

代码语言:javascript复制
[root@nginx /]# vim /etc/nginx/nginx.conf #添加下面两个配置
upstream tomcat {
    server 106.52.23.151:8080 weight=1;
    server 106.52.22.151:8080 weight=1;
}

location ~.*.jsp$ {
    proxy_pass http://tomcat;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}

编辑测试文件

代码语言:javascript复制
[root@nginx /]# echo "<h1>Nginx IP:129.204.95.233</h1>" >> /usr/share/nginx/html/index.html
[root@nginx /]# systemctl start nginx

部署tomcat

两台tomcat服务器都要执行下面的所有操作 安装JDK

代码语言:javascript复制
tomcat1:
[root@tomcat1 /]# yum localinstall jdk-8u211-linux-x64.rpm
[root@tomcat1 /]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

tomcat2:
[root@tomcat2 /]# yum localinstall jdk-8u211-linux-x64.rpm
[root@tomcat2 /]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

安装tomcat

代码语言:javascript复制
tomcat1:
[root@tomcat1 /]# tar xf apache-tomcat-9.0.21.tar.gz -C /usr/local/
[root@tomcat1 /]# ln -s /usr/local/apache-tomcat-9.0.21 /usr/local/tomcat
[root@tomcat1 /]# echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile
[root@tomcat1 /]# source /etc/profile

tomcat2:
[root@tomcat2 /]# tar xf apache-tomcat-9.0.21.tar.gz -C /usr/local/
[root@tomcat2 /]# ln -s /usr/local/apache-tomcat-9.0.21 /usr/local/tomcat
[root@tomcat2 /]# echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile
[root@tomcat2 /]# source /etc/profile

下载memcached-session-manager等相关软件包并copy到tomcat安装目录的lib目录中

代码语言:javascript复制
tomcat1:
[root@tomcat1 /]# mkdir we && cd we
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.0/memcached-session-manager-2.3.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc9/2.3.0/memcached-session-manager-tc9-2.3.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/2.3.0/msm-kryo-serializer-2.3.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.12.2/spymemcached-2.12.2.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.42/kryo-serializers-0.42.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.0/reflectasm-1.11.0.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/com/esotericsoftware/kryo/4.0.0/kryo-4.0.0.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/org/ow2/asm/asm/7.0/asm-7.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/org/objenesis/objenesis/3.0.1/objenesis-3.0.1.jar
[root@tomcat1 we]# cp ./* /usr/local/tomcat/lib/

tomcat2:
[root@tomcat2 /]# mkdir we && cd we
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.0/memcached-session-manager-2.3.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc9/2.3.0/memcached-session-manager-tc9-2.3.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/2.3.0/msm-kryo-serializer-2.3.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.12.2/spymemcached-2.12.2.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.42/kryo-serializers-0.42.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.0/reflectasm-1.11.0.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/com/esotericsoftware/kryo/4.0.0/kryo-4.0.0.jar
[root@tomcat1 we]# wget https://repo1.maven.org/maven2/org/ow2/asm/asm/7.0/asm-7.0.jar
[root@tomcat1 we]# wget http://repo1.maven.org/maven2/org/objenesis/objenesis/3.0.1/objenesis-3.0.1.jar
[root@tomcat2 we]# cp ./* /usr/local/tomcat/lib/

编辑配置文件,添加连接memcached

在<Context>和</Context>里面加上下面一段

代码语言:javascript复制
tomcat1:
[root@tomcat1 /]# vim /usr/local/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:106.52.8.76:11211" 
        lockingMode="auto"
        sticky="false"
        requestUriIgnorePattern= ".*.(png|gif|jpg|css|js)$"  
        sessionBackupAsync= "false"  
        sessionBackupTimeout= "100"  
        copyCollectionsForSerialization="true"  
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />

tomcat2:
[root@tomcat2 /]# vim /usr/local/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:106.52.8.76:11211" 
        lockingMode="auto"
        sticky="false"
        requestUriIgnorePattern= ".*.(png|gif|jpg|css|js)$"  
        sessionBackupAsync= "false"  
        sessionBackupTimeout= "100"  
        copyCollectionsForSerialization="true"  
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />

准备测试文件

代码语言:javascript复制
tomcat1:
[root@tomcat1 /]# rm -rf /usr/local/tomcat/webapps/*
[root@tomcat1 /]# mkdir /usr/local/tomcat/webapps/ROOT
[root@tomcat1 /]# vim /usr/local/tomcat/webapps/ROOT/index.jsp
SessionID:<%=session.getId()%> <BR>
SessionIP:<%=request.getServerName()%> <BR>
SessionPort:<%=request.getServerPort()%>
<br>Hello World tomcat1 106.52.23.151</br>

tomcat2:
[root@tomcat2 /]# rm -rf /usr/local/tomcat/webapps/*
[root@tomcat2 /]# mkdir /usr/local/tomcat/webapps/ROOT
[root@tomcat2 /]# vim /usr/local/tomcat/webapps/ROOT/index.jsp
SessionID:<%=session.getId()%> <BR>
SessionIP:<%=request.getServerName()%> <BR>
SessionPort:<%=request.getServerPort()%>
<br>Hello World tomcat2 106.52.22.151</br>

启动tomcat

代码语言:javascript复制
tomcat1:
[root@tomcat1 /]# /usr/local/tomcat/bin/startup.sh
[root@tomcat1 /]# lsof -i:8080
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    8355 root   64u  IPv6  37482      0t0  TCP *:webcache (LISTEN)

tomcat2:
[root@tomcat2 /]# /usr/local/tomcat/bin/startup.sh
[root@tomcat2 /]# lsof -i:8080
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    19920 root   64u  IPv6  47857      0t0  TCP *:webcache (LISTEN)

0 人点赞