1 Star 0 Fork 1

李林烜/final-session

forked from 凌康/final-session 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

final-session

final-session 一个轻量级分布式session框架,它可以无限水平扩展你的集群,使集群会话变得简单。

        轻巧、易于配置、低入侵是 final-session 的设计理念,轻盈而美。

core

支持redis、数据库存储会话session,推荐使用redis存储方案。通过自定义生成不同集群ID,读写访问不同的redis集群,从而实现节点无限扩展,架构图如下:

集群架构图

demo展示 集群架构图

快速入门

方式一:

直接从gitee下载最新jar包:https://gitee.com/lingkang_top/final-session/releases

方式二:

拉取代码,使用maven打包

mvn clean package

得到项目核心包:final-session-core/target/final-session-core-1.0.0.jar
final-session-core-1.0.0.jar引入项目 例如Maven中:

<dependency>
    <groupId>top.lingkang</groupId>
    <artifactId>final-session-core</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/final-session-core-1.0.0.jar</systemPath>
</dependency>

配置

使用配置

继承FinalSessionConfigurerAdapter类进行配置。
在spring中配置:(必须在所有拦截器的前面)

@Order(-19951219)
@Component
public class MyFinalSessionConfig extends FinalSessionConfigurerAdapter {
    @Override
    protected void configurer(FinalSessionProperties properties) {
        // 对项目进行配置
    }
}

在传统 Servlet 3.1+ 配置

public class MyFinalSessionConfig extends FinalSessionConfigurerAdapter {
    @Override
    protected void configurer(FinalSessionProperties properties) {
        // 对项目进行配置
    }
}

然后在web.xml中配置:(必须在所有拦截器的前面)

<filter-mapping>
    <filter-name>sessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>sessionFilter</filter-name>
    <filter-class>top.lingkang.exampleservlet.config.MyFinalSessionConfig</filter-class>
</filter>

会话名称和会话ID

默认的cookie为fs,会话ID为UUID,可以定制

    @Override
    protected void configurer(FinalSessionProperties properties) {
        // 自定义会话时长
        properties.setMaxValidTime(19951219L);
        // 自定义cookie名称
        properties.setCookieName("lingkang");
        // 配置id生成规则
        properties.setIdGenerate(new IdGenerate() {
            @Override
            public String generateId(HttpServletRequest request) {
            // 自定义id的值,可以根据不同id前缀访问不同redis集群,从而实现集群无限扩展
            return UUID.randomUUID().toString();
            }
        });

        // 更多配置 ...
    }

会话的存储

通过存储于db、nosql中,实现分布式会话存储。

存储于redis中

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    protected void configurer(FinalSessionProperties properties) {
        // 默认会话存储于内存中,下面将会话存储于redis中,需要引入RedisTemplate依赖
        properties.setRepository(new FinalRedisRepository(redisTemplate));
    }

若想在可视化工具看到key,可自行反序列化

存储于数据库

目前支持了mysql(表名必须fs_session),你可以自行扩展FinalRepository接口,以支持其他数据库。

CREATE TABLE `fs_session`
(
    `id`          varchar(64) NOT NULL,
    `session`     blob,
    `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

忽略了数据库连接等,配置如下

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    protected void configurer(FinalSessionProperties properties) {
        // 默认会话存储于内存中,下面将会话存储于mysql中,需要引入JdbcTemplate依赖
        properties.setRepository(new FinalDataBaseRepository(jdbcTemplate));
    }

若默认方案不满意,可自行扩展FinalRepository接口进行自定义。final-session准备了许多可扩展接口,你可以尽情发挥你的idea。

其他

有问题issues,也可以邮箱:ling-kang@qq.com

也能打赏我:
pay

MIT License Copyright (c) 2022 凌康 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

final-session 一个轻量级分布式session框架,它可以无限水平扩展你的集群,使集群会话变得简单。 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/iceman123/final-session.git
git@gitee.com:iceman123/final-session.git
iceman123
final-session
final-session
master

搜索帮助

Cb406eda 1850385 E526c682 1850385