SpringBoot整合redis使用指南

一、下载redis及redis可视化工具RedisDesktopManager

​ redis的话下载解压版就行了、可视化工具直接百度就可,是免费的。

这里贴一个redis的下载地址:https://github.com/MicrosoftArchive/redis/releases

下载后解压;

在解压所在目录使用如下命令启动服务端

./redis-server.exe redis.windows.conf

启动命令端:

./redis-cli.exe -h 127.0.0.1 -p 6379

二、引入依赖POM YML

POM:

<!--jedis-->
<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
</dependency>

YML:

redis配置,以下有默认配置的也可以使用默认配置

  redis:
    host: 127.0.0.1
    port: 6379
    pool:
      max-active: 8
      max-wait: 1
      max-idle: 8
      min-idle: 0
    timeout: 0


如果换成了集群方式,配置修改如下所示:

spring:
    application:
        name: spring-boot-redis
    redis:
        host: 192.168.145.132
        port: 6379
        timeout: 20000
        cluster:
            nodes: 192.168.211.134:7000,192.168.211.134:7001,192.168.211.134:7002
            maxRedirects: 6
        pool:
            max-active: 8
            min-idle: 0
            max-idle: 8
            max-wait: -1

三、建立redis配置类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig {
    
    
   //从配置文件中获取参数注入
   @Value("${spring.redis.host}")
   private String redisHost;
   @Value("${spring.redis.port}")
   private Integer redisPort;


   //配置连接池
   @Bean
   JedisPoolConfig jedisPoolConfig(){
    
    
       JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
       return jedisPoolConfig;
   }

   //构造一个连接池对象
   @Bean
   public JedisPool jedis(JedisPoolConfig jedisPoolConfig){
    
    
       JedisPool jedisPool = new JedisPool(jedisPoolConfig,redisHost,redisPort,
               30);
       return jedisPool;
   }

}

四、在controller中的使用

在这里插入图片描述
在这里插入图片描述

五、关于可视化工具RedisDesktopManager

第一次下载安装redis需要按照上面的指引启动redis服务器

但之后的话,直接在RedisDesktopManager上连接redis就可以了
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/GBS20200720/article/details/121168971