2016-11-24 147 views
1

我正在使用datastax java驅動程序3.1.0連接到cassandra羣集,我的cassandra羣集版本是2.0.10。使用datastax java驅動程序連接到本地cassandra節點?

下面是我用來連接cassandra集羣的單例類。

public class CassUtil { 
    private static final Logger LOGGER = Logger.getInstance(CassUtil.class); 

    private Session session; 
    private Cluster cluster; 

    private static class Holder { 
    private static final CassUtil INSTANCE = new CassUtil(); 
    } 

    public static CassUtil getInstance() { 
    return Holder.INSTANCE; 
    } 

    private CassUtil() { 
    List<String> servers = TestUtils.HOSTNAMES; 
    String username = 
     TestUtils.loadCredentialFile().getProperty(TestUtils.USERNAME); 
    String password = 
     TestUtils.loadCredentialFile().getProperty(TestUtils.PASSWORD); 

    // is this right setting? 
    PoolingOptions poolingOptions = new PoolingOptions(); 
    poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 4, 10).setConnectionsPerHost(
     HostDistance.REMOTE, 2, 4); 

    Builder builder = Cluster.builder(); 
    cluster = 
     builder 
      .addContactPoints(servers.toArray(new String[servers.size()])) 
      .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) 
      .withPoolingOptions(poolingOptions) 
      .withReconnectionPolicy(new ConstantReconnectionPolicy(100L)) 
      .withLoadBalancingPolicy(
       DCAwareRoundRobinPolicy 
        .builder() 
        .withLocalDc(
         !TestUtils.isProduction() ? "DC2" : TestUtils.getCurrentLocation() 
          .get().name().toLowerCase()).build()) 
      .withCredentials(username, password).build(); 

    try { 
     session = cluster.connect("testkeyspace"); 
     StringBuilder sb = new StringBuilder(); 
     Set<Host> allHosts = cluster.getMetadata().getAllHosts(); 
     for (Host host : allHosts) { 
     sb.append("["); 
     sb.append(host.getDatacenter()); 
     sb.append(host.getRack()); 
     sb.append(host.getAddress()); 
     sb.append("]"); 
     } 
     LOGGER.logInfo("connected: " + sb.toString()); 
    } catch (NoHostAvailableException ex) { 
     LOGGER.logError("error= ", ExceptionUtils.getStackTrace(ex)); 
    } catch (Exception ex) { 
     LOGGER.logError("error= " + ExceptionUtils.getStackTrace(ex)); 
    } 
    } 

    public void shutdown() { 
    LOGGER.logInfo("Shutting down the whole cassandra cluster"); 
    if (null != session) { 
     session.close(); 
    } 
    if (null != cluster) { 
     cluster.close(); 
    } 
    } 

    public Session getSession() { 
    if (session == null) { 
     throw new IllegalStateException("No connection initialized"); 
    } 
    return session; 
    } 

    public Cluster getCluster() { 
    return cluster; 
    } 
} 

什麼是我需要使用連接到本地節點卡桑德拉第一,如果他們都放下,然後只談論到遠程節點的設置。另外我的池配置選項就在這裏,我在上面的代碼中使用?

回答

1

默認情況下,datastax驅動程序將只連接到本地DC中的節點。如果您不使用withLocalDc,它將嘗試從它能夠連接的聯繫點的DC中辨別本地數據中心。

如果你想在驅動程序無法在遠程數據中心(S)主持,你應該使用withUsedHostsPerRemoteDc,即:

cluster.builder()   
    .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder() 
    .withLocalDc("DC1") 
    .withUsedHostsPerRemoteDc(3).build()) 

利用這種結構,驅動程序將建立在每個遠程3個主機的連接DC,並且只在本地數據中心的所有主機關閉時才向它們發送查詢。

還有其他策略可用於故障轉移到遠程數據中心。例如,您可以在與C *數據中心相同的物理數據中心中運行應用程序客戶端,然後當物理數據中心發生故障時,可以在更高級別(例如負載均衡器)進行故障轉移。

另外我的池配置選項就在這裏,我在上面的代碼中使用?

我認爲你有什麼是好的。默認值也很好。

+0

假設我們在每個數據中心沒有3個節點,那麼如果我們在那裏指定'3',會發生什麼?或者它必須是數據中心中的機器數量? – john

+0

3是一個天花板,所以如果你只有2個節點在遠程DC,它將連接到2. –

+0

我有一個與datastax java驅動程序相關的更多問題[here](http://stackoverflow.com/questions/41757029 /如何對綁定值到綁定的語句-IN-A-通用路 - 使用 - datastax-java的驅動器)。想看看你能幫我嗎? – john

相關問題