Table of contents
First introduce the relevant dependencies: the springBoot version I use is 2.5.2
<!-- 提供Redis连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
# redis配置
redis:
# Redis数据库索引(默认为0)
database: 1
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password: root
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池最大连接数
max-active: 200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
# 连接池中的最大空闲连接
max-idle: 10
# 连接池中的最小空闲连接
min-idle: 0
Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException:
If you use the same password port as Redis Desktop Manager on the client side, you can connect normally, but an error will be reported when using the interface request after running the java program
Several problems encountered in operation:
1. Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException:
If you use the same password port as Redis Desktop Manager on the client side, you can connect normally, but you will report an error if you use the interface request after running the java program, so for The configuration file redis.conf is modified as follows:
1. 修改protected-mode配置的值为no
protected-mode is an option in the Redis configuration file to control whether Redis enables protected mode. When protected-mode is set to yes, it means that Redis is in protected mode, and the Redis service can only be accessed through the local loopback address (127.0.0.1), that is, only local clients are allowed to connect, and no external network access is allowed.
2. 注释#bind 127.0.0.1
By default, bind
it is set to empty , which means that Redis listens on all available network interfaces, including local 127.0.0.1 and external network addresses. This means that Redis can accept connections from any network interface. Whenbind
set to a specific IP address or hostname, Redis will only listen to the specified network interface, and will not listen to other network interfaces. For example, ifbind
set to127.0.0.1
, Redis will only listen to the local loopback address, that is, only local clients are allowed to connect, while connections from external networks are rejected.
3. 增加redis连接密码
Search for requirepass in the redis.conf configuration file, and you can add a password:
Then just restart redis.
After all the above configurations are completed, an error is still reported, and the content of the error is as follows:
"Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
The reason is that there is a problem with the configuration file. Our configuration file just now is:
You need to change password to auth:
redis:
database: 0
host: localhost
port: 6379
auth: abc123456
In this way, you can access normally!