1.添加依赖pom.xml
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency>
2. java连接
@SuppressWarnings("resource") public static void main(String[] args) { Set<String> sentinels=new HashSet<String>(); sentinels.add("192.168.1.80:26379"); sentinels.add("192.168.1.81:26379"); sentinels.add("192.168.1.82:26379"); JedisSentinelPool pool = new JedisSentinelPool("master01", sentinels, new GenericObjectPoolConfig(), 1000); Jedis jedis = pool.getResource(); try { /// ... do stuff here ... for example jedis.set("foo", "bar"); String foobar = jedis.get("foo"); System.out.println(foobar); /* jedis.zadd("sose", 0, "car"); jedis.zadd("sose", 0, "bike"); Set<String> sose = jedis.zrange("sose", 0, -1);*/ } finally { /// ... it's time to release alive/broken Jedis instance... if (null != jedis) { jedis.close(); } } /// ... when closing your application: pool.destroy(); }
3. spring的不再赘述。