Redis集群密码设置
在网上查到一种方式,修改所有Redis集群中的redis.conf文件,加入:
masterauth <yourpassword>
requirepass <yourpassword>
这种方式需要重新启动各节点,比较麻烦
另一种方式我们可以进入每一个节点,输入如下指令:
./redis-cli -c -h bigdata24 -p 8000
config set masterauth <yourpassword>
config set requirepass <yourpassword>
config rewrite
这种方式rewrite不知道有没有意义,因为在执行
config set requirepass yourpassword
该指令之后运行config rewrite
会提示错误
(error) NOAUTH Authentication required.
原因,没有是没有进行密码认证,输入auth <yourpassword>
就可以啦
使用JedisCluster访问Redis集群
public class JedisClusterUtil {
private static volatile JedisCluster cluster;
private JedisClusterUtil() {}
public static JedisCluster getInstance() {
if (cluster == null) {
synchronized (JedisClusterUtil.class) {
if (cluster == null) {
Set<HostAndPort> nodes = new LinkedHashSet<>();
nodes.add(new HostAndPort("bigdata24", 8000));
nodes.add(new HostAndPort("bigdata24", 8001));
nodes.add(new HostAndPort("bigdata24", 8002));
nodes.add(new HostAndPort("bigdata24", 8003));
nodes.add(new HostAndPort("bigdata24", 8004));
nodes.add(new HostAndPort("bigdata24", 8005));
cluster = new JedisCluster(nodes,5000,3000,10,"cltest", new JedisPoolConfig());
return cluster;
}
}
}
return cluster;
}
}
JedisCluster参数
public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,int maxAttempts, String password, final GenericObjectPoolConfig poolConfig) {
super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, poolConfig);
}
Jedis pom文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>