引入Maven依赖:
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
SpringBoot配置绑定
Spring封装的组件,比较完善的支持,我们直接在下新增关于集群的配置:
spring:
redis:
host: 192.168.3.101
port: 6379
password: 123456
调用测试:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RedisTest {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public List<RedisClientInfo> getClientList(String param) {
return redisTemplate.getClientList();
}
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
public void put(String k, String v) {
redisTemplate.opsForValue().set(k, v);
}
}