一、添加依赖
<!--添加redis功能--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--添加redis配置类中使用的通用连接池对象(GenericObjectPoolConfig)--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.8.1</version> </dependency>
二、创建配置类
2.1 添加连接配置信息(application.yml)
spring:
redis:
sentinel:
master: test
nodes: --1:6379,--2:6379,--3:6379
password: ------
database: 1
lettuce:
pool:
max-active: 8
max-wait: 10000
max-idle: 8
min-idle: 0
2.2 创建配置类
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.RedisServer; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; import java.util.List; /** * redis配置类 */ @EnableConfigurationProperties(RedisProperties.class) @Configuration public class RedisConfig { private final RedisProperties properties; public RedisConfig(RedisProperties properties) { this.properties = properties; } @Bean(name = "StringRedisTemplate") public StringRedisTemplate stringRedisTemplate() { StringRedisTemplate redisTemplate = new StringRedisTemplate(); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); redisTemplate.setConnectionFactory(getConnectionFactory()); return redisTemplate; } @Bean public RedisConnectionFactory getConnectionFactory() { //哨兵模式 RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(); configuration.setMaster(properties.getSentinel().getMaster()); configuration.setPassword(properties.getPassword()); configuration.setDatabase(properties.getDatabase()); List<String> nodes = properties.getSentinel().getNodes(); nodes.forEach(node -> { String[] str = node.split(":"); RedisNode redisServer = new RedisServer(str[0], Integer.parseInt(str[1])); configuration.sentinel(redisServer); }); LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration, getPool()); //使用前先校验连接,这个最好是要配置:不然会带来connection reset by peer factory.setValidateConnection(true); return factory; } @Bean public LettuceClientConfiguration getPool() { GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); //redis客户端配置:超时时间默认 LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(). commandTimeout(Duration.ofMillis(60000)); //链接池配置 RedisProperties.Pool pool = properties.getLettuce().getPool(); genericObjectPoolConfig.setMaxIdle(pool.getMaxIdle()); genericObjectPoolConfig.setMinIdle(pool.getMinIdle()); genericObjectPoolConfig.setMaxTotal(pool.getMaxActive()); genericObjectPoolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis()); builder.shutdownTimeout(Duration.ofMillis(4000)); builder.poolConfig(genericObjectPoolConfig); return builder.build(); } }
三、创建操作类
3.1 接口
public interface RedisService { /** * string 类型 设置 * * @param key * @param value * @return */ void set(String key, String value); /** * string 类型 get值 * * @param key * @return */ String get(String key); /** * 设置过期时间 * * @param key * @param expire * @return */ boolean expire(String key, long expire); }
3.2 实现
import org.springframework.data.redis.core.*; import org.springframework.stereotype.Service; import hzq.maven.demo.service.redis.RedisService; import java.util.concurrent.TimeUnit; import javax.annotation.Resource; @Service public class RedisServiceImpl implements RedisService { @Resource private StringRedisTemplate stringRedisTemplate; /** * 不允许key永久存在,最长就是1天 * @param key * @param value */ @Override public void set(final String key, final String value) { stringRedisTemplate.opsForValue().set(key, value,1L, TimeUnit.DAYS); } @Override public String get(final String key) { return stringRedisTemplate.opsForValue().get(key); } @Override public boolean expire(final String key, long expire) { try { stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS); }catch (Exception e){ return false; } return true; } }
四、使用
@Resource private final RedisService redisService; public String getTestRedis(String key){ return redisService.get(key); }