开发者

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

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

一、前提条件

修改Redis.conf配置文件

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

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

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

 

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

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

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

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

二、整合步骤

1、导入依赖

<!--r编程edis-->
<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

@Autowired
private RedisTemplate redisTemplate;

使用例子

    @Autowired
    private RedisTemplate redisTemplate;
 
    @Test
    public void redisTestSet(  ) {
 
        Value开发者_mssql2008Operations 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:android命令模式

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新数据库

数据库排行榜