为了远程连接阿里云的redis把我折腾了一晚上,网上找了许多教程都不行,后来在一篇博客看到SpringBoot连接有高低版本之分。
我的springboot是2.2.4版本的,redis也是目前的最新版,为了不枉费我一晚上的折腾,就记录一下连接步骤(CentOS7.3)
1.ps aux | grep redis-server
在终端输入,查看redis状态,没有修改redis.conf文件的最后面将是127.0.0.1:6379
也就是说仅支持本地访问。这时候我们就要去修改配置文件里的一些配置了。
2.为了快速且避免出错,我用宝塔面板修改的redis.conf配置文件,配置文件根据上图,可以看出在>
根目录/www/server/redis/redis.conf
protected-mode no 关闭保护
port 6379 端口默认6379
requirepass *** 设置密码***(在507行)
# bind 127.0.0.1 注释掉绑定ip(所有ip可访问,69行)
还有一个守护进程daemonize,我这里默认为yes
修改完毕,保存.重启redis服务
3.在idea中我springboot版本为2.2.4
引入依赖
<!--引入redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
application.properties中设置如下,可别少了
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=47.94.108.112
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=redisPassword
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
4.完毕测试
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void getRedis(){
stringRedisTemplate.opsForValue().set("msg:","world!");
}
5.未报错,再用本地客户端连接服务器的redis,看出已经存入数据