项目作者: ihaolin

项目描述 :
轻量的分布式会话组件(A Lightweight Distributed Session Component)
高级语言: Java
项目地址: git://github.com/ihaolin/session.git
创建时间: 2015-12-25T07:00:33Z
项目社区:https://github.com/ihaolin/session

开源协议:MIT License

关键词:
"distributed-session" "redis" "sessionfilter"

下载


Session

轻量的分布式会话组件(A Lightweight Distribute Session Component)

  • 包引入(maven dependency):

    1. <dependency>
    2. <groupId>me.hao0</groupId>
    3. <artifactId>session-redis</artifactId>
    4. <version>1.0.0</version>
    5. </dependency>
  • 依赖包,注意引入项目时是否需要排除(exclude if necessary):

    1. <dependency>
    2. <groupId>me.hao0</groupId>
    3. <artifactId>session-api</artifactId>
    4. <version>1.0.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>redis.clients</groupId>
    8. <artifactId>jedis</artifactId>
    9. <version>2.7.2</version>
    10. </dependency>
  • Servlet-Filter机制(Mechanism)

  • SessionFilter如何工作(How SessionFilter Works):

  • 使用(Usage):

    • web.xml中配置Filter(Config Filter in web.xml):

      1. <filter>
      2. <filter-name>RedisSessionFilter</filter-name>
      3. <filter-class>me.hao0.session.redis.RedisSessionFilter</filter-class>
      4. <init-param>
      5. <!-- cookie中的sessionId名称 -->
      6. <!-- the session id in cookie -->
      7. <param-name>sessionCookieName</param-name>
      8. <param-value>scn</param-value>
      9. </init-param>
      10. <init-param>
      11. <!-- session过期时间(秒) -->
      12. <!-- session expired time in seconds -->
      13. <param-name>maxInactiveInterval</param-name>
      14. <param-value>1800</param-value>
      15. </init-param>
      16. <init-param>
      17. <!-- cookie上下文路径 -->
      18. <!-- cookie context path -->
      19. <param-name>cookieContextPath</param-name>
      20. <param-value>/</param-value>
      21. </init-param>
      22. <init-param>
      23. <!-- cookie域名 -->
      24. <!-- cookie domain -->
      25. <param-name>cookieDomain</param-name>
      26. <param-value>session.com</param-value>
      27. </init-param>
      28. <init-param>
      29. <!-- cookie保存时间(秒) -->
      30. <!-- cookie lifetime in seconds -->
      31. <param-name>cookieMaxAge</param-name>
      32. <param-value>1800</param-value>
      33. </init-param>
      34. </filter>
      35. <filter-mapping>
      36. <filter-name>RedisSessionFilter</filter-name>
      37. <url-pattern>/*</url-pattern>
      38. </filter-mapping>
    • classpath下配置session.properties(Config session.properties in classpath)

      1. # redis model: is sentinel or not, default is not sentinel
      2. # session.redis.mode=
      3. # session serialize class, default is JsonSerializer
      4. # session.serializer=
      5. # redis host
      6. session.redis.host=localhost
      7. # redis port
      8. session.redis.port=6379
      9. # redis max connections, default is 5
      10. # session.redis.pool.max.total=5
      11. # redis max idle connections, default is 2
      12. # session.redis.pool.max.idle=2
      13. # redis session id prefix
      14. session.redis.prefix=sid
  • Demo:

    • 配置/etc/hosts(config /etc/hosts):

      1. 127.0.0.1 demo1.session.com demo2.session.com
    • 假如使用nginx作web代理,配置server(config server if use nginx):

      1. upstream session_server {
      2. server localhost:10000;
      3. server localhost:10001;
      4. }
      5. server {
      6. listen 80;
      7. server_name demo1.session.com demo2.session.com;
      8. location / {
      9. proxy_pass http://session_server;
      10. proxy_set_header X-Real-IP $remote_addr;
      11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      12. proxy_set_header Host $http_host;
      13. }
      14. }
    • 运行session-demo(run session-demo):

      1. # demo1.session.com
      2. mvn clean jetty:run -Dmaven.test.skip -Djetty.port=10000
      3. # demo2.session.com
      4. mvn clean jetty:run -Dmaven.test.skip -Djetty.port=10001
    • 测试(Test):

      • 用户首页(user index):

        1. http://demo1.session.com/users/index
        2. http://demo2.session.com/users/index
      • 用户登录(user login):

        1. http://demo1.session.com/users/login?username=admin&passwd=admin
        2. http://demo2.session.com/users/login?username=admin&passwd=admin
      • 用户登出(user logout):

        1. http://demo1.session.com/users/logout
        2. http://demo2.session.com/users/logout
  • 使用其它的存储,如Memcache(Use other storage, Memcache, etc):

    1. 继承SessionFilter(inherit SessionFilter);
    2. 实现SessionManager(implement SessionManager)。
  • 相关文档(References):

  • 历史版本(Release History):

    • 1.0.0:

      • 基本功能实现(basic implementation)。
  • 你是好人:

    • 倘若你钱多人傻花不完,小弟乐意效劳😊,掏出你的微信神器做回好人吧:

    • 倘若你还不够尽兴,继续掏出你的支付宝神器,疯狂扫吧: