我對微服務架構的整個概念相當陌生,但我設法創建了一個簡單的概念驗證應用程式,它由 1 個 Blazor 服務器端前端、6 個微服務組成,它們都是 .net 6 個 WEB API并且目前正在使用 InMemory 資料庫,并且它們都在啟動時在其資料庫中播種模擬資料。我正在使用 Project Tye 同時運行所有專案并輕松地將它們部署到本地 kubernetes 集群。
現在我想將每個微服務的上述 InMemory 資料庫遷移到 sqlserver 資料庫。
Tye 允許將影像作為 tye.yaml 檔案中的依賴項運行,并從配置中設定環境變數,然后通過 Tye 的 VS 擴展作為 ConnectionString 進行配置。
這是其中關于一個 sqlserver 的片段:
- 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 run并檢查來自提到的微服務的日志,這就是我遇到的:
[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 服務器的映像并使用我的變數作為憑據,不是嗎?
是否需要在我的機器上運行一個單獨的 sqlserver 容器?但是為什么它會拉取影像并創建一個容器呢?
任何幫助深表感謝。謝謝你。
編輯:CER 為我解決了關于tye run的問題,但是在運行tye deploy時,sqlserver 被跳過了嗎?

這發生在部署結束時。
uj5u.com熱心網友回復:
我認為可能有2個問題:
- 您的
bindings部分應包括containerPort:1433映射到9643. 請參閱tye 架構。 - 回復:
connectionstring。主機和埠之間的 SQL Server 分隔符不是冒號 (:),而是逗號 (,)。例如localhost,9643。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/472211.html
上一篇:關于DockerHub的一些問題
