Tomcat實現session會話保持(三)

在後端用session server作session存儲服務器

使用memcache作後端session server

Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它經過在內存中緩存數據和對象來減小讀取數據庫的次數,從而提升動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,可是客戶端能夠用任何語言來編寫,並經過memcached協議與守護進程通訊。這裏主要是作爲tomcat的session存儲設備css

實驗架構圖:

Tomcat實現session會話保持(三)

實驗設備

操做系統:CentOS7.6
nginx IP:192.168.8.134 
tomcat1 IP:192.168.8.160
tomcat2 IP:192.168.8.161
session server IP:192.168.8.162

環境準備html

#清空防火牆規則
iptables -F
iptables -X

#臨時設置關閉selinux
setenforce 0

#安裝jdk,centos7的源默認最高支持jdk1.8
yum -y install java-1.8.0-openjdk-devel 

#更改主機名解析
vim /etc/hosts
192.168.8.161 tomcat2
192.168.8.160 tomcat1

實驗部署步驟:

配置Nginx

安裝並配置Nginx反向代理java

[root@nginx ~]#yum -y instll nginx

修改nginx配置文件node

[root@nginx ~]#vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}
upstream web {
        server 192.168.8.160:8080;
        server 192.168.8.161:8080;
}
server {
        listen 80 default_server;
        index index.jsp;
        location / {
                proxy_pass http://web/;
        }
}

檢查無誤後啓動nginxlinux

[root@nginx ~]#nginx -t
[root@nginx ~]#systemctl start nginx
[root@nginx ~]#systemctl enable nginx

配置tomcat1

安裝tomcat以及對應的其餘管理工具nginx

[root@tomcat1 ~]#yum -y install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp

建立必要的文件夾web

[root@tomcat1 ~]#mkdir -pv /data/app/ROOT/{WEB-INF,META-INF,classes,lib}

編輯網頁文件數據庫

[root@tomcat1 ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
        <html>
                <head><title>TomcatA</title></head>
                <body>
                        <h1><font color="red">TomcatA</font></h1>
            table align="centre" border="1">
                                <tr>
                                        <td>Session ID</td>
                                <% session.setAttribute("www.zd.com","www.zd.com"); %>
                                        <td><%= session.getId() %></td>
                                </tr>
                                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
                </body>
        </html>

編輯tomcat配置文件vim

[root@tomcat1 ~]#vim /etc/tomcat/server.xml 
         #在Host中加入
         <Context path="/" docBase="ROOT" reloadable="">
                <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                                 #memcache服務器的IP地址,能夠指定多個IP,用,號隔開便可
                 memcachedNodes="n1:192.168.8.162:11211"
                                 #指定當前tomcat的主memcache服務器
                failoverNodes="n1"
                requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
        </Context>

修改tomcat-users.xml配置文件後端

[root@tomcat1 ~]#vim /etc/tomcat/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="www.test.com" roles="manager-gui,manager-script,admin-gui,admin-script"/>

把須要的軟件包拷貝到/usr/share/java/tomcat下,可從GitHub上下載

百度網盤連接:https://pan.baidu.com/s/1TukaicJ1sObH6ZakZa2qEA 
提取碼:8jwk

Tomcat實現session會話保持(三)
啓動tomcat服務

[root@tomcat1 ~]#systemctl restart tomcat
[root@tomcat1 ~]#systemctl enable tomcat

配置tomcat2

建立必要的目錄

[root@tomcat2 ~]#mkdir /data

從tomcat1上拷貝相關文件

[root@tomcat1 ~]#scp -r /data/app/ 192.168.8.161:/data/ 
[root@tomcat1 ~]#scp /etc/tomcat/server.xml 192.168.8.161:/etc/tomcat/server.xml 
[root@tomcat1 ~]#scp /etc/tomcat/tomcat-users.xml 192.168.8.161:/etc/tomcat/tomcat-users.xml 
[root@tomcat1 ~]#scp -r /usr/share/java/tomcat 192.168.8.161:/usr/share/java/tomcat

編輯網頁文件

[root@tomcat2 ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
        <html>
                <head><title>TomcatB</title></head>
                <body>
                        <h1><font color="blue">TomcatB</font></h1>
            table align="centre" border="1">
                                <tr>
                                        <td>Session ID</td>
                                <% session.setAttribute("www.test.com","www.test.com"); %>
                                        <td><%= session.getId() %></td>
                                </tr>
                                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
                </body>
        </html>

啓動tomcat服務

[root@tomcat2 ~]#systemctl restart tomcat
[root@tomcat2 ~]#systemctl enable tomcat

配置session server

安裝memcache

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

更改配置文件

[root@memchche ~]#vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
#併發鏈接數最大限制
MAXCONN="1024"
#默認最大使用的內存
CACHESIZE="64"
OPTIONS=""

啓動服務

[root@memchche ~]#systemctl enable memcached
[root@memchche ~]#systemctl start memcached

測試實驗效果,可觀察session始終同樣
Tomcat實現session會話保持(三)

相關文章
相關標籤/搜索