当前位置: 首页 > 工具软件 > shiro-redis > 使用案例 >

shiro-redis集成lettuce(支持集群、哨兵、单机)

南宫阳冰
2023-12-01

发现类库shiro-redis版本没人维护,并且还是使用的jedis,想自己集成一下lettuce,现已发布到github上面,可以引入到项目当中
项目地址

1.修改Maven settings.xml

  <mirrors>
	<mirror>
      <id>nexus-aliyun</id>
      <mirrorOf>*,!jitpack.io</mirrorOf>
      <name>Nexus aliyun</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>

2.导入pom.xml

注意:jedis请使用4.XX版本

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://www.jitpack.io</url>
        </repository>
    </repositories>

	<dependencies>
		<dependency>
            <groupId>com.github.TsukasaHwan</groupId>
            <artifactId>shiro-redis</artifactId>
            <version>3.4.1</version>
        </dependency>
	</dependencies>

3.与Spring-Boot集成

    private final RedisProperties redisProperties;
    
    /**
     * 单机配置
     *
     * @return
     */
    @Bean
    public LettuceRedisManager lettuceRedisManager() {
        LettuceRedisManager lettuceRedisManager = new LettuceRedisManager();
        String password = redisProperties.getPassword();
        if (StringUtils.isNotBlank(password)) {
            lettuceRedisManager.setPassword(password);
        }
        lettuceRedisManager.setHost(redisProperties.getHost());
        lettuceRedisManager.setPort(redisProperties.getPort());
        lettuceRedisManager.setTimeout(redisProperties.getTimeout());
        lettuceRedisManager.setDatabase(redisProperties.getDatabase());
        // 可选项是否开启异步,默认不开启
        // lettuceRedisClusterManager.setIsAsync();
        // 客户端选项
        // lettuceRedisSentinelManager.setClientOptions();
        // 连接池配置
        // lettuceRedisSentinelManager.setGenericObjectPoolConfig();
        return lettuceRedisManager;
    }

    /**
     * 集群配置
     *
     * @return
     */
    @Bean
    public LettuceRedisClusterManager lettuceRedisClusterManager() {
        LettuceRedisClusterManager lettuceRedisClusterManager = new LettuceRedisClusterManager();
        lettuceRedisClusterManager.setPassword(redisProperties.getPassword());
        lettuceRedisClusterManager.setDatabase(redisProperties.getDatabase());
        lettuceRedisClusterManager.setNodes(redisProperties.getCluster().getNodes());
        // 可选项是否开启异步,默认不开启
        // lettuceRedisClusterManager.setIsAsync();
        // 客户端选项
        // lettuceRedisClusterManager.setClusterClientOptions();
        // 连接池配置
        // lettuceRedisSentinelManager.setGenericObjectPoolConfig();
        return lettuceRedisClusterManager;
    }

    /**
     * 哨兵模式
     *
     * @return
     */
    @Bean
    public LettuceRedisSentinelManager lettuceRedisSentinelManager() {
        LettuceRedisSentinelManager lettuceRedisSentinelManager = new LettuceRedisSentinelManager();
        lettuceRedisSentinelManager.setPassword(redisProperties.getPassword());
        lettuceRedisSentinelManager.setDatabase(redisProperties.getDatabase());
        lettuceRedisSentinelManager.setMasterName(redisProperties.getSentinel().getMaster());
        lettuceRedisSentinelManager.setNodes(redisProperties.getSentinel().getNodes());
        lettuceRedisSentinelManager.setSentinelPassword(redisProperties.getSentinel().getPassword());
        // 读取模式
        lettuceRedisSentinelManager.setReadFrom(ReadFrom.REPLICA_PREFERRED);
        // 可选项是否开启异步,默认不开启
        // lettuceRedisClusterManager.setIsAsync();
        // 客户端选项
        // lettuceRedisSentinelManager.setClientOptions();
        // 连接池配置
        // lettuceRedisSentinelManager.setGenericObjectPoolConfig();
        return lettuceRedisSentinelManager;
    }

    /**
     * 使用Redis实现 shiro sessionDao
     *
     * @return
     */
    @Bean
    public RedisSessionDAO redisSessionDAO(LettuceRedisManager lettuceRedisManager) {
        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
        redisSessionDAO.setRedisManager(lettuceRedisManager);
        return redisSessionDAO;
    }

    /**
     * redis实现缓存
     *
     * @return
     */
    @Bean
    public RedisCacheManager redisCacheManager(LettuceRedisManager lettuceRedisManager) {
        RedisCacheManager redisCacheManager = new RedisCacheManager();
        redisCacheManager.setRedisManager(lettuceRedisManager);
        return redisCacheManager;
    }

spring-boot-starter

项目地址
注意:需要单独引入jedis或lettuce类库。如若使用spring-boot则无需引入lettuce类库,默认使用lettuce,否则需要引入其中一个类库

        <dependency>
            <groupId>com.github.TsukasaHwan</groupId>
            <artifactId>shiro-redis-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>

1. application.yml
单机配置:

shiro:
  redis:
  	#客户端类型(jedis,lettuce)
    client-type: lettuce
    #redis地址
    host: ${spring.redis.host}
    #redis password 没有可不配置
    password: ${spring.redis.password}
    #redis db
    database: ${spring.redis.database}
    #redis port
    port: ${spring.redis.port}
    #连接超时时间
    timeout: ${spring.redis.timeout}
    #session redis key前缀 可不配置
    session:
      key-prefix: ''
    cache:
      #cache redis key前缀 可不配置
      key-prefix: ''
      # 主体id字段名称 默认为id 可不配置
      principal-id-field-name: 'id'
      # 缓存过期时间单位秒,默认1800秒 可不配置
      expire: 1800s
    lettuce:
      #lettuce是否异步 默认false
      async: false
 类似资料: