Redis3.2.1不设置密码影响JedisPool获取链接
-
和同事整合功能时发现,代码一致但是程序在我本地环境无法运行
-
后来发现他的redis设置了密码,而我的默认没设置 疑惑:难道密码不设置会影响功能,抱着试试看的心理,设置了密码,发现能用了!
-
整理一下这个坑。
1.问题场景:idea 2020.1、redis 3.2.1、操作系统:window(linux也同样要设置密码,否则设置)
package com.enjoylife.system.config;
import com.beust.jcommander.internal.Lists;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import org.springframework.data.redis.core.*;
import java.util.List;
@CommonsLog
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis;
//--- 以上配置文件内容没啥说的
@Autowired(required = false)
private RedisTemplate<String, Object> redisTemplate;
//注入Bean
@Bean
public JedisPool redisPoolFactory() {
log.info("JedisPool注入成功!!");
log.info("redis地址:" + host + ":" + port);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password);
return jedisPool;
}
}
//某个控制器某个方法中
public class MyController {
public void getSetValue(String value) {
//获取jedis对象
Jedis jedis = redisManager.redisPoolFactory().getResource();
//存入、取出需要用的数据
jedis.set("key",value);
String s = jedis.get("key");
}
}
使用场景非常简单,就是的单纯的存取数据
2.解决办法:设置redis密码 【window下】
- 打开redis安装目录,找到redis.windows.conf、redis.windows-service.conf两个配置文件(可以都修改,也可以只修改第一个文件,主要看自己启动时用哪个文件)
- 打开文件后,添加
# requirepass foobared
requirepass 你的密码
- 添加完文件后重启redis,启动命令 : redis-server.exe redis.windows.conf
OK!