1. 程式人生 > >redis負載均衡和高併發

redis負載均衡和高併發

public static void main(String[] args) {
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
        shards.add(new JedisShardInfo("127.0.0.1", 6379));
        shards.add(new JedisShardInfo("127.0.0.1", 6380));

        ShardedJedisPool sjp = new ShardedJedisPool(new JedisPoolConfig(), shards);
        ShardedJedis shardClient = sjp.getResource();
        try {
            shardClient.set("A", "123");
            shardClient.set("B", "234");
            shardClient.set("C", "345");

            try {
                System.out.println(shardClient.get("A"));
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                System.out.println(shardClient.get("B"));
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                System.out.println(shardClient.get("C"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sjp.returnResource(shardClient);
        }
    }

}