Redis solving session-based distributed consistency

What is 1.session

When a user initiates a request at the front end, the server will create a user for the current session, the server will sessionId wrote back to the client, as long as the user does not close the browser, the server requests again, sessionId passed to the server,

The server finds the corresponding session for their services according to sessionId.

 In a distributed deployment time, session is stored on the server side, the next time the user requests over, load-balancing, may be distributed to other server nodes; therefore, the need to address the issue of consistency session,

Redis be implemented using a distributed session, personal understanding is that the session stored in redis, the time came for each request, reads session information from redis

 

2. code implementation

 Dependence ①.maven need to be introduced

  

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session</artifactId>
</dependency>

②. Redis configuration file specifies the ip and port

spring.redis.host=172.18.66.45
spring.redis.port=6379

 

③.httpSession Configuration

package com.session.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
//maxInactiveIntervalInSeconds设置session有效时间,单位是秒;默认是30分钟
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=180)
public class SessionConfig {
    
    @Value("${spring.redis.host}")
    private String hostName;
    @Value("${spring.redis.port}")
    private int port;

    @Bean
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPort(port);
        connectionFactory.setHostName(hostName);
        System.out.println("获取到的hostName是:"+hostName + ",port :"+port);
        return connectionFactory;
    }
}

 

And then implement a controller, you can simulate real business scenario writing session, according to whether the login session check user

package com.session.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SessionController {

    //模拟第一次登陆时,保存session
    @RequestMapping(value="/login")
    public String login(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String str = (String) session.getAttribute("userInfo");
        if(str == null || "".equals(str)) {
            session.setAttribute("userInfo", "mpy test session");
            return "用户登录成功-success";
        }else{
            return "session中存在数据,用户已登录";
        }
    }

    /*该方法模式登陆之后,调用业务接口
*在实际的业务场景中,可以使用拦截器,对每次请求进行判断,校验用户是否登录、session是否过期
*/ @RequestMapping(value="/getSession") public String getSession(HttpServletRequest request) { HttpSession session = request.getSession(); STR String = (String) session.getAttribute ( "the userInfo" ); IF (STR == null || "" .equals (STR)) { return "no data in the session, sign" ; } the else { // logged the user, the processing of the other service code System.out.println ( "session attributes are:" + STR); return STR; } } @RequestMapping (value = "/ Zimbabwe Logout" ) public String Zimbabwe Logout (the HttpServletRequest Request) { the HttpSession the session = Request.getSession (); String str = (String) session.getAttribute("userInfo"); if(str != null && "".equals(str)) { session.invalidate(); } return "退出成功"; } }

 

 The principle @EnableRedisHttpSession and spring-session of learning to be studied

Specific code has been uploaded to GitHub, https: //github.com/mapy95/spring-session.git

Guess you like

Origin www.cnblogs.com/mpyn/p/11069179.html