业务场景
单点登录服务器如果压力过大的情况,那么可以使用集群分担压力,但是cas 默认不支持session同步。
所以可以需要做session同步,可以使用j2cache 实现session同步。
实现方法
1.准备jar包
将这些包copy 到cas的 WEB-INF/lib目录下
2.编辑CAS web.xml 文件
在web.xml 的
<filter-mapping> <filter-name>encoding-filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
后面添加
<filter> <filter-name>j2cache-session-filter</filter-name> <filter-class>net.oschina.j2cache.session.J2CacheSessionFilter</filter-class> <init-param><!-- 内存中存放会话数 --> <param-name>session.maxSizeInMemory</param-name> <param-value>2000</param-value> </init-param> <init-param><!-- 会话有效期,单位:秒钟 --> <param-name>session.maxAge</param-name> <param-value>1800</param-value> </init-param> <!-- cookie configuration --> <init-param> <param-name>cookie.name</param-name> <param-value>J2CACHE_SESSION_ID</param-value> </init-param> <init-param> <param-name>cookie.path</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>cookie.domain</param-name> <param-value></param-value> </init-param> <init-param> <param-name>redis.mode</param-name> <param-value>single</param-value> </init-param> <init-param> <param-name>redis.hosts</param-name> <param-value>192.168.31.100:6379</param-value> </init-param> <init-param> <param-name>redis.channel</param-name> <param-value>j2cache</param-value> </init-param> <init-param> <param-name>redis.cluster_name</param-name> <param-value>j2cache</param-value> </init-param> <init-param> <param-name>redis.timeout</param-name> <param-value>2000</param-value> </init-param> <init-param> <param-name>redis.password</param-name> <param-value></param-value> </init-param> <init-param> <param-name>redis.database</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>redis.maxTotal</param-name> <param-value>100</param-value> </init-param> <init-param> <param-name>redis.maxIdle</param-name> <param-value>10</param-value> </init-param> <init-param> <param-name>redis.minIdle</param-name> <param-value>10</param-value> </init-param> </filter> <filter-mapping> <filter-name>j2cache-session-filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
redis 可以做相应配置。
3.NGINX配置
upstream linuxidc { server 192.168.31.77:8082; server 192.168.31.77:8081; }
添加cas配置
location /cas { proxy_pass http://linuxidc/cas; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; }
这样就实现了cas session同步。