1. Install tomcat
2. Install redis
3. Install nginx
After the above three are installed:
4. Tomcat session sharing configuration steps
Add the jar package redis session cluster depends on to the TOMCAT_BASE/lib directory
* tomcat-redis-session-manager-2.0.0.jar
* jedis-2.5.2.jar
* commons-pool2-2.2.jar
Modify the context.xml file in the TOMCAT_BASE/conf directory
Add the following under the <Context> tag
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="ip"
port="6379"
database="0"
password="1"
maxInactiveInterval="300"/>
Important: java.lang.ClassNotFoundException
Valve className or Manager className may report this exception at startup, which should be caused by different versions.
At this time, open the tomcat-redis-session-manager jar package,
Find the specific directory where the RedisSessionHandlerValve class or the RedisSessionManager class is located, this is true. Just replace it.
Attribute Explanation:
1. host redis server address
2. port The port number of the redis server
3. The redis database index to be used by database
4. maxInactiveInterval session maximum idle timeout time, if not filled, use tomcat's timeout time, generally tomcat defaults to 1800, which is half an hour
5. sessionPersistPolicies session saving strategy, in addition to the default strategy, you can also choose the following strategies:
[SAVE_ON_CHANGE]: It will be saved every time session.setAttribute() and session.removeAttribute() are triggered.
Note: This function cannot detect changes of specific properties that already exist in redis,
Tradeoff: This strategy will slightly reduce session performance, any changes will be saved to redis.
[ALWAYS_SAVE_AFTER_REQUEST]: Force save after every request, whether or not changes are detected.
Note: This option is especially useful for changing a session attribute that is already stored in redis.
Tradeoff: Not recommended if not all requests require changing session attributes, as it will increase concurrency contention.
* 1234567
6. sentinelMaster redis cluster master node name (Redis cluster is built by sharding plus master-slave to meet scalability requirements)
7. connectionPoolMaxTotal
8. connectionPoolMaxIdle The maximum number of connections that jedis can keep in the idle state
9. connectionPoolMinIdle is the opposite of connectionPoolMaxIdle
10. maxWaitMillis jedis pool when no object is returned, the maximum waiting time
11. minEvictableIdleTimeMillis
12. softMinEvictableIdleTimeMillis
13. numTestsPerEvictionRun
14. testOnCreate
15. When testOnBorrow jedis calls the borrowObject method, is there a valid check?
16. When testOnReturn jedis calls the returnObject method, is there a valid check?
17. testWhileIdle
18. timeBetweenEvictionRunsMillis
19. evictionPolicyClassName
20. blockWhenExhausted
21. jmxEnabled
22. jmxNameBase
23. jmxNamePrefix * **
24. password redis password
5. Other configuration session expiration time
<session-config>
<session-timeout>5</session-timeout>
</session-config>
6. Understand.
Session is a session key generated by the client accessing the tomcat container for the first time. First, the server calls
getSession (true) under HttpServletRequest to generate a session (true is a parameter). The method of creating sessionid is provided by
the ManagerBase class of tomcat
: the method of generating sessionId random number + time + jvmid.
The StandardManager class stores the session
in memory
,
It can also be persisted to file, database, memcache, redis, etc.
The client only saves the sessionid in the cookie
, but does not save the session . The session can only be destroyed by invalidate or timeout. Closing the browser will not close the session.