【问题标题】:Cassandra cluster manager and endpoint exceptionCassandra 集群管理器和端点异常
【发布时间】:2020-08-13 16:49:04
【问题描述】:

我在一个解决方案中有 4 个依赖的工作 Windows 服务,并使用 cassandrasharp 3.1.4 和 cassandra 2.0.6。

在第一个中,我使用以下命令初始化 clusterManager; (此代码仅适用于第一个服务,当我尝试在每个服务中配置 clusterManager 时,这些服务无法启动。

CassandraSharp.Config.XmlConfigurator.Configure();

这是我的 app.config;

<configSections>
    <section name="CassandraSharp" type="CassandraSharp.SectionHandler, CassandraSharp.Interfaces" />
  </configSections>

  <CassandraSharp>
    <Cluster name="main">
      <Endpoints>
        <Server>kml-vm-cas-001.cloudapp.net</Server>
      </Endpoints>
    </Cluster>
  </CassandraSharp> 

和每个服务的 OnStop;

ClusterManager.Shutdown();

过程很简单,这些服务中的每一个都从不同的直播流中读取字符串,反序列化并推送到 cassandra。

      string query = null;
      ICqlCommand pocoCommand = null;
      Task task = null;
      using (ICluster iCluster = ClusterManager.GetCluster("main"))
      {
        query = string.Format("insert into Tvr.Zools (Part, Name, Ticks) values ('zools', '{0}', {1}) using ttl 86400;",
                  this.zools[i].Name,
                  dateTime.Ticks,
                );
        pocoCommand = iCluster.CreatePocoCommand();
        task = pocoCommand.Execute(query).AsFuture();
        task.Wait();

        query = string.Format("insert into Tvr.Temps (Part, Name, Ticks) values ('zools', '{0}', {1}) using ttl 10800;", this.zools[i].Name, dateTime.Ticks);
        pocoCommand = iCluster.CreatePocoCommand();
        task = pocoCommand.Execute(query).AsFuture();
        task.Wait();
      }

这适用于小流,但是当这些服务开始捕获大量流时,我得到了这些异常;

System.ArgumentException: Can't find any valid endpoint

对于某些服务;

System.InvalidOperationException: ClusterManager is not initialized

我在每个服务中都尝试过,但没有成功;

CassandraSharp.Config.XmlConfigurator.Configure();

//push process..

ClusterManager.Shutdown();

很抱歉有任何遗漏的信息,如果有的话我会编辑。

已经谢谢了。

【问题讨论】:

    标签: cassandra cassandra-sharp


    【解决方案1】:

    CassandraSharp 为您处理所有连接管理和池化。您所要做的就是每个应用程序运行一次CassandraSharp.Config.XmlConfigurator.Configure();。 CassandraSharp 会在您需要时为您打开一个连接。您应该只在应用程序关闭时调用ClusterManager.Shutdown();。这是一个sn-p from the author of CassandraSharp

    Configure() 必须被调用一次,不能再调用一次。如果你想关闭 关闭所有集群连接(假设您的进程正在退出), 然后调用 Shutdown()。

    Shutdown() 正在关闭所有连接和服务 整个过程 - 它可能会对您的其他部分产生不良影响 应用程序,如果您要连接到同一个集群中的多个集群 过程。

    即使您连接到多个集群,您也应该在 XML 配置中定义它们,并且仍然只调用一次 Configure()。所以这是一个示例设置。

    示例 XML 配置

    <CassandraSharp>
    <Cluster name="Dev">
      <Transport cl="ONE" cqlver="3.1.1" />
      <Endpoints>
        <Server>server1.test.com</Server>
      </Endpoints>
    </Cluster>
    <Cluster name="Prod">
      <Transport cl="ONE" cqlver="3.1.1" />
      <Endpoints>
        <Server>server1.test.com</Server>
        <Server>server2.test.com</Server>
      </Endpoints>
    </Cluster>
    </CassandraSharp>
    

    示例代码

    注意static cluster。这只会在应用程序中存在一次,因此您不会在创建多个实例时遇到问题。

    public interface IClusterFactory
    {
        ICluster GetCluster();
    }
    
    public class ClusterFactory : IClusterFactory
    {
        private static ICluster cluster;
    
        public ICluster GetCluster()
        {
            if (cluster != null)
            {
                return cluster;
            }
            else
            {
                XmlConfigurator.Configure();
                cluster = ClusterManager.GetCluster("Prod");
                return cluster;
            }
        }
    }
    

    然后,只要您想连接到 Cassandra:

    IClusterFactory factory = new ClusterFactory();
    ICluster cluster = factory.GetCluster();
    IPropertyBagCommand cmd = cluster.CreatePropertyBagCommand();
    

    不用担心处置或关闭集群,因为这在 CassandraSharp 中处理。只需在应用程序关闭时执行ClusterManager.Shutdown()

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2013-02-24
      • 2018-12-25
      • 2018-01-08
      • 2017-06-29
      • 1970-01-01
      • 2016-08-14
      • 2012-05-02
      相关资源
      最近更新 更多