java在springboot中连接redis需要以下操作。
1.测试类上需要加以下两个注解(随版本变动,很多视频教程都不加,也可以运行。)
@RunWith(SpringRunner.class) @SpringBootTest(classes = MyMain.class)
不加RunWith可能会空指针异常。
需要两个测试依赖,可能会发生冲突异常。
将spring-test依赖放在spring-boot-test依赖的前面,可以避免冲突。
2.加入以下两个依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
3.对liunx上的redis进行设置。
1. 关闭防火墙
使用 systemctl stop firewalld
对redis.conf 文件进行设置
将bind 127.0.0.1注释掉
将protected-mode yes的yes改为no
必须重启redis,不然配置文件不会生效!!!!!!!