【问题标题】:Advise/Code samples on how to make an application "Cluster Aware" [closed]关于如何使应用程序“集群感知”的建议/代码示例 [关闭]
【发布时间】:2011-01-20 00:07:31
【问题描述】:

我在网上浏览了有关如何使用Failover Cluster API 制作应用程序/Web 应用程序“Cluster Aware”的资源。我发现了很多技术文章,但没有一个是从程序员的角度写的。有人有什么好的链接,或者可以为我提供代码示例或其他一些关于如何从程序员的角度制作应用程序集群感知的输入吗?我们使用 C# 作为我们的主要编程语言。

集群是一个主动/被动集群,包含两个运行 IIS 的节点(Windows 2003 Server)。

因为我还没有找到任何东西,所以我怀疑我遗漏了一些东西!

Br

奥斯加尔

【问题讨论】:

  • 集群感知是什么意思?你试图用这种意识做什么?

标签: c# cluster-computing failover


【解决方案1】:

你有什么运气吗?

我正在寻找相同的信息?

在应用程序的数据库驻留在 SQL 集群上的场景中。当集群执行故障转移时,SQL 连接池变得无效和损坏。连接池需要被刷新并重新创建,而不会出现异常。

从代码的角度来看,你需要先。

  1. 向连接池提交 SQL 查询。
  2. 捕获使用无效 SQL 连接池的异常。
  3. 刷新连接池或遍历所有连接,直到连接池没有可用的连接。
  4. 重新创建一个新的连接池。
  5. 重新提交 SQL 查询。

我的问题是我是一名基础架构架构师,而且我的编码技能在最好的时候都很薄弱。

【讨论】:

    【解决方案2】:

    一直在玩,在同事的帮助下想出了以下例子。

    static void Main(string[] args)
        {
            Boolean PrevSqlError = false;
            Boolean NewSqlPool = false;
            String ConStr = "Data Source=SQL-CLUSTER1;Initial Catalog=Example;Integrated Security=True;Connection Timeout=60;Min pool size=5";
            Console.WriteLine("Press any key to read from database");
            Console.ReadKey();
            while (true)
            {
                try
                {
                    Console.WriteLine("Attempting to connect");
    
                    using (var context1 = new ExampleDataContext())
                    {
                        var customers1 = context1.Customers.ToList();
                        var connection1 = new SqlConnection(ConStr);
                        connection1.Open();
                        PrintCustomers(customers1);
                        connection1.Close();
                    }
                    PrevSqlError = false;
                    NewSqlPool = false;
                    Console.WriteLine("Sleeping 3s");
                    Thread.Sleep(3000);
                }
                catch (SqlException sqlException)
                {
                    var SqlError = sqlException.Number;
                    Console.WriteLine("Error connecting to SQL : " + SqlError+" : "+sqlException.Message);
                    if (NewSqlPool == true)
                    {
                        Console.WriteLine("Error in New Connection Pool. Exiting!");
                        Thread.Sleep(10000);
                        return;
                    }
                    if (PrevSqlError == true)
                    {
                        if (SqlError == 10054 || SqlError == 232 || SqlError == 233 || SqlError == 64 || SqlError == 4060)
                        {
                            Console.WriteLine("SQL Cluster Failing Over. Waiting 5s");
                            SqlConnection.ClearAllPools();
                            PrevSqlError = false;
                            NewSqlPool = true;
                            Thread.Sleep(5000);
                        }
                        else
                        {
                            Console.WriteLine("Fatal SQL Exception. Exiting!");
                            Thread.Sleep(10000);
                            return;
                        }
                    }
                    else
                    {
                    Console.WriteLine("SQL Error, Retrying in 3s");
                    PrevSqlError = true;
                    Thread.Sleep(3000);
                    }
                }
            }
        }
    
        private static void PrintCustomers(List<Customer> customers)
        {
            foreach (var item in customers)
            {
                Console.WriteLine(string.Format("{0} - {1}", item.Id, item.Name));
            }
        }
    

    【讨论】:

    • 这只是展示如何连接到集群数据库。我认为这没什么大不了的。这很简单。
    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 2013-09-16
    • 2022-12-22
    • 1970-01-01
    • 2021-06-03
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多