1、在pom.xml中配置相关的jar依赖;
<!-- 加载spring boot redis包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、在Springboot核心配置文件application.properties中配置redis连接信息:
spring.redis.host=192.168.230.128
spring.redis.port=6379
spring.redis.password=123456
3、配置了上面的步骤,Spring boot将自动配置RedisTemplate,在需要操作redis的类中注入redisTemplate;
在使用的类中注入:
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
哨兵模式redis集群配置:
spring.redis.password=123456
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.30.128:26380,192.168.30.128:26382,192.168.30.128:26384