springboot整合使用云服务器上的Redis方法

2022-12-08 0 1,056

目录一、前提条件二、整合步骤三、可能遇到的坑

一、前提条件

修改redis.conf配置文件

1、protected-mode yes(默认的) 修改成 protected-mode no,解除保护模式

springboot整合使用云服务器上的Redis方法

2、注释掉绑定ip ,绑定ip的话,使得除了本机(服务器)以外的主机无法访问redis数据库

springboot整合使用云服务器上的Redis方法

3、将守护进程模式关闭 daemonize yes 改成 daemonize no

springboot整合使用云服务器上的Redis方法

4、最后,一定记住要redis-server redis.conf重启redis的配置文件,否则修改不生效!!!

二、整合步骤

1、导入依赖

<!–redis–><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>

2、配置yml配置文件

spring: redis: host: 你的云服务器的ip port: 6379 # Redis的端口 password: redis密码 # Redis服务器连接密码

3、注入RedisTemplate

@Autowiredprivate RedisTemplate redisTemplate;

使用例子

@Autowired private RedisTemplate redisTemplate; @Test public void redisTestSet( ) { ValueOperations ops = redisTemplate.opsForValue(); ops.set( \”username\”, 41); System.out.println( ); } @Test public void redisTestGet( ) { ValueOperations ops = redisTemplate.opsForValue(); Object name = ops.get( \”username\”); System.out.println( name ); }

三、可能遇到的坑

报错1:

springboot整合使用云服务器上的Redis方法

解决方式1:在redis.conf配置文件中requirepass后空一一格,然后加上密码

springboot整合使用云服务器上的Redis方法

解决方式2:命令模式

1)登录redis

redis-cli

2)设置密码 (比如我设置密码为12345678)

set requirepass 12345678

3)验证 :输入默认用户名 auth 和 密码

auth 12345678

springboot整合使用云服务器上的Redis方法

错误2:

启动项目时报错:java.net.ConnectException: Connection refused: no further information

原因:开头的前提条件2可能没有弄好,或是弄了但没有和重新启动redis

到此这篇关于springboot整合使用云服务器上的Redis方法的文章就介绍到这了,更多相关springboot整合Redis内容请搜索悠久资源以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Redis数据库 springboot整合使用云服务器上的Redis方法 https://www.u-9.cn/sql/redis/6313.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    文章总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

注册会员,众多资源免费下载