API访问redis数据库
----------------------------------
依赖
1<dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>2.9.0</version> 5 </dependency> 6 <dependency> 7 <groupId>junit</groupId> 8 <artifactId>junit</artifactId> 9 <version>4.11</version> 10 </dependency>
代码
1import redis.clients.jedis.Jedis; 2 3/** 4* @author star 5*/ 6public class TestRedis { 7 public static void main(String[] args) { 8 //Jedis(主机,端口) 9 Jedis redis = new Jedis("s101", 6379); 10 redis.set("key2","tom1"); 11 String value = redis.get("key2"); 12 13 System.out.println(value); 14 System.out.println(redis.hgetAll("user_0")); 15 16 } 17}
API访问 redis集群
-----------------------------
代码
@Test
public void testRedisCluster() throws IOException {
Set<HostAndPort> hosts = new HashSet<HostAndPort>();
hosts.add(new HostAndPort("192.168.116.101", 7000));
hosts.add(new HostAndPort("192.168.116.101", 7001));
hosts.add(new HostAndPort("192.168.116.101", 7002));
hosts.add(new HostAndPort("192.168.116.101", 7003));
hosts.add(new HostAndPort("192.168.116.101", 7004));
hosts.add(new HostAndPort("192.168.116.101", 7005));
BinaryJedisCluster cluster = new BinaryJedisCluster(hosts);
byte[] value = cluster.get("key1".getBytes());
System.out.println(new String(value));
cluster.close();
}