SpringBoot整合SpringDataRedis

1 添加相关的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

2 配置信息

spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.jedis.pool.max-active=20
spring.redis.host=192.168.187.120
spring.redis.port=6379

14.3 Redis的配置类

@Configuration
public class RedisConfig {

    /**
     * 创建JedisPoolConfig对象
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis.pool")
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        System.out.println("默认值:" + config.getMaxIdle());
        System.out.println("默认值:" + config.getMinIdle());
        System.out.println("默认值:" + config.getMaxTotal());
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis.pool")
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setPoolConfig(config);
        return factory;
    }

    @Bean
    public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory){
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        // 设置 key的序列号器
        template.setKeySerializer(new StringRedisSerializer());
        // 设置 value的序列化器
        template.setValueSerializer(new StringRedisSerializer());
        return template;

    }
}

4 单元测试



@RunWith(SpringRunner.class)
@SpringBootTest
public class GpSpringbootRedisDemoApplicationTests {

    @Autowired
    private RedisTemplate<String,Object> template;

    /**
     * 添加一个简单的字符串
     */
    @Test
    public void test01() {
        this.template.opsForValue().set("name","helo redis");
    }

}

在连接出现链接超时

redis连接报错:

java redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
我确定我的代码没有问题,但是linu虚拟机的防火墙没有开
1.使用命令 firewall-cmd --state查看防火墙状态。得到结果是running或者not running

2.在running 状态下,向firewall 添加需要开放的端口

命令为 firewall-cmd --permanent --zone=public --add-port=6379/tcp //永久的添加该端口。去掉–permanent则表示临时。

4.firewall-cmd --reload //加载配置,使得修改有效。

5.使用命令 firewall-cmd --permanent --zone=public --list-ports //查看开启的端口,出现6379/tcp这开启正确



[root@192 bin]# systemctl start firewalld  查看防火墙状态。得到结果是running或者not running 
[root@192 bin]# firewall-cmd --state   
running
[root@192 bin]#  firewall-cmd --permanent --zone=public --add-port=6379/tcp 
success
[root@192 bin]# firewall-cmd --reload
success
[root@192 bin]# firewall-cmd --permanent --zone=public --list-ports
6379/tcp
[root@192 bin]# config set protected-mode "no"
bash: config: 未找到命令...
[root@192 bin]# ./redis-c
redis-check-aof  redis-check-rdb  redis-cli        
[root@192 bin]# ./redis-cli
127.0.0.1:6379> set name
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379> 
[root@192 bin]# ./redis-server ../redis.conf 
3395:C 18 Sep 2020 18:41:19.387 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3395:C 18 Sep 2020 18:41:19.387 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=3395, just started
3395:C 18 Sep 2020 18:41:19.387 # Configuration loaded
[root@192 bin]# ./redis-cli
127.0.0.1:6379> get name
"helo redis"

链接完成

/**
     *序列化对象
     */
    @Test
    public void test03() {
//       template.opsForValue().get("name");

        User user = new User(1, "zhangSan", 18);

        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.opsForValue().set("user",user);

    }

    /**
     * 将Redis中存储的User对象反序列化出来
     */
    @Test
    public void test03_1(){
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        User user = (User) this.template.opsForValue().get("user");
        System.out.println(user);
    }

    @Test
    public void test04() {
        template.opsForValue().get("name");
        System.out.println("123");
    }


    /**
     * 将User对象转换为JSON对象存储
     */
    @Test
    public void test05(){
        User user = new User(2,"李四",19);
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
        template.opsForValue().set("userJson",user);

    }

    /**
     * 将Redis中存储的JSON数据取出转换为User对象
     */
    @Test
    public void test06(){
        template.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
        User userJson = (User) template.opsForValue().get("userJson");
        System.out.println(userJson);
    }


在序列化出现的报错信息如下:
ResponseError: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

究其原因是因为强制把redis快照关闭了导致不能持久化的问题,在网上查了一些相关解决方案。

通过stop-writes-on-bgsave-error值设置为no即可避免这种问题。

链接: 有两种解决方法.

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页