GithubHelp home page GithubHelp logo

tomzhang / session Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ihaolin/session

0.0 2.0 0.0 657 KB

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

License: MIT License

Java 100.00%

session's Introduction

Session

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

  • 包引入(maven dependency):

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

     <dependency>
         <groupId>me.hao0</groupId>
         <artifactId>session-api</artifactId>
         <version>1.0.0</version>
     </dependency>
     <dependency>
         <groupId>redis.clients</groupId>
         <artifactId>jedis</artifactId>
         <version>2.7.2</version>
     </dependency>
     
  • Servlet-Filter机制(Mechanism)

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

  • 使用(Usage):

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

       <filter>
           <filter-name>RedisSessionFilter</filter-name>
           <filter-class>me.hao0.session.redis.RedisSessionFilter</filter-class>
           <init-param>
           	  <!-- cookie中的sessionId名称 -->
           	  <!-- the session id in cookie -->
               <param-name>sessionCookieName</param-name>
               <param-value>scn</param-value>
           </init-param>
           <init-param>
           	  <!-- session过期时间(秒) -->
           	  <!-- session expired time in seconds -->
               <param-name>maxInactiveInterval</param-name>
               <param-value>1800</param-value>
           </init-param>
           <init-param>
           	  <!-- cookie上下文路径 -->
           	  <!-- cookie context path -->
               <param-name>cookieContextPath</param-name>
               <param-value>/</param-value>
           </init-param>
           <init-param>
           	  <!-- cookie域名 -->
           	  <!-- cookie domain -->
               <param-name>cookieDomain</param-name>
               <param-value>session.com</param-value>
           </init-param>
           <init-param>
           	  <!-- cookie保存时间(秒) -->
           	  <!-- cookie lifetime in seconds -->
               <param-name>cookieMaxAge</param-name>
               <param-value>1800</param-value>
           </init-param>
    RedisSessionFilter /* ``` + 在**classpath**下配置**session.properties**(Config **session.properties** in **classpath**)
      ```ruby
      # redis model: is sentinel or not, default is not sentinel
      # session.redis.mode=	
      
      # session serialize class, default is JsonSerializer
      # session.serializer=
      
      # redis host
      session.redis.host=localhost
      
      # redis port
      session.redis.port=6379
      
      # redis max connections, default is 5
      # session.redis.pool.max.total=5
      
      # redis max idle connections, default is 2
      # session.redis.pool.max.idle=2
      
      # redis session id prefix
      session.redis.prefix=sid
      ``` 
    
  • Demo:

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

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

       upstream session_server {
           server localhost:10000;
           server localhost:10001;
       }
      
       server {
           listen 80;
           server_name demo1.session.com demo2.session.com;
       
           location / {
              proxy_pass 	http://session_server;
              proxy_set_header   X-Real-IP $remote_addr;
              proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header   Host $http_host;
           }
       }
    • 运行session-demo(run session-demo):

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

      • 用户首页(user index):

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

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

         http://demo1.session.com/users/logout
         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)。
  • 你是好人:

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

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

session's People

Contributors

ihaolin avatar

Watchers

mingfeng.zhang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.