命令行连接
客户端启动命令: ./redis-cli -c -h ip -p port -a password
不基于-a直接输入密码,可以通过auth输入密码: auth password
服务端启动命令: ./redis-server ../redis.conf
Jedis连接
<dependency>
<groupId> redis.clients</groupId>
<artifactId> jedis</artifactId>
<version> 2.9.0</version>
</dependency>
public void single ( ) {
Jedis jedis = new Jedis ( "10.12.52.85" , 6379 ) ;
jedis. auth ( "password" ) ;
jedis. set ( "key" , "value" ) ;
jedis. close ( ) ;
}
public void singlePool ( ) {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig ( ) ;
poolConfig. setMaxIdle ( 100 ) ;
poolConfig. setMaxTotal ( 100 ) ;
JedisPool jedispool = new JedisPool ( poolConfig, "192.168.241.133" , 6379 ) ;
Jedis jedis = jedispool. getResource ( ) ;
jedis. set ( "key" , "value" ) ;
jedis. close ( ) ;
jedispool. close ( ) ;
}
public void clusterPool ( ) {
GenericObjectPoolConfig config = new JedisPoolConfig ( ) ;
config. setMaxIdle ( 100 ) ;
config. setMaxTotal ( 100 ) ;
Set< HostAndPort> nodeList = new HashSet < > ( ) ;
nodeList. add ( new HostAndPort ( "127.0.0.1" , 7000 ) ) ;
nodeList. add ( new HostAndPort ( "127.0.0.1" , 7001 ) ) ;
nodeList. add ( new HostAndPort ( "127.0.0.1" , 7002 ) ) ;
JedisCluster jedisCluster = new JedisCluster ( nodeList, 3000 , 3000 , 100 , "password" , config) ;
jedisCluster. set ( "key" , "value" ) ;
jedisCluster. close ( ) ;
}