记录springboot整合redis及linux安装配置redis的一些坑

记录springboot整合redis及linux安装配置redis的一些坑

springboot整合redis

在SpringBoot中一般使用RedisTemplate提供的方法来操作Redis。
一开始我是在pom文件中引入了下面的依赖:

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

然后在application.properties文件中对redis进行了一些必要的配置

#redis服务器ip地址
spring.redis.host=175.24.41.93
#端口号
spring.redis.port=6379
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=200 
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=1000
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=20
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=5

自己写了个Controller的demo

@Controller
public class redeisDemo {
    /**
     * 注入响应式的ReactiveRedisTemplate
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/getval", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public void getredis() {
        redisTemplate.opsForValue().set("aaa","bbb");
       String name= redisTemplate.opsForValue().get("zxt");
       System.out.println("name="+name);
    }
}

坑点一:
启动java程序浏览器输入http://localhost:8080/getval 报错

Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConf

发现是未引入redis的数据库连接池引起的
解决办法:

<!--redis数据库连接池-->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>

坑点二
启动项目浏览器再次输入http://localhost:8080/getval 依然报错

Connection refused: no further information

顿时一脸懵逼,网上一查,发现是我的linux服务器的防火墙没关,一般默认是关闭的,我查看了一下我的防火墙状态是打开的,
解决办法:
手动关闭防火墙 (/手动狗头~~~)
坑点三
再次启动项目浏览器再次输入http://localhost:8080/getval 又双叕报错!!!我心态崩了啊┭┮﹏┭┮

redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because
protected mode is enabled, no bind address was specified, no authentication password is requested to
clients. In this mode connections are only accepted from the loopback interface. If you want to 
connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable
protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by 
connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly 
accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively 
you can just disable the protected mode by editing the Redis configuration file, and setting the protected 
mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, 
restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password.
NOTE: You only need to do one of the above things in order for the server to start accepting connections 
from the outside.

仔细一看,原来是redis的保护模式开着,拒绝的我的访问,好在这里还给出了解决办法,因为我使用的是redis默认的配置文件启动,所以保护模式是开着的。
解决办法:
redis.conf里把protected-mode yes 改为protected-mode no
重启redis服务
启动项目 浏览器再次输入http://localhost:8080/getval
看后台输出
在这里插入图片描述
哈哈,终于打通了,,,长舒一口气。

linux安装启动redis

这里顺便记录一下linux上如何安装redis以及我遇到的一些坑:
首先百度redis,跳到redis官网,下载redis安装包,这里我推荐下载redis-5.0.8,因为比较稳定,
现redis的最新的版本为6.0.4。
把下载好的redis-5.0.8.tar.gz复制到你的linux服务器上
执行解压命令 tar -zxvf redis-5.0.8.tar.gz 得到解压完的redis-5.0.8的文件夹
cd redis-5.0.8 进入目录
坑点
现在如果执行make命令会报错,因为redis是由ANSI C语言写的,我的linux服务器缺少运行环境,所以报错了。
解决办法:
先查看是否安装c++的编译器; 执行 yum -y install gcc-c++

再次执行make命令,安装成功!
执行redis-server 启动redis服务,如果出现下面这个界面,说明成功启动
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值