实践一:配置多个host

客户端配置多个cluster host,这样做有两个好处:

  1. 避免单点问题
  2. 客户端请求负载均衡(round-robin)

方法一、

 

HFactory.getOrCreateCluster("MyCluster","host1:9160,host2:9160,host3:9160")

 

方法二、

 

HFactory.getOrCreateCluster("MyCluster","host0:9160")

cluster.addHost(new CassandraHost("host1", 9160), true);
cluster.addHost(new CassandraHost("host2", 9160), true);
cluster.addHost(new CassandraHost("host3", 9160), true);

   注意:addHost()方法第二个参数为true才会真正生效

 

--end