SpringBoot整合redis
首先准备redis环境,这里采用阿里云服务器中的redis
- 安装redis以及做相应的配置
redis采用宝塔安装,安装好修改redis.conf文件,将1注掉以便外网访问,将2改为no
在阿里云控制台设置安全组,打开6379
为redis设置密码
如果未设置密码,则2)应为“”,否则使用config set requirepass 你的密码 , 设置你的密码。
- 测试连接
做好以上配置则可以进行连接测试
这样就是已经能够访问成功了。 - Springboot整合Redis
1.导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在yaml中配置连接
3.测试连接
@Autowired
StringRedisTemplate redisTemplate;
@Test
void testRedis(){
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.set("hello","world");
String hello = operations.get("hello");
System.out.println(hello);
System.out.println(redisConnectionFactory.getClass());
}