SSH之传统方式接入分布式会话(Spring Session)篇

背景

由于项目业务发展越来越快,开发人员越来越多;导致传统一个项目模块开发方式效率低下并且系统性能瓶颈凸显慢慢需要考虑采用分布式/微服务体系进行解耦解决各种性能与效率的问题;但在分布式环境中会碰到各种各样的问题需要解决,比较典型的就是会话管理了,这篇只针对会话管理进行展开。

说明

Maven依赖

<dependency>
   <groupId>org.springframework.session</groupId>
   <artifactId>spring-session-data-redis</artifactId>
   <version>1.2.2.RELEASE</version>
   <type>pom</type>
</dependency>

配置注解

在全局可以扫描到的地方创建一个类,加入注解@EnableRedisHttpSession,告诉Spring开启利用Redis存储Session方案;具体表现在SpringHttpSessionConfiguration类99行中(初始化SessionRepositoryFilter)。

package com.livenaked.hub.common;

import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

/**
* 全局配置类
* @author cary
* @date 2016/11/24
*/
@EnableRedisHttpSession
public class AppConfiguration {
}

配置Redis的连接池

我这里采用XML配置方式,实际上可以把这段配置可以以注解的形式写进上面全局配置类中。

<!-- Redis Connection Pool -->
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" name="connectionFactory" >
   <property name="hostName" value="${global.redis.hostname}"/>
   <property name="port"  value="${global.redis.port}"/>
   <property name="password"  value="${global.redis.password}"/>
</bean>

配置Web.xml

由于springSessionRepositoryFilter是由Spring容器进行管理的,这个时候要把这个对象与JAVAEE传统的过滤器注册方式关联起来则需要通过过滤器代理进行注册就好了。

<filter>
   <filter-name>springSessionRepositoryFilter</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
   <filter-name>springSessionRepositoryFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

REDIS Namespace控制方式

通过VM参数设置进行控制

-dspring.session.redis.namespace=dev

通过YAML配置文件进行控制

spring:
 session:
     redis:
       namespace: dev

更多的YAML的配置参考我的另一篇文章。

参考资料

http://dorole.com/1422/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值