SpringBoot连接远程服务器redis
1、指定redis配置启动
-  
进入redis安装地址,我这里安装的是 /usr/local/src/redis-6.2.6
 -  
先copy一份配置文件
-  
cp redis.conf redis.conf.bck 
 -  
 -  
然后修改配置文件信息
-  
vim redis.conf
 -  
bind 0.0.0.0 # 守护进程,修改为yes后即可后台运行 daemonize yes # 密码,设置后访问Redis必须输入密码 requirepass 123321 # 监听的端口 port 6379 # 日志文件,默认为空,不记录日志,可以指定日志文件名 logfile "redis.log" 
 -  
 -  
启动redis
-  
# 进入redis安装目录 cd /usr/local/src/redis-6.2.6 # 启动 redis-server redis.conf 
 -  
 -  
去云服务器安全组中开放端口
-  
firewall-cmd --add-port=6379/tcp --permanent 
 -  
 -  
重启防火墙
-  
firewall-cmd --reload 
 -  
 -  
查看6379的防火墙状态
-  
firewall-cmd --zone=public --query-port=6379/tcp 
 -  
 
2、导入依赖
-  
1、Spring-redis依赖
-  
<!-- redis依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- pool的依赖--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> 
 -  
 -  
2、配置redis的信息
-  
spring: # redis 配置 redis: # 地址 host: xxxx.xxxx.xxxx # 端口,默认为6379 port: 6379 # 密码,密码用双引号括起来,血与泪的排查(重置服务器的代价) password: "xxxx" # 连接超时时 timeout: 5200 lettuce: pool: # 连接池中的最小空闲连接 min-idle: 0 # 连接池中的最大空闲连接 max-idle: 8 # 连接池的最大数据库连接数 max-active: 8 # #连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1 
 -  
 -  
3、运行
-  
@SpringBootTest class SpringbootRedisDemoApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void contextLoads() { redisTemplate.opsForValue().set("name","lmg"); System.out.println(redisTemplate.opsForValue().get("name")); } } 
 -  
 




















