一.引入jedis的Maven配置文件
<!-- redis连接客户端jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> <type>jar</type> <scope>compile</scope> </dependency>
二.java连接redis代码
package yuelj.utils; 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 RedisUtils { private static ShardedJedisPool pool; static { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(100); config.setMaxIdle(50); config.setMaxWaitMillis(3000); config.setTestOnBorrow(true); config.setTestOnReturn(true); // 集群 JedisShardInfo jedisShardInfo1 = new JedisShardInfo("120.26.1.1", 6379); jedisShardInfo1.setPassword("123456888888"); List<JedisShardInfo> list = new LinkedList<JedisShardInfo>(); list.add(jedisShardInfo1);
pool = new ShardedJedisPool(config, list); } public static void main(String[] args) { ShardedJedis jedis = pool.getResource(); String keys = "myname"; String vaule = jedis.set(keys, "lxr"); System.out.println(vaule); } }