du0204 2019-05-06 09:07
浏览 127
已采纳

不断重新连接到Cassandra

I have read online that the best practice when using Cassandra is to have 1 Cluster and 1 Session for the lifetime of your service.

My questions are:

  1. In case our Cassandra server goes down, how do I make sure that my Cluster and/or Session will keep trying to reconnect until our Cassandra server gets back online?

  2. Should only the Cluster attempt to reconnect, or only the Session, or both?

We are using Go and github.com/gocql/gocql for our service.

I have seen the following snippet in the gocql documentation, but it looks like it only has a limited number of retries:

cluster.ReconnectionPolicy = &gocql.ConstantReconnectionPolicy{MaxRetries: 10, Interval: 8 * time.Second}

I also found the below snippet online, but it doesn't look like it's designed to handle this scenario:

var cluster *gocql.ClusterConfig
var session *gocql.Session

func getCassandraSession() *gocql.Session {
    if session == nil || session.Closed() {
        if cluster == nil {
            cluster = gocql.NewCluster("127.0.0.1:9042")
            cluster.Keyspace = "demodb"
            cluster.Consistency = gocql.One
            cluster.ProtoVersion = 4
        }
        var err error
        if session, err = cluster.CreateSession(); err != nil {
            panic(err)
        }
    }
    return session
}

Are any of the above methods sufficient to ensure that reconnections are attempted until our Cassandra server gets back online? And if not, what's the best practice for this scenario?

  • 写回答

1条回答 默认 最新

  • doulu5109 2019-05-07 05:56
    关注

    Special thanks to @Jim Wartnick for this. I just tried turning Cassandra off on my local machine and then turning it back on and gocql instantly reconnected without having to use any of the above snippets in my question.

    As long as your Cluster and Session have connected to Cassandra at least once, even if Cassandra goes down, they will instantly reconnect to it as soon as Cassandra gets back online.

    Many thanks to everyone who helped!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助