若依官网提供的集成redis实现集群会话,redis使用的是单机版,如果需要采用Redis集群还有做小的修改

application.yml配置示例

spring:
    redis:
        password:    # 密码(默认为空)
        timeout: 3000  # 连接超时时长(毫秒)
        cluster:
            nodes: #改为实际集群地址
                - 127.0.0.1:6370
                - 127.0.0.1:6371
                - 127.0.0.1:6372
                - 127.0.0.1:6373
                - 127.0.0.1:6374
                - 127.0.0.1:6375

在参考官网的修改完了之后,修改ShiroConfig.java文件

    @Autowired
    private Environment environment;

    /**
     * Cache Manager (shiro-redis)
     */
    @Bean
    public RedisCacheManager redisCacheManager()
    {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(redisManager());
        redisCacheManager.setPrincipalIdFieldName("userId");
        return redisCacheManager;
    }

    @Bean
    public JedisCluster getJedisCluster() {
        String ipPort = environment.getProperty("spring.redis.cluster.nodes[0]");
        Set<HostAndPort> nodes = new HashSet<>();
        while (ipPort != null) {
            String[] ipAndPort = ipPort.split(":");
            nodes.add(new HostAndPort(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1])));
            ipPort = environment.getProperty("spring.redis.cluster.nodes["+nodes.size()+"]");
        }
        String password = environment.getProperty("spring.redis.password");

        if ( password.length()==0)
            password = null;
        int timeout = Integer.parseInt(environment.getProperty("spring.redis.timeout"));

        return new JedisCluster(nodes, timeout, timeout, 2, password, new GenericObjectPoolConfig());
    }

    /**
     * RedisManager (shiro-redis)
     */
    @Bean
    public RedisClusterManager redisManager()
    {
        RedisClusterManager redisManager = new RedisClusterManager();
        redisManager.setJedisCluster(getJedisCluster());
        redisManager.setTimeout(expireTime * 60);
        return redisManager;
    }

参考资料:

官网集成redis实现集群会话

http://doc.ruoyi.vip/ruoyi/document/cjjc.html

springboot+shiro+redis(集群redis版)整合教程
https://blog.csdn.net/weixin_33704234/article/details/86444067?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242

Logo

快速构建 Web 应用程序

更多推荐