概述
之前项目上线总会碰到session方面的问题,具体表现在总是要重复去登录,没有保持session,后来我们是通过redis来保持session,架构为redis+tomcat。下面讲下实现的方式和步骤。
1、安装并启动redis
[root@node1 ~]# cd /usr/local/redis/[root@node1 redis]# ll总用量 18724-rw-r--r-- 1 root root 1607 6月 5 18:38 dump.rdb-rwxr-xr-x 1 root root 5578335 6月 5 16:45 redis-benchmark-rwxr-xr-x 1 root root 5707267 6月 5 16:45 redis-cli-rw-r--r-- 1 root root 46712 6月 5 18:32 redis.conf-rwxr-xr-x 1 root root 7828042 6月 5 16:44 redis-server
[root@node1 redis]# ./redis-server 6739:C 06 Jun 17:24:54.506 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf6739:M 06 Jun 17:24:54.509 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit .-`` .-```. ```/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 6739 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 6739:M 06 Jun 17:24:54.514 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.6739:M 06 Jun 17:24:54.514 # Server started, Redis version 3.2.96739:M 06 Jun 17:24:54.514 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.6739:M 06 Jun 17:24:54.516 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.6739:M 06 Jun 17:24:54.518 * DB loaded from disk: 0.002 seconds6739:M 06 Jun 17:24:54.518 * The server is now ready to accept connections on port 6379
2、修改tomcat配置文件
2.1 修改tomcat配置文件server.xml
.... ....
.... ....
2.2 拷贝包
实现包和依赖包commons-pool2-2.3.jar、jedis-2.7.2.jar、tomcat8_redis_session-0.0.1-SNAPSHOT.jar拷贝到tomcat的lib下
[root@node1 bin]# ls /home/tomcat80/lib/annotations-api.jar catalina-tribes.jar jasper-el.jar servlet-api.jar tomcat-dbcp.jar tomcat-jdbc.jarcatalina-ant.jar commons-pool-1.6.jar jasper.jar tomcat7-websocket.jar tomcat-i18n-es.jar tomcat-redis-session-manager-1.2-tomcat-7.jarcatalina-ha.jar ecj-4.4.2.jar jedis-2.1.0.jar tomcat-api.jar tomcat-i18n-fr.jar tomcat-util.jarcatalina.jar el-api.jar jsp-api.jar tomcat-coyote.jar tomcat-i18n-ja.jar websocket-api.jar
2.3修改tomcat的context.xml配置
className实际上是tomcat8_redis_session-0.0.1-SNAPSHOT.jar中的class路径,其中tomcat8_redis_session-0.0.1-SNAPSHOT.jar依赖于commons-pool2-2.3.jar、jedis-2.7.2.jar
![bb475a88e38df17496b254017aefb1d9.png](https://img-blog.csdnimg.cn/img_convert/bb475a88e38df17496b254017aefb1d9.png)
3、修改项目
修改tomcat配置文件server.xml指向实际项目路径
在/home/project下新建rediscluster文件夹,rediscluster下新建index.jsp获取SessionID
title SessionID:
SessionIP:
SessionPort:
为了区分不同的端口,应用都放在各自tomcat下的webapp下
4、启动tomcat8080和9090端口
[root@node1 bin]# sh /home/tomcat90/bin/startup.sh Using CATALINA_BASE: /home/tomcat90Using CATALINA_HOME: /home/tomcat90Using CATALINA_TMPDIR: /home/tomcat90/tempUsing JRE_HOME: /usrUsing CLASSPATH: /home/tomcat90/bin/bootstrap.jar:/home/tomcat90/bin/tomcat-juli.jarTomcat started.
5、测试
http://127.0.0.1:8080/rediscluster
http://127.0.0.1:9090/rediscluster
![173fead1bd8bb8925cbfb9a1447444d9.png](https://img-blog.csdnimg.cn/img_convert/173fead1bd8bb8925cbfb9a1447444d9.png)
![a593da269af4e1fd67183cabb4aa1ca4.png](https://img-blog.csdnimg.cn/img_convert/a593da269af4e1fd67183cabb4aa1ca4.png)
这里可以发现访问不同的端口(即不同的tomcat)都是同个sessionID,也就证明保持会话了。
总结:
保持会话有很多实现方式,例如F5就有个参数是配置会话保持的,nginx的ip_hash参数,tomcat集群实现会话保持、weblogic集群也可以实现会话保持,会话放到memcached或者会话放到redis来保持都是可行的,具体的话大家根据项目需要选择就可以了。