引入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
yml配置
单机版
1 2 3 4 5
| spring: redis: host: 127.0.0.1 port: 6379 password: 123456
|
哨兵模式
1 2 3 4 5 6 7 8 9
| spring: redis: database: 0 sentinel: master: my-master nodes: 192.168.72.132:27001,192.168.72.132:27002,192.168.72.132:27003 password: 123456
|
集群模式
1 2 3 4 5 6
| spring: redis: database: 0 cluster: nodes: 192.168.72.132:7001,192.168.72.132:7002,192.168.72.132:7003,192.168.72.132:7004,192.168.72.132:7005,192.168.72.132:7006 password: 123456
|
通用配置
1 2 3 4 5 6 7 8 9
| spring: redis: timeout: 6000ms jedis: pool: max-active: 1000 max-wait: -1ms max-idle: 10 min-idle: 5
|
RedisTemplate配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.example.springboot.common.config;
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.*; import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
@Configuration public class RedisConfig {
@Resource private RedisConnectionFactory factory;
@Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(factory); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
|