Alwayson--配置只读请求

时间:2024-11-26 16:04:55

场景:有三台服务器SQLNode131,SQLNode132 ,SQLNode133:

1) 三台服务器加入域DCDemo.com

2) 三台服务器使用端口访问数据库

3) 三台服务器构建故障转移群集SQLNode02

4) 三台服务器构建高可用性组AGNode134

5) SQLNode12和SQLNode22实现自动故障转移

目标:实现对SQLNode12和SQLNode22的只读请求转移

SQLNode131的IP:192.168.25.131

SQLNode132的IP:192.168.25.132

AGNode134的IP:192.168.25.134

只读路由配置

--=====================
--只读路由配置
USE master
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode131' WITH
(
SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY)
);
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode131' WITH
(
SECONDARY_ROLE (READ_ONLY_ROUTING_URL = N'TCP://192.168.25.131:1433')
);
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode132' WITH
(
SECONDARY_ROLE (ALLOW_CONNECTIONS = READ_ONLY)
);
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode132' WITH
(
SECONDARY_ROLE (READ_ONLY_ROUTING_URL = N'TCP://192.168.25.132:1433')
);
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode131' WITH
(
PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=('SQLNode132','SQLNode131'))
);
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode132' WITH
(
PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=('SQLNode131','SQLNode132'))
); GO

只读路由验证:

SELECT
g.name AS AGName,
ar1.replica_server_name AS ReplicaServerName,
ar2.replica_server_name AS RountingReplicaServerName,
ar1.endpoint_url AS ReplicaServerURL,
ar2.endpoint_url AS RountingReplicaServerURL
FROM sys.availability_read_only_routing_lists rl
INNER JOIN sys.availability_replicas ar1
ON ar1.replica_id=rl.replica_id
INNER JOIN sys.availability_replicas ar2
ON ar2.replica_id=rl.read_only_replica_id
INNER JOIN sys.availability_groups g
ON ar1.group_id=g.group_id
WHERE rl.routing_priority=1

删除只读路由

ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode132' WITH
(
PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=NONE)
);
GO
ALTER AVAILABILITY GROUP [AGNode134]
MODIFY REPLICA ON N'SQLNode131' WITH
(
PRIMARY_ROLE (READ_ONLY_ROUTING_LIST=NONE)
);
GO

使用:

MSDN指明可使用.NET Framework4.0.2来访问,但是.NET 4正式发布版本为.0.3,经过测试,该功能在.NET4.5 S上可正常运行,连接串为

Server=tcp:192.168.25.123,1433;Database=DB1;ApplicationIntent=ReadOnly;MultiSubnetFailover=True;User Id=sa;Password=password;

ApplicationIntent 有两种配置值:Readonly 和ReadWrite,ApplicationIntent并不限制请求的操作,只是表明该连接上应该发生的操作。

对于配置了只读路由的请求,每次请求会首先转发到主副本,通过主副本获取到可用的副本,依次遍历找到一个可以处理只读请求的辅助副本,并将该请求转发给该辅助副本。

在辅助副本上执行查询时,Alwayson采用行版本来消除数据库上的阻塞问题,所有查询都会自动运行在快照隔离级别,即使显示设置其他隔离级别或锁提示。快照隔离级别虽然避免在数据上加S锁,但仍会对对象加Sch-S锁,Sch-S锁会阻塞DDL语句。

由于无法在辅助副本上单独建立索引,因此为提升查询性能,需先在主副本上建立索引,这会导致在索引维护是同步大量索引整理数据

将辅助副本设置为可读时,需要考虑查询时所消耗的temp空间(临时统计和快照隔离级别的行版本信息)