SpringBoot整合redis


本机环境

主机xin7 旗舰版64位
虚拟机CentOS Linux release 7.7.1908 (Core)
redis5.0.7
工具idea, xshell

redis安装

我的redis安装在虚拟机CentOS上,具体的安装方法百度即可,此处略过。。。

redis 配置bind

################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这段翻译过来的意思就是:

默认情况下,如果没有指定bind配置指令,则 Redis 监听来自服务器上所有可用网络接口的连接。可以使用bind配置指令来监听一个或多个选定的接口,在bind后拼接一个或多个 IP 地址即可。例如:

bind 192.168.1.100 10.0.0.1
bind 127.0.0.1 ::1

警告: 如果运行 Redis 的计算机直接暴露在互联网上,绑定到所有的接口是很危险的,并会将实例暴露给互联网上的每个人。因此,默认情况下,我们取消注释以下绑定指令,这将强制 Redis 只监听 IPv4 回环接口地址(这意味着 Redis 只接受来自运行它的计算机上的客户端的连接)。

如果你确定希望你的实例能够监听所有的接口,只需要注释下面的这一行即可。

bind 127.0.0.1 ::1

redis 部署在本机

执行 ifconfig 中获取到本机ip (或者 hostname -I )
redis.conf 配置为 bind 127.0.0.1 本机ip

redis 部署在docker

执行 ifconfig 中获取到本机ip (或者 hostname -I )
redis.conf 配置为 bind 127.0.0.1 本机ip

参考文章 : Redis配置文件中bind参数

redis 配置密码

修改 redis.conf 中的 requirepass, 注意去掉前面的注释符号#

spring boot 2.2.2 整合redis

  1. 在虚拟机上启动你的redis服务
  2. idea创建springboot项目
    需要添加的依赖
    在这里插入图片描述
  3. 配置properties文件
    spring.redis.host=你的redis主机ip
    spring.redis.database=0
    spring.redis.port=6379
    spring.redis.password=你的redis客户端密码
    
  4. 编写测试控制
    @RestController
    public class HelloController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    
    @GetMapping("/set")
    public void set(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("name", "jacob");
    	}
    
    @GetMapping("/get")
    public String get(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        return ops.get("name");
    	}
    }
    
  5. 启动你的springboot项目,查看你生成的security密码
    在这里插入图片描述
  6. 浏览器输入 http://localhost:8080/ ,浏览器会自动跳转到security登录页面
    用户名: user
    密码: 如上图,自动生成的密码
  7. 浏览器输入 http://localhost:8080/set ,这时浏览器空白,你的redis里面已经有这条数据了,我用xshell连上查看结果如下:
    在这里插入图片描述
    在这里插入图片描述

    我在这里报了个错

    io.lettuce.core.RedisCommandExecutionException: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk.
    大致意思: (错误)misconf redis被配置以保存数据库快照,但misconf redis目前不能在硬盘上持久化。用来修改数据集合的命令不能用,请使用日志的错误详细信息。
    解决方案
    连接redis后运行 config set stop-writes-on-bgsave-error no 命令

  8. 浏览器接着键入 http://localhost:8080/get ,查看结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值