目录
-
pom.xml导入redis依赖
<!-- redis 操作依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
-
配置下application.yaml文件
spring:
redis:
host: 127.0.0.1 # Redis服务器地址
database: 0 # Redis数据库索引(默认为0)
port: 6379 # Redis服务器连接端口
password: 123456 # Redis服务器连接密码(默认为空)
timeout: 300ms # 连接超时时间(毫秒)
poolMaxTotal: 10 #连接池中的最大连接数
poolMaxIdleL: 10 # 连接池中的最大空闲连接
poolMaxWait: 3 # 连接池最大阻塞等待时间(使用负值表示没有限制)
-
密码设置
不设密码在后面的程序会抛个没有密码的异常,导致失败
打开客户端
redis 127.0.0.1:6379> config set requirepass “你的密码”
验证密码
redis 127.0.0.1:6379> auth “你的密码”
Windows环境启动redis服务命令:在redis目录下执行:
redis-server.exe redis.windows.conf
-
测试类测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void test(){
System.out.println(stringRedisTemplate.getClientList());
stringRedisTemplate.opsForValue().set("aaa", "122");
}
}