无法使用 Project Tye 与 Docker 容器中的 SQL 服务器 运行 建立连接
Cannot establish connection to a SQL server running in Docker container using Project Tye
我对微服务架构的整个概念相当陌生,但我已经设法创建了一个简单的概念验证应用程序,它由 1 个 Blazor 服务器端前端、6 个微服务组成,它们都是.net 6 WEB API,目前正在使用 InMemory 数据库,它们都在启动时将模拟数据播种到它们的数据库中。
我正在使用 Project Tye 同时 运行 所有项目并轻松将它们部署到本地 kubernetes 集群。
现在我想将每个微服务的上述 InMemory 数据库迁移到 sql 服务器数据库。
Tye 允许 运行 图像作为 tye.yaml 文件中的依赖项并从配置中设置环境变量,然后通过 Tye 的 VS 扩展作为 ConnectionString 配置。
这是其中关于一个 sql 服务器的片段:
- name: sqlserver-videoteka
image: mcr.microsoft.com/mssql/server:2019-latest
env:
- name: SA_PASSWORD
value: "M!cr0s3rv!ce"
- name: ACCEPT_EULA
value: 'Y'
volumes:
- name: videoteka-storage
target: /var/opt/mssql
bindings:
- port: 9634
connectionString: Server=${host}:${port};Database=VideotekaDB;MultipleActiveResultSets=true;User Id=sa;Password=${env:SA_PASSWORD};
然后在微服务的 Program.cs 文件中添加这两行:
connectionString = configuration.GetConnectionString("sqlserver-videoteka");
builder.Services.AddDbContext<VideotekaDbContext>(options => options.UseSqlServer(connectionString));
然后我尝试 运行 tye 运行 并检查来自上述微服务的日志,这就是我遇到的:
[videoteka-api_ae1ba91d-5]:E:\OceniFilm\Videoteka.API\bin\Debug\net6.0\Videoteka.API.exe
[videoteka-api_ae1ba91d-5]: CONNECTION STRING: Server=localhost:9634;Database=VideotekaDB;MultipleActiveResultSets=true;User Id=sa;Password=M!cr0s3rv!ce;
[videoteka-api_ae1ba91d-5]: info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
[videoteka-api_ae1ba91d-5]: Entity Framework Core 6.0.3 initialized 'VideotekaDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.3' with options: None
[videoteka-api_ae1ba91d-5]: [PrepareDb] Migration unsuccessful: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
[videoteka-api_ae1ba91d-5]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[videoteka-api_ae1ba91d-5]: An error occurred using the connection to database 'VideotekaDB' on server 'localhost:9634'.
[videoteka-api_ae1ba91d-5]: fail: Microsoft.EntityFrameworkCore.Query[10100]
[videoteka-api_ae1ba91d-5]: An exception occurred while iterating over the results of a query for context type 'Videoteka.API.Data.VideotekaDbContext'.
[videoteka-api_ae1ba91d-5]: Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
[videoteka-api_ae1ba91d-5]: ---> System.ComponentModel.Win32Exception (53): The network path was not found.
[videoteka-api_ae1ba91d-5]: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
[videoteka-api_ae1ba91d-5]: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
据我了解 Tye 的配置,它会创建 sql 服务器的映像并使用我的变量作为凭据,不是吗?
单独的 sql 服务器容器是否需要 运行 在我的机器上?但是为什么它会拉取镜像并创建一个容器呢?
非常感谢任何帮助。谢谢。
编辑:CER 为我解决了关于 tye 运行 的问题,但是当 运行ning tye deploy,sql服务器被跳过?
这发生在部署结束时。
我认为可能有两个问题:
- 您的
bindings
部分应包含 containerPort:1433
以映射到 9643
。参见 tye schema。
- 回复:
connectionstring
。 SQL 主机和端口之间的服务器分隔符 不是 冒号 (:
),而是逗号 (,
)。例如localhost,9643
.
我对微服务架构的整个概念相当陌生,但我已经设法创建了一个简单的概念验证应用程序,它由 1 个 Blazor 服务器端前端、6 个微服务组成,它们都是.net 6 WEB API,目前正在使用 InMemory 数据库,它们都在启动时将模拟数据播种到它们的数据库中。 我正在使用 Project Tye 同时 运行 所有项目并轻松将它们部署到本地 kubernetes 集群。
现在我想将每个微服务的上述 InMemory 数据库迁移到 sql 服务器数据库。
Tye 允许 运行 图像作为 tye.yaml 文件中的依赖项并从配置中设置环境变量,然后通过 Tye 的 VS 扩展作为 ConnectionString 配置。
这是其中关于一个 sql 服务器的片段:
- name: sqlserver-videoteka
image: mcr.microsoft.com/mssql/server:2019-latest
env:
- name: SA_PASSWORD
value: "M!cr0s3rv!ce"
- name: ACCEPT_EULA
value: 'Y'
volumes:
- name: videoteka-storage
target: /var/opt/mssql
bindings:
- port: 9634
connectionString: Server=${host}:${port};Database=VideotekaDB;MultipleActiveResultSets=true;User Id=sa;Password=${env:SA_PASSWORD};
然后在微服务的 Program.cs 文件中添加这两行:
connectionString = configuration.GetConnectionString("sqlserver-videoteka");
builder.Services.AddDbContext<VideotekaDbContext>(options => options.UseSqlServer(connectionString));
然后我尝试 运行 tye 运行 并检查来自上述微服务的日志,这就是我遇到的:
[videoteka-api_ae1ba91d-5]:E:\OceniFilm\Videoteka.API\bin\Debug\net6.0\Videoteka.API.exe
[videoteka-api_ae1ba91d-5]: CONNECTION STRING: Server=localhost:9634;Database=VideotekaDB;MultipleActiveResultSets=true;User Id=sa;Password=M!cr0s3rv!ce;
[videoteka-api_ae1ba91d-5]: info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
[videoteka-api_ae1ba91d-5]: Entity Framework Core 6.0.3 initialized 'VideotekaDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.3' with options: None
[videoteka-api_ae1ba91d-5]: [PrepareDb] Migration unsuccessful: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
[videoteka-api_ae1ba91d-5]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[videoteka-api_ae1ba91d-5]: An error occurred using the connection to database 'VideotekaDB' on server 'localhost:9634'.
[videoteka-api_ae1ba91d-5]: fail: Microsoft.EntityFrameworkCore.Query[10100]
[videoteka-api_ae1ba91d-5]: An exception occurred while iterating over the results of a query for context type 'Videoteka.API.Data.VideotekaDbContext'.
[videoteka-api_ae1ba91d-5]: Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
[videoteka-api_ae1ba91d-5]: ---> System.ComponentModel.Win32Exception (53): The network path was not found.
[videoteka-api_ae1ba91d-5]: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
[videoteka-api_ae1ba91d-5]: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
据我了解 Tye 的配置,它会创建 sql 服务器的映像并使用我的变量作为凭据,不是吗?
单独的 sql 服务器容器是否需要 运行 在我的机器上?但是为什么它会拉取镜像并创建一个容器呢?
非常感谢任何帮助。谢谢。
编辑:CER 为我解决了关于 tye 运行 的问题,但是当 运行ning tye deploy,sql服务器被跳过?
这发生在部署结束时。
我认为可能有两个问题:
- 您的
bindings
部分应包含containerPort:1433
以映射到9643
。参见 tye schema。 - 回复:
connectionstring
。 SQL 主机和端口之间的服务器分隔符 不是 冒号 (:
),而是逗号 (,
)。例如localhost,9643
.