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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题