Posts Ssh之传统方式接入分布式会话(spring session)篇
Post
Cancel

Ssh之传统方式接入分布式会话(spring session)篇

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

说明

Maven依赖

1
2
3
4
5
6
<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)。

1
2
3
4
5
6
7
8
9
10
11
12
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配置方式,实际上可以把这段配置可以以注解的形式写进上面全局配置类中。

1
2
3
4
5
6
<!-- 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传统的过滤器注册方式关联起来则需要通过过滤器代理进行注册就好了。

1
2
3
4
5
6
7
8
9
<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配置文件进行控制

1
2
3
4
spring:
 session:
     redis:
       namespace: dev

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

参考资料

http://dorole.com/1422/

This post is licensed under CC BY 4.0

Https信任证书申请与非信任证书生成方式, 适用于tls双向安全校验

Diy搭建黑群晖(xpenology)备忘记载

Comments powered by Disqus.