springboot整合redisson

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37465368/article/details/82897026

遇到的最大问题:redisson配置了password之后依然显示的是未配置密码

原因:启动redis的方式不对,没有走redis.config,本文最下边会说明。

整合步骤

1、添加redisson依赖包

<!--redission 依赖包-->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.5.4</version>
            <exclusions>
                <exclusion>
                    <artifactId>cache-api</artifactId>
                    <groupId>javax.cache</groupId>
                </exclusion>
            </exclusions>
        </dependency>

2、主项目中添加redisson相关bean

@Autowired
    private RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();

        SingleServerConfig singleServerConfig = config.useSingleServer();
        String schema = redisProperties.isSsl() ? "rediss://" : "redis://";
        singleServerConfig.setAddress(schema + redisProperties.getHost() + ":" + redisProperties.getPort());
        singleServerConfig.setDatabase(redisProperties.getDatabase());
        if (redisProperties.getPassword() != null) {
            singleServerConfig.setPassword(redisProperties.getPassword());
        }

        // 其他配置项都先采用默认值
        return Redisson.create(config);
    }

3、service层可直接编写redisson操作缓存

public void testRedisson(){
        RBucket<String > bucket = redissonClient.getBucket(redisConfig.addPrefix("CarryJey"));
        System.out.println(bucket.getName());
    }

4、启动redis服务方式(一定要一致)

(1)最简单的就是不指定配置启动$:./redis-server &

这样就不要配置spring.redis.password=了。

(2)安全起见,配置密码

redis按指定配置文件启动

项目redis密码也要配置


注意点:使用redisson需要将自己本地的redis配置文件redis.config 将requirepass foobared注释打开,(foobared即为密码)

然后重启redis:

关闭:  redis-cli -h 127.0.0.1 -p 6379 shutdown

启动:1)、指定配置文件 $: ./redis-server /usr/local/redis.conf &

          2)、不指定配置:$: ./redis-server &

          不指定配置文件启动时采用默认配置,无密码 redis通过属性requirepass 设置访问密码,但没有设置该属性时,客户端向服务端发送AUTH请求,服务端就好返回异常:ERR Client sent AUTH, but no password is set


解决问题相关资料:

1、ERR Client sent AUTH, but no password is set

https://blog.csdn.net/iw1210/article/details/72428824

2、启动、停止、重启redis服务器

https://blog.csdn.net/sinat_29330337/article/details/77740368

猜你喜欢

转载自blog.csdn.net/qq_37465368/article/details/82897026