美文网首页
SpringBoot整合redis

SpringBoot整合redis

作者: 我有一只喵喵 | 来源:发表于2020-07-08 20:11 被阅读0次

一、添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

二、填写redis配置

redis配置相关类为org.springframework.boot.autoconfigure.data.redis。RedisProperties。可以通过在application.yaml中进行配置。

spring:
  redis:
    host: 192.168.0.104
    port: 6379
    #jedis客户端pool配置
    jedis:
      pool:
        #最大空闲连接数,小于0则没有限制
        max-idle: 8
        #最小空闲连接数
        minIdle: 0
        max-active: 8

    #lettuce客户端pool配置
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0

上述配置中连接池使用jedis还是lettuce可根据选择的客户端而定,参数为默认值,可不进行配置

三、使用RedisTempalte向redis写值


@SpringBootTest
class StudyApplicationTests {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("hello","redis");
    }
}

四、过程中可能出现问题解决

1)DENIED Redis is running in protected mode because protected mode is enabled
修改redis.conf中

protected-mode yes

将此值改为no,重启redis服务器

或者启动redis时使用 --protected-mode no选项重启

./redis-server --protected-mode no

2)java.net.ConnectException: Connection refused: no further information
修改redis.conf中

bind 127.0.0.1

修改绑定为0.0.0.0

相关文章

网友评论

      本文标题:SpringBoot整合redis

      本文链接:https://www.haomeiwen.com/subject/kxdhcktx.html