SpringBoot整合redis
本机环境
主机 | xin7 旗舰版64位 |
虚拟机 | CentOS Linux release 7.7.1908 (Core) |
redis | 5.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
- 在虚拟机上启动你的redis服务
- idea创建springboot项目
需要添加的依赖
- 配置properties文件
spring.redis.host=你的redis主机ip spring.redis.database=0 spring.redis.port=6379 spring.redis.password=你的redis客户端密码
- 编写测试控制
@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"); } }
- 启动你的springboot项目,查看你生成的security密码
- 浏览器输入
http://localhost:8080/
,浏览器会自动跳转到security登录页面
用户名: user
密码: 如上图,自动生成的密码 - 浏览器输入
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
命令 - 浏览器接着键入
http://localhost:8080/get
,查看结果