Galosys.Foundation.Data
26.5.12.3
See the version list below for details.
dotnet add package Galosys.Foundation.Data --version 26.5.12.3
NuGet\Install-Package Galosys.Foundation.Data -Version 26.5.12.3
<PackageReference Include="Galosys.Foundation.Data" Version="26.5.12.3" />
<PackageVersion Include="Galosys.Foundation.Data" Version="26.5.12.3" />
<PackageReference Include="Galosys.Foundation.Data" />
paket add Galosys.Foundation.Data --version 26.5.12.3
#r "nuget: Galosys.Foundation.Data, 26.5.12.3"
#:package Galosys.Foundation.Data@26.5.12.3
#addin nuget:?package=Galosys.Foundation.Data&version=26.5.12.3
#tool nuget:?package=Galosys.Foundation.Data&version=26.5.12.3
Galosys.Foundation.Data
成熟度: 🟢 稳定 — 生产可用,测试充分,活跃维护
简介
Galosys.Foundation.Data 提供 ADO.NET 多数据源支持,包含连接池管理、主从读写分离、健康检查等企业级特性。
特性
- 多数据源管理 - 基于
IDataSource接口的统一数据源抽象 - 连接池 -
DruidDataSource内置连接池,支持泄漏检测 - 主从读写分离 -
DataSourceContext基于 AsyncLocal 的线程安全上下文切换 - 健康检查池 -
HealthCheckedDataSourcePoolRound-robin 轮询 + 健康探测 + 全挂降级主库 - 自动 DI 注册 - 通过配置自动注册数据源
安装
<PackageReference Include="Galosys.Foundation.Data" Version="x.x.x" />
配置
1. 连接字符串配置
在 appsettings.json 中配置主库和副本连接字符串:
{
"ConnectionStrings": {
"Default": "Server=master-host;Database=MyDb;User Id=sa;Password=xxx;Provider=Microsoft.Data.SqlClient",
"Default.Replica1": "Server=replica1-host;Database=MyDb;User Id=sa;Password=xxx;Provider=Microsoft.Data.SqlClient",
"Default.Replica2": "Server=replica2-host;Database=MyDb;User Id=sa;Password=xxx;Provider=Microsoft.Data.SqlClient"
}
}
最小化配置仅需主库连接字符串(无
Replica后缀),模块会自动识别副本。
2. 注册数据源
// 在 Program.cs 或 Startup.cs 中
services.AddDataSources(configuration);
有副本时自动创建 HealthCheckedDataSourcePool(Round-robin + 健康探测),无副本时直接使用主库 PooledDataSource。
使用示例
基本查询
public class QueryService
{
private readonly IDataSource _dataSource;
public QueryService([FromKeyedServices("Default")] IDataSource dataSource)
{
_dataSource = dataSource;
}
public async Task<List<User>> GetUsersAsync()
{
using var conn = _dataSource.GetConnection();
// 执行查询...
}
}
读写分离
通过 DataSourceContext.SwitchTo 切换到副本上下文:
using (DataSourceContext.SwitchTo(DataSourceType.Replica))
{
// 此范围内的查询走副本
var users = await GetUsersAsync();
}
// 离开 using 后自动恢复之前的上下文
嵌套切换
using (DataSourceContext.SwitchTo(DataSourceType.Master))
{
// 写操作走主库
await SaveOrderAsync(order);
using (DataSourceContext.SwitchTo(DataSourceType.Replica))
{
// 读操作走副本
var stats = await GetStatsAsync();
}
// 自动恢复为 Master
}
DataSourceType 枚举
| 值 | 说明 |
|---|---|
Master |
主库,默认值 |
Replica |
副本,用于读操作 |
核心类
| 类/接口 | 说明 |
|---|---|
IDataSource |
数据源接口,提供 GetConnection() |
PooledDataSource |
简单数据源,每次创建新连接 |
DruidDataSource |
连接池数据源,支持泄漏检测 |
HealthCheckedDataSourcePool |
健康检查池,Round-robin 轮询副本 |
DataSourceContext |
AsyncLocal 上下文,管理读写分离 |
DataSourceType |
数据源类型枚举(Master / Replica) |
HealthCheckedDataSourcePool
| 参数 | 默认值 | 说明 |
|---|---|---|
master |
必填 | 主库数据源,全挂降级使用 |
replicas |
必填(至少 1 个) | 副本数据源集合 |
healthCheckInterval |
30 秒 | 健康探测间隔 |
行为:
- 轮询选择: Round-robin 依次选择健康副本
- 跳过不健康副本: 健康检查失败自动跳过
- 全挂降级主库: 所有副本不健康时自动回退主库
- 自动恢复: 副本恢复健康后自动重新纳入轮询
DruidDataSource 连接池修复
修复了 GetConnection 中 lock + semaphore 嵌套导致的死锁问题:信号量等待移到 lock 外部,确保等待线程不阻塞连接归还。
配置项(通过 ConnectionStrings 节点下的子键指定):
| 配置项 | 默认值 | 说明 |
|---|---|---|
ConnectionString |
必填 | 数据库连接字符串 |
Provider |
Microsoft.Data.SqlClient |
数据库提供程序 |
MaxPoolSize |
20 | 最大连接数 |
MinPoolSize |
5 | 最小连接数 |
ConnectionTimeout |
00:00:30 | 获取连接超时 |
LeakDetectionThreshold |
00:30:00 | 连接泄漏检测阈值 |
依赖
- Galosys.Foundation.Core
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Galosys.Foundation.Core (>= 26.5.12.3)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Galosys.Foundation.Data:
| Package | Downloads |
|---|---|
|
Galosys.Foundation.EntityFrameworkCore
Galosys.Foundation快速开发库 |
|
|
Galosys.Foundation.Dapper
Galosys.Foundation快速开发库 |
|
|
Galosys.Foundation.FreeSql
Galosys.Foundation快速开发库 |
|
|
Galosys.Foundation.SqlSugar
Galosys.Foundation快速开发库 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.5.19.1 | 245 | 5/19/2026 |
| 26.5.18.1 | 242 | 5/18/2026 |
| 26.5.15.1 | 243 | 5/15/2026 |
| 26.5.12.3 | 241 | 5/12/2026 |
| 26.5.12.2 | 225 | 5/12/2026 |
| 26.4.27.1-rc1 | 223 | 4/26/2026 |
| 26.4.25.1-rc1 | 216 | 4/25/2026 |
| 26.4.22.2-rc7 | 225 | 4/22/2026 |
| 26.4.22.2-rc6 | 214 | 4/22/2026 |
| 26.4.22.2-rc4 | 211 | 4/22/2026 |
| 26.4.22.2-rc3 | 211 | 4/22/2026 |
| 26.4.19.1-rc1 | 145 | 4/19/2026 |
| 26.4.12.8-rc1 | 195 | 4/12/2026 |
| 26.4.12.7-rc1 | 190 | 4/12/2026 |
| 26.1.30.1-rc1 | 216 | 1/30/2026 |
| 26.1.29.1 | 225 | 1/29/2026 |
| 26.1.28.5 | 225 | 1/28/2026 |
| 26.1.28.4 | 220 | 1/28/2026 |
| 26.1.28.2 | 222 | 1/28/2026 |
| 26.1.23.6 | 233 | 1/23/2026 |