Redis在单机、主从、哨兵、集群模式下,通过spring-boot-starter-data-redis如何使用。
单机
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
-
配置文件
spring: redis: host: 8.140.224.210 port: 6379 password: xiaoyuan # 如果需要密码
-
创建RedisTemplate Bean
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public JedisConnectionFactory redisConnectionFactory() { return new JedisConnectionFactory(); // 自动获取application.yml中的配置信息 } @Bean public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); return redisTemplate; } }
-
使用RedisTemplate
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; public void saveData(String key, String value) { redisTemplate.opsForValue().set(key, value); System.out.println("保存到redis中的数据为:" + redisTemplate.opsForValue().get(key)); } }
主从
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
-
配置文件
spring: redis: master: host: 8.140.224.210 port: 6380 password: xiaoyuan # 如果需要密码 slave01: host: 8.140.224.210 port: 6381 password: xiaoyuan # 如果需要密码 slave02: host: 8.140.224.210 port: 6382 password: xiaoyuan # 如果需要密码
-
创建RedisTemplate Bean
分别
masterRedisTemplate
、slave01RedisTemplate
、slave01RedisTemplate
,对应主从节点import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Value("${spring.redis.master.host}") private String masteHost; @Value("${spring.redis.master.port}") private String mastePort; @Value("${spring.redis.master.password}") private String mastePassword; @Value("${spring.redis.slave01.host}") private String slave01Host; @Value("${spring.redis.slave01.port}") private String slave01Port; @Value("${spring.redis.slave01.password}") private String slave01Password; @Value("${spring.redis.slave02.host}") private String slave02Host; @Value("${spring.redis.slave02.port}") private String slave02Port; @Value("${spring.redis.slave02.password}") private String slave02Password; @Bean public JedisConnectionFactory masterConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(masteHost); factory.setPort(mastePort); factory.setPassword(mastePassword); return factory; } @Bean public JedisConnectionFactory slave01ConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(slave01Host); factory.setPort(slave01Port); factory.setPassword(slave01Password); factory.setReadOnly(true); // 设置为只读连接 return factory; } @Bean public JedisConnectionFactory slave02ConnectionFactory() { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(slave02Host); factory.setPort(slave02Port); factory.setPassword(slave02Password); factory.setReadOnly(true); // 设置为只读连接 return factory; } @Bean public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory masterConnectionFactory) { RedisTemplate<String, String> template = new RedisTemplate<>(); template.setConnectionFactory(masterConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); return template; } @Bean public RedisTemplate<String, String> slave01RedisTemplate(JedisConnectionFactory slave01ConnectionFactory) { RedisTemplate<String, String> template = new RedisTemplate<>(); template.setConnectionFactory(slave01ConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); return template; } @Bean public RedisTemplate<String, String> slave02RedisTemplate(JedisConnectionFactory slave02ConnectionFactory) { RedisTemplate<String, String> template = new RedisTemplate<>(); template.setConnectionFactory(slave02ConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); return template; } }
-
使用RedisTemplate
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { @Autowired private RedisTemplate<String, String> masterRedisTemplate; @Autowired private RedisTemplate<String, String> slave01RedisTemplate; @Autowired private RedisTemplate<String, String> slave02RedisTemplate; public void saveData(String key, String value) { masterRedisTemplate.opsForValue().set(key, value); System.out.println("从节点01,保存到redis中的数据为:" + slave01RedisTemplate.opsForValue().get(key)); System.out.println("从节点02,保存到redis中的数据为:" + slave02RedisTemplate.opsForValue().get(key)); } }
哨兵
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置文件
spring: redis: sentinel: master: mymaster # 所有哨兵sentinel.conf配置文件中,自定义生命的Redis Master的名称 nodes: # 将所有哨兵的ip:port列举出来 - 8.140.224.210:26380 - 8.140.224.210:26381 - 8.140.224.210:26382 password: xiaoyuan # 如果 Redis 集群设置了密码
-
创建RedisTemplate Bean
在哨兵模式下,spring-data-redis默认使用
Lettuce
,通过LettuceConnectionFactory
来创建连接。默认已经创建了RedisTemplate Bean,便无需手动创建。 -
使用 RedisTemplate
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; public void saveData(String key, String value) { redisTemplate.opsForValue().set(key, value); System.out.println("保存到redis中的数据为:" + redisTemplate.opsForValue().get(key)); } }
集群
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- jedis、lettuce 选择一个 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
-
配置文件
在
application.yml
文件中配置 Redis 集群的信息。spring: redis: cluster: nodes: # 将所有集群的ip:port列举出来 - 8.140.224.210:6001 - 8.140.224.210:6002 - 8.140.224.210:6003 password: xiaoyuan # 如果 Redis 集群设置了密码
-
创建RedisTemplate Bean
在集群模式下,通常需要使用
Jedis
或Lettuce
,但通常是通过JedisConnectionFactory
或LettuceConnectionFactory
来创建连接。以下是使用Jedis
的配置示例:import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Value("${spring.redis.cluster.nodes}") private String clusterNodes; @Value("${spring.redis.password}") private String password; @Bean public JedisConnectionFactory redisConnectionFactory() { RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration() .clusterNode(clusterNodes.split(",")) .setPassword(password); // 设置密码,如果有的话 return new JedisConnectionFactory(clusterConfig); } @Bean public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); return redisTemplate; } }
-
使用 RedisTemplate
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; public void saveData(String key, String value) { redisTemplate.opsForValue().set(key, value); System.out.println("保存到redis中的数据为:" + redisTemplate.opsForValue().get(key)); } }