Springboot如何配置Redis服务连接

配置文件

如果redis设置了密码,一定要写

spring:
  application:
    name: redis-server
    database: 1           # Redis服务器数据库
    host: 127.0.0.1  # Redis服务器地址
    port: 6379        # Redis服务器连接端口
    timeout: 6000ms       # 连接超时时间(毫秒)
    jedis:
      pool:
        max-active: 200   # 连接池最大连接数(使用负值表示没有限制)
        max-wait: -1      # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 10      # 连接池中的最大空闲连接
        min-idle: 0       # 连接池中的最小空闲连接
  implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
    implementation 'org.springframework.data:spring-data-redis:2.2.6.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.2.6.RELEASE'

Redis Config配置类

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author : Jenson.Liu
 * @date : 2020/5/14  2:07 下午
 */
@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory( factory );
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory( factory );
        return stringRedisTemplate;
    }
}

函数代码

采用的是StringRedisTemplate

import com.sap.content.migrationexcel.service.RedisService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author : Jenson.Liu
 * @date : 2020/5/14  1:43 下午
 */
@Service
public class RedisServiceImpl implements RedisService {

    private final StringRedisTemplate stringRedisTemplate;
    public static final int expire_time = 60*10*10;


    public RedisServiceImpl(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public void setString(String key, String value) {
        stringRedisTemplate.opsForValue().set(key,value,expire_time, TimeUnit.SECONDS);
    }

    @Override
    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public void setHash(String key, String hashKey, String hashValue) {
        stringRedisTemplate.opsForHash().put(key,hashKey,hashValue);
        stringRedisTemplate.expire(key,expire_time,TimeUnit.SECONDS);
    }

    @Override
    public String getHashValue(String key, String hashKey) {
        return stringRedisTemplate.opsForHash().get(key,hashKey).toString();
    }

    @Override
    public boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }

}


 

SpringBoot配置 Redis 连接池需要以下步骤: 1. 引入 Redis 相关依赖,例如 jedis 或 lettuce。 2. 在 application.properties 或 application.yml 中配置 Redis 连接池相关属性,如下: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 spring.redis.lettuce.pool.max-active=8 spring.redis.lettuce.pool.max-idle=8 spring.redis.lettuce.pool.min-idle=0 spring.redis.lettuce.pool.max-wait=-1ms ``` 3. 在 Java 代码中通过 @Bean 注解创建 RedisConnectionFactory 和 RedisTemplate 对象,如下: ```java @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.database}") private int database; @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port); redisStandaloneConfiguration.setDatabase(database); redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); return new LettuceConnectionFactory(redisStandaloneConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 以上代码中的 LettuceConnectionFactory 和 RedisTemplate 对象的创建需要使用到 Redis 的相关依赖,在 pom.xml 文件中添加以下依赖即可: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值