利用Redis同步spring-session

上一篇 << 下一篇 >>>分布式全局ID生成总览


原理:

第一次创建session,往redis里缓存备份一份
第二次,先从session里面找,找不到的话在往redis里面找

注:

在springboot项目里能很好的支持,如果自己写的话,还需要过滤器等很麻烦

接入流程:

1、引入依赖


        org.springframework.boot
        spring-boot-starter-parent
        1.3.3.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-redis
        
        
        
            org.springframework.session
            spring-session-data-redis
        

    

2、加入代码

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;

//这个类用配置redis服务器的连接
//maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class SessionConfig {

    // 冒号后的值为没有配置文件时,制动装载的默认值
    @Value("${redis.hostname:localhost}")
    String HostName;
    @Value("${redis.port:6379}")
    int Port;

    @Bean
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory connection = new JedisConnectionFactory();
        connection.setPort(Port);
        connection.setHostName(HostName);
        return connection;
    }
}


import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
//初始化Session配置
public class SessionInitializer extends AbstractHttpSessionApplicationInitializer {
    public SessionInitializer() {
        super(SessionConfig.class);
    }
}

3、session使用没啥区别

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {


    @Value("${server.port}")
    private String port;

    @RequestMapping("/index")
    public String index() {
        return "server---value:" + port;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request, String key, String value) {
        HttpSession session = request.getSession();
        session.setAttribute(key, value);
        return "server---port:" + port + ",success";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request, String key) {
        HttpSession session = null;
        try {
            session = request.getSession(false);
        } catch (Exception e) {
            // TODO: handle exception
        }
        String value = null;
        if (session != null) {
            value = (String) session.getAttribute(key);
        }

        return "server---port:" + port + "  value:" + value;
    }
}

你可能感兴趣的:(利用Redis同步spring-session)