我正在我的登台环境机器中使用来自官方docker映像的MongoDB 3.4.4实例。我使用MongoDB.Driver NuGet软件包2.5.0从代码进行连接。 当我从已部署的应用程序连接时运行良好(从aspnetcore:2.0.3基本映像在docker容器中运行)。我的应用程序有一些集成测试。在应用程序项目和测试中,与Mongo创建的连接相同,如下所示:
services.AddSingleton<IMongoClient>(s =>
{
var config = s.GetService<ISettings>();
var logger = s.GetService<ILogger<IMongoClient>>();
var settings = MongoClientSettings.FromUrl(new MongoUrl("mongodb://dev:pass@192.168.0.163/authDb?connect=direct"));
settings.MaxConnectionIdleTime = TimeSpan.FromSeconds(5);
settings.MaxConnectionPoolSize = 1000;
settings.MinConnectionPoolSize = 30;
if (config.ActiveLogMongoQueries)
{
settings.ClusterConfigurator = (cb) =>
{
cb.Subscribe<CommandStartedEvent>(
(e) =>
{
if (!IsServiceRequest(e.CommandName))
{
logger.LogInformation($"Mongo.Query DataBaseNameSpace: {e.DatabaseNamespace}. CommandName: {e.CommandName}. Command: {e.Command.ToString()}");
}
});
};
}
return new MongoClient(settings);
});
services.AddTransient(s =>
{
var config = s.GetService<ISettings>();
var dbName = "myDbName";
var mongoConnectionString = new MongoUrl(config.MongoConnectionString);
if (!string.IsNullOrEmpty(mongoConnectionString.DatabaseName))
{
dbName = mongoConnectionString.DatabaseName;
}
return s.GetService<IMongoClient>().GetDatabase(dbName);
});
当我从Visual Studio IDE测试运行程序运行集成测试时,这也很好用。但是当我尝试使用gitlab CI自动运行测试项目时,我有了ConnectionTimeoutExeption:
System.AggregateException : One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Direct", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "192.168.0.163:27017" }", EndPoint: "192.168.0.163:27017", State: "Disconnected", Type: "Unknown" }] }.) (The following constructor parameters did not have matching fixture data: MongoDatabaseFixture fixture)
---- System.TimeoutException : A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Direct", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "192.168.0.163:27017" }", EndPoint: "192.168.0.163:27017", State: "Disconnected", Type: "Unknown" }] }.
---- The following constructor parameters did not have matching fixture data: MongoDatabaseFixture fixture
Stack Trace:
----- Inner Stack Trace #1 (System.TimeoutException) -----
at MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(IServerSelector selector, ClusterDescription description)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(Task completedTask)
at MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChanged(IServerSelector selector, ClusterDescription description, Task descriptionChangedTask, TimeSpan timeout, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Clusters.Cluster.SelectServer(IServerSelector selector, CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupportedAfterServerSelection(CancellationToken cancellationToken)
at MongoDB.Driver.MongoClient.AreSessionsSupported(CancellationToken cancellationToken)
at MongoDB.Driver.OperationExecutor.StartImplicitSession(CancellationToken cancellationToken)
at MongoDB.Driver.MongoDatabaseImpl.UsingImplicitSession(Action`1 func, CancellationToken cancellationToken)
at MongoDB.Driver.MongoDatabaseImpl.DropCollection(String name, CancellationToken cancellationToken)
at IntegrationTests.MongoDatabaseFixture.DropPreviousTestData() in /builds/project/project-main/project.IntegrationTests/MongoDatabaseFixture.cs:line 40
----- Inner Stack Trace #2 (Xunit.Sdk.TestClassException) -----
我在gitlab-ci.yml中的测试阶段如下:
integrationtests:
image: microsoft/aspnetcore-build:1.0-2.0
stage: integrationtests
script:
- dotnet test -c Release ./$PROJECT_NAME.IntegrationTests/$PROJECT_NAME.IntegrationTests.csproj
tags:
- docker
only:
- schedules
主机192.168.0.163操作系统是Ubuntu,它还包含其他docker容器(如clickhouse),并且在gitlab Runner上运行测试时,与它们的连接没有问题。
我尝试将MongoDb.Driver更新到最新版本2.7.2,它解决了问题。但是出于某些原因,对我来说更好的是不更新驱动程序版本。 我认为,如果代码在运行的应用程序中正常工作时不抛出异常,那么相同的代码在测试中也可以正常工作吗? 任何避免更新MongoDb库和 为什么会发生?我还看到了这篇帖子Connection times out after upgrading MongoDB.Driver from 2.7.0 to 2.7.1
但是,如果问题出在驱动程序上,为什么应用程序可以与旧的Mongo.Driver库一起正常工作? 非常感谢!
已更新:今天,我尝试重新制作由gitlabRunner执行的步骤(如果我正确输入的话)。我在本地构建相同的映像,并在我的PC上使用docker运行它。 我使用以下.Dockerfile进行构建:
FROM microsoft/aspnetcore-build:1.0-2.0 as base
WORKDIR /app
EXPOSE 80
FROM base AS final
WORKDIR /Test
COPY ./Test /Test
ENTRYPOINT /bin/bash
其中“测试”是我的解决方案文件夹。接下来,我使用以下命令进入容器:
docker run -it 2da12ce6cb7d
(在构建cmd之后由docker分配的名称)并尝试执行
- dotnet test -c Release ./ProjectName.IntegrationTests/ProjectName.IntegrationTests.csproj
并再次获得了这些例外。因此,因为我运行的应用程序(可以正常连接)使用microsoft / aspnetcore:2.0.3 Docker映像作为基础,所以问题可能出在其中。