你可以到 https://mvnrepository.com/ 网站去搜索jedis的maven依赖,如下:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
使用 JedisShardInfo 的 setPassword() 方法设置密码,具体实例如下:
package com.huangx; import java.util.LinkedList; import java.util.List; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; public class RedisTest { private static ShardedJedisPool pool; static { // 配置Redis信息 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(100); config.setMaxIdle(50); config.setMaxWaitMillis(3000); config.setTestOnBorrow(true); config.setTestOnReturn(true); // 集群 JedisShardInfo jedisShardInfo1 = new JedisShardInfo("127.0.0.1", 6379); // 设置Redis的密码 jedisShardInfo1.setPassword("helloWorld"); List<JedisShardInfo> list = new LinkedList<JedisShardInfo>(); list.add(jedisShardInfo1); pool = new ShardedJedisPool(config, list); } public static void main(String[] args) { ShardedJedis jedis = pool.getResource(); // 插入key-value String keys = "hello"; String vaule = jedis.set(keys, "hello redis"); System.out.println(vaule); } }