3.API访问redis数据库、redis集群

API访问redis数据库

----------------------------------

依赖

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

  代码

import redis.clients.jedis.Jedis;

/**
* @author star
*/
public class TestRedis {
    public static void main(String[] args) {
        //Jedis(主机,端口)
        Jedis redis = new Jedis("s101", 6379);
        redis.set("key2","tom1");
        String value = redis.get("key2");
        
        System.out.println(value);
        System.out.println(redis.hgetAll("user_0"));

    }
}

API访问 redis集群

-----------------------------

代码

@Test

public void testRedisCluster() throws IOException {

    Set hosts = new HashSet();

    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();

}

评论

暂无评论