Redis集群密碼設(shè)置
在網(wǎng)上查到一種方式,修改所有Redis集群中的redis.conf文件,加入:
masterauth <yourpassword>
requirepass <yourpassword>
這種方式需要重新啟動各節(jié)點(diǎn),比較麻煩
另一種方式我們可以進(jìn)入每一個節(jié)點(diǎn),輸入如下指令:
./redis-cli -c -h bigdata24 -p 8000
config set masterauth <yourpassword>
config set requirepass <yourpassword>
config rewrite
這種方式rewrite不知道有沒有意義,因為在執(zhí)行
config set requirepass yourpassword
該指令之后運(yùn)行config rewrite
會提示錯誤
(error) NOAUTH Authentication required.
原因,沒有是沒有進(jìn)行密碼認(rèn)證沃饶,輸入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參數(shù)
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>