1. 程式人生 > >Tomcat7基於Redis的Session共享

Tomcat7基於Redis的Session共享

Tomcat7/8基於Redis(Sentinel)的Session共享實戰

筆者線上環境的多個tomcat需要共享session

基於epel源安裝、啟動redis

yum install redis -y && /etc/init.d/redis start 
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 60
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis/redis_6379.pid
loglevel notice
logfile /var
/log/redis/redis_6379.log databases 16 save 60 100000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump_6379.rdb dir /var/lib/redis slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 requirepass Aniuredis123 rename-command
CONFIG "" rename-command FLUSHALL "" maxclients 10000 maxmemory 32gb maxmemory-policy volatile-lru maxmemory-samples 5 appendonly yes appendfilename "appendonly_6379.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time
-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes

安裝並配置Tomcat

在伺服器上安裝並配置兩個tomcat7,命令為tomcat7-7081、tomcat7-7082

wget http://mirrors.shuosc.org/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz -P /usr/local/src
mkdir /opt/tomcats &&  cd /usr/local/src && tar zxf apache-tomcat-7.0.82.tar.gz -C /opt/tomcats
cd /opt/tomcats && mv apache-tomcat-7.0.82 tomcat7-7082 && cp -r tomcat7-7082 tomcat7-7081

更改兩個tomcat的server.xml,修改埠

  • 編制兩個index.jsp頁面,分別放入tomcat7-7081\webapps\ROOT、tomcat7-7082\webapps\ROOT目錄下,index.jsp頁面內容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>獲取session id</title>
</head>
<body>
    Session Id : <%= request.getSession().getId() %>
</body>
</html>

Tomcat部分配置完成繼續下面操作

下載必須jar檔案

  • 將下載的jar拷貝到${TOMCAT_HOME}/lib下
[[email protected] src]# cp commons-pool2-2.2.jar jedis-2.9.0.jar tomcat-redis-session-manage-tomcat7.jar /opt/tomcats/tomcat7-7081/lib/
[[email protected] src]# cp commons-pool2-2.2.jar jedis-2.9.0.jar tomcat-redis-session-manage-tomcat7.jar /opt/tomcats/tomcat7-7082/lib/

配置Tomcat

編輯${TOMCAT_HOME}/conf/context.xml,在context中加入

    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
       host="localhost"
       port="6379"
       password='Aniuredis123'
       database="0"
       maxInactiveInterval="60" />

# 貼下使用redis sentinel的配置
       <!-- Sentinel 配置 -->
     <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />        
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
        maxInactiveInterval="60"
        sentinelMaster="mymaster"
        sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382"
        />      

兩個tomcat都需要進行配置,配置完成啟動tomcat介面如下:

Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:25 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Attached to RedisSessionHandlerValve
Dec 06, 2017 11:10:25 AM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
Dec 06, 2017 11:10:25 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Will expire sessions after 1800 seconds
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/examples has finished in 921 ms
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/host-manager
Dec 06, 2017 11:10:25 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/host-manager has finished in 341 ms
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/ROOT
Dec 06, 2017 11:10:25 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Attached to RedisSessionHandlerValve
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Will expire sessions after 1800 seconds
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/ROOT has finished in 101 ms
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/docs
Dec 06, 2017 11:10:26 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Attached to RedisSessionHandlerValve
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Will expire sessions after 1800 seconds
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/docs has finished in 121 ms
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/manager
Dec 06, 2017 11:10:26 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/manager has finished in 95 ms
Dec 06, 2017 11:10:26 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-7081"]
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1685 ms

這裡寫圖片描述

測試結果

這裡寫圖片描述

獲取的SESSIONID是同一個,說明成功了,其中71D850EDC7C97D9F87035C6A5777E263就是SESSIONID

  • 連線Redis檢視已經存在的sessionid
[root@yunwei src]# redis-cli -a Aniuredis123 -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys 71D850EDC7C97D9F8*
1) "71D850EDC7C97D9F87035C6A5777E263"

筆者的redis做了持久化

參考連結

相關推薦

Tomcat7基於Redis的Session共享

Tomcat7/8基於Redis(Sentinel)的Session共享實戰 筆者線上環境的多個tomcat需要共享session 基於epel源安裝、啟動redis yum install redis -y && /etc

Tomcat7基於Redis的Session共享實戰一

本文主要介紹如何使用redis對tomcat7的session進行託管。 1、安裝Redisredis安裝比較簡單,此處略過。 2、配置兩個Tomcat在本機上配置兩個Tomcat,分別為tomcat7-8081、tomcat7-8082。 編制兩個index.jsp頁面,分別放入tomcat7-808

61、Heartbeat V1基於NFS共享儲存的WEB高可用實戰

1、涉及機器 192.168.130.61 node1.ha.com 192.168.130.62 node2.ha.com 192.168.130.63 node3.ha.com 2、安裝heartbeat V2 rpm -ivh https://mirrors.aliyun.com/ep

Heartbeat V2基於NFS共享儲存的WEB高可用實戰(基於heartbeat-gui配置)

1、涉及機器 192.168.130.61 node1.ha.com 192.168.130.62 node2.ha.com 192.168.130.63 node3.ha.com 2、安裝heartbeat V2 rpm -ivh https://mirrors.aliyun.com/ep

63、Heartbeat V2基於NFS共享儲存的MySQL高可用實戰(heartbeat-gui)

1、涉及機器 192.168.130.61 node1.ha.com 192.168.130.62 node2.ha.com 192.168.130.63 node3.ha.com 2、安裝heartbeat V2 rpm -ivh https://mirrors.aliyun.com/ep

ActiveMQ基於檔案共享的主從配置方案

假設兩臺裝有activeMq服務的伺服器分別為:<span style="white-space:pre"> </span>192.160.102.90 192.168.102.91 並打算將91上activeMq目錄的資料儲存目錄共享給90的a

android基於Activity共享元素切換效果的實現

先上一張效果圖: 主要用到的類有: android.support.v4下的三個類: ActivityOptionsCompat,    ActivityCompat,    ViewCompat 這三個類的主要作用就是 適配不同的android版本,    即使是在不支

redis+tomcat7實現session共享

其實很簡單,就幾個步驟:  1.配置Tomcat的conf目錄下的context.xml檔案: 1> 單點Reids配置 <code class="hljs xml has-numbering" style="display: block; paddi

Tomcat7叢集共享Session 基於redis進行統一管理(轉)

背景:       很多時候,生產環境,需要多個tomcat協作,那麼session的統一管理是一個首先需要解決的問題。session的統一管理有很多解決方案,比如儲存至資料庫、memcache、redis。那麼我想給大家介紹的是將session儲存至redis這個方案。

擼代碼--linux進程通信(基於共享內存)

-- log pac 字符指針 clas fcn eno csdn printf 1.實現親緣關系進程的通信,父寫子讀 思路分析:1)首先我們須要創建一個共享內存。 2)父子進程的創建要用到fork函數。fork函數創建後,兩

suselinux12基於ISCSI的共享存儲設置

suselinux iscsitarget 實驗環境:vmware workstation12 操作系統:suse12sp2 虛擬機處於同一網段下 網絡環境架構如下 主機名角色IP地址Suse12sp2N1ISCSI initiator192.168.2.246Suse12sp2N2ISCSI initi

擼代碼--類QQ聊天實現(基於linux 管道 信號 共享內存)

time 針對 -s .net water pos 關閉 mes sigint 一:任務描寫敘述 A,B兩個進程通過管道通信,像曾經的互相聊天一樣,然後A進程每次接收到的數據通過A1進程顯示(一個新進程,用於顯示A接收到的信息),A和A1

開啟tomcat的apr模式,並利用redis做tomcat7的session的共享

pen 目錄 b- classname xsl 集群配置 sent permsize ast 更新系統組件 yum -y install readline* xmlto kernel-devel yum* screen vim* psmisc wget lrzsz p

基於kickstart實現網絡共享以及制作光盤和U盤實現半自動安裝centos6系統

centos 一、使用kickstart實現網絡共享半自動化安裝。 ①在centos6上安裝system-config-kickstart、ftpd包。 ②使用system-config-kickstart命令,編輯裏面的內容,該文件生成ks.cfg文件。 修改完之後在File菜單中選擇Sa

基於redis的session共享

1.首先啟動redis,在windows下的命令是: redis-server redis.windows.conf 2.需要在載入的jar包為(基於maven的工程): <dependency> <groupId>org.springframew

CentOS7設定資料夾共享基於VMWare Workstation 14 Pro)

1、開啟VMWare Workstation 14 Pro,選單欄找到“虛擬機器(M)”,點選,下拉選單依次選擇“設定”、“選項”,點選“共享資料夾”,在“資料夾共享”下選擇“總是啟用”,“資料夾(F)”可選擇任意路徑下的資料夾; 2、不能建立共享資料夾的解決辦法: 1)選單欄找到“

APP開發:基於Bmob後臺文件共享移動web(一)

前言 本篇部落格主要用來記錄我個人移動web小專案開發過程,當然還有一些關於javascript如何使用Bmob後臺的API,現在網上關於javascript使用Bmob後臺API的例項太少了,我目前又不會搞後臺開發,加上我的這個專案打算幾個月後提交併參加比賽有些急,後臺也來不及去研

開發實戰:基於深度學習+maven+SSM+EasyUI的高校共享汽車管理系統(二)

基於深度學習+maven+SSM+EasyUI的高校共享汽車管理系統   繼上一篇 [專案需求分析](https://blog.csdn.net/ITBigGod/article/details/82729233)之後,接下來就是資料庫設計了。      作為一個管理系統,各種資訊表是必

開發實戰:基於深度學習+maven+SSM+EasyUI的高校共享汽車管理系統(一)

基於深度學習+maven+SSM+EasyUI的高校共享汽車管理系統 1.專案簡介   在現在,共享汽車在中國各地方開始熱起來,於是本人想做一個基於maven+SSM+EasyUI的高校共享汽車管理系統,當然該專案是博主本人2019年的畢業設計,除了javaweb部分,本專案還

使用redis實現session共享(基於Spring Boot)

Session 共享 什麼是 Session 由於 HTTP 協議是無狀態的協議,所以服務端需要記錄使用者的狀態時,就需要用某種機制來識具體的使用者。Session 是另一種記錄客戶狀態的機制,不同的是 Cookie 儲存在客戶端瀏覽器中,而 Session 儲存在伺服器上。客戶端瀏覽器訪問