redis 进行session共享

一.流程图
 
操作步骤
1.引入jar包
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
2.创建springsession,springredis配置类,添加启用session的注解
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

/**
 * maxInactiveIntervalInSeconds配置是设置session的最大存活时间,默认是1800秒.
 * redis有三种模式-standalone,sentinel,cluster,
 */
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class SpringRedisSessionConfig{

    private String redisHost = "127.0.0.1";
    private Integer port = 6379;

    @Bean("redisStandaloneConnectionFactory")
    public RedisConnectionFactory redisStandaloneMasterConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, port);
        RedisConnectionFactory lettuceConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration);
        return lettuceConnectionFactory;
    }

    /**
     * sentinel模式的通道提供
     * @return
     */
//    @Bean("redisSentinelctionFactory")
//    public RedisConnectionFactory redisSentinelctionFactory() {
//        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration()
//                .master("mymaster")
//                .sentinel("10.70.33.238", 26379)
//                .sentinel("10.70.33.239", 26379)
//                .sentinel("10.70.33.246", 26379);
//        return new JedisConnectionFactory(redisSentinelConfiguration);
//    }

    /**
     * cluster模式的通道提供
     * @return
     */
//    @Bean("redisRedisClusterFactory")
//    public RedisConnectionFactory redisSentinelctionFactory() {
//        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration()
//        RedisNode redisNode1 = new RedisNode("127.0.0.1", 6379);    // 有多少个节点就写多少个节点
//        RedisNode redisNode2 = new RedisNode("127.0.0.1", 6379);
//        redisClusterConfiguration.addClusterNode(redisNode1);
//        redisClusterConfiguration.addClusterNode(redisNode2);
//        return new JedisConnectionFactory(redisClusterConfiguration);
//    }

    // 配置resdTemplate 
    @Bean("redisTemplate")
    public RedisTemplate<String, String> redisTemplate(@Qualifier("redisStandaloneConnectionFactory") RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}
 
3. 将自定义配置SpringRedisSessionConfig注册给spring的AbstractHttpSessionApplicationInitializer管理
/**
 * 为了使每个serverlet容器都使用 spring 提供的 springSessionRepositoryFilter 过滤器创建此类.
 * 上述过滤器,作用是用Spring会话支持的自定义实现替换HttpSession
 * @author 愉快淡定
 */
public class ForSpringSessionRepositoryFilter extends AbstractHttpSessionApplicationInitializer {
    public ForSpringSessionRepositoryFilter() {
        super(SpringRedisSessionConfig.class);   //前面写的配置类
    }
}
 
 
5.启动测试
 
 
 
 
6.使用redisTemplate
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping
@RestController
public class Demo01Controller {

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/a")
    public String test() {
        redisTemplate.opsForValue().set("aa", "abcdefghijklmnopqrstuvwxyz");
        final Object aa = redisTemplate.opsForValue().get("aa");
        System.out.println("aa = " + aa);
        return (String) aa;
    }
}
 
 
 
 

posted @ 2022-05-15 13:05  得好好活  阅读(506)  评论(0)    收藏  举报