使用 SMO 收集读取路由信息在 SQL Server 2016 中不准确

Nic*_*Nic 7 sql-server metadata smo availability-groups sql-server-2016

我最近一直在使用 SMO 来检查一些属性,并且在尝试收集有关 SQL Server 2016 中的读取路由顺序的信息时遇到了一个问题。

在早期版本中,路由列表非常简单,列表将按顺序处理,但在 2016 年,他们引入了循环算法,允许您让多个辅助副本接受读取流量。

虽然此数据可通过 T-SQL 访问,但 SMO 似乎并未更新以反映这一点,这意味着您无法准确获取或设置配置,因为它仍然是一个简单的字符串集合。

是否有一些我遗漏的属性可以让我获得 SQL Server 2016 的准确数据?(下面的 C# 示例 SMO 调用)

它只是似乎无法访问的循环信息,它列出了读取路由列表中的所有副本,它似乎没有区分可能存在的各种副本组。

连接错误报告。 连接已死...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace SmoTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the servername");
            string connectServer = Console.ReadLine();
            Console.WriteLine("Enter the AG name");
            string agName = Console.ReadLine();

            Server srv = new Server();

            try
            {
                srv = new Server(connectServer);
                srv.ConnectionContext.StatementTimeout = 60; //timeout after 60 seconds running the query

                foreach (AvailabilityGroup ag in srv.AvailabilityGroups)
                {
                    if (ag.Name == agName)
                    {
                        ag.PrimaryReplicaServerName.ToString());
                        foreach (AvailabilityReplica ar in ag.AvailabilityReplicas)
                        {
                            if (ar.Name.ToString() == "connectServer")
                            {
                                foreach (Object obj in ar.ReadonlyRoutingList)
                                {
                                    Console.WriteLine("  {0}", obj);
                                }
                            }
                        }
                    } 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.ToString());
            }
            finally
            {
                srv.ConnectionContext.Disconnect();
            }

            Console.WriteLine("press a key");
            Console.Read();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sea*_*ser 6

该问题已在 SSMS 17.3 中修复,但是您需要加载“applocal”程序集,而不是来自 GAC 的程序集。此外,为了“向后兼容”,该ReadOnlyRoutingList属性将保持字符串并像以前一样行为/行为。在添加的LoadBalancedReadOnlyRoutingList(以及它的getter / setter)将有你正在寻找正确的表示。

连接已死...

连接万岁!

代码复制(为了可读性而切开)查看新的属性和方法:

#Load the Applocal assembly from SSMS 17.4
[System.Reflection.Assembly]::LoadFrom('C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Microsoft.SqlServer.SMO.dll')

#Create a new server instance hitting the listener named SQL2016Listen
$SI = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "SQL2016Listen"

#Get the list of Availability Replicas for an Availability Group called "SeedingTest"
$ARs = $SI.AvailabilityGroups | where {$_.Name -eq "SeedingTest"} | select AvailabilityReplicas

#Grab the first replica in the list of replicas and show all of the members that have "load" somewhere in the name
$ARs.AvailabilityReplicas[0] | get-member | where {$_.name -like "*load*"} | select name
Run Code Online (Sandbox Code Playgroud)