java串連redis叢集

來源:互聯網
上載者:User

標籤:lan   ati   exception   null   ace   target   bsp   模式   oid   

http://m.blog.csdn.net/koushr/article/details/50956870

串連redis資料庫(非叢集模式)

public static void main(String[] args) {    JedisPoolConfig poolConfig = new JedisPoolConfig();    // 最大串連數    poolConfig.setMaxTotal(2);    // 最大空閑數    poolConfig.setMaxIdle(2);    // 最大允許等待時間,如果超過這個時間還未擷取到串連,則會報JedisException異常:    // Could not get a resource from the pool    poolConfig.setMaxWaitMillis(1000);    JedisPool pool = new JedisPool(poolConfig, "192.168.83.128", 6379, 0, "123");    Jedis jedis = null;    try {        for (int i = 0; i < 5; i++) {            jedis = pool.getResource();            jedis.set("foo" + i, "bar" + i);            System.out.println("第" + (i + 1) + "個串連, 得到的值為" + jedis.get("foo" + i));            // 用完一定要釋放串連            jedis.close();        }    } finally {        pool.close();    }}

串連redisCluster(叢集模式)

public static void main(String[] args) {    JedisPoolConfig poolConfig = new JedisPoolConfig();    // 最大串連數    poolConfig.setMaxTotal(1);    // 最大空閑數    poolConfig.setMaxIdle(1);    // 最大允許等待時間,如果超過這個時間還未擷取到串連,則會報JedisException異常:    // Could not get a resource from the pool    poolConfig.setMaxWaitMillis(1000);    Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();    nodes.add(new HostAndPort("192.168.83.128", 6379));    nodes.add(new HostAndPort("192.168.83.128", 6380));    nodes.add(new HostAndPort("192.168.83.128", 6381));    nodes.add(new HostAndPort("192.168.83.128", 6382));    nodes.add(new HostAndPort("192.168.83.128", 6383));    nodes.add(new HostAndPort("192.168.83.128", 6384));    JedisCluster cluster = new JedisCluster(nodes, poolConfig);    String name = cluster.get("name");    System.out.println(name);    cluster.set("age", "18");    System.out.println(cluster.get("age"));    try {        cluster.close();    } catch (IOException e) {        e.printStackTrace();    }}

 

java串連redis叢集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.