将环境变量设置为docker-compose中的字符串数组

时间:2020-02-19 06:47:48

标签: docker docker-compose

我想从docker-compose.override文件中读取队列名称。为此,我使它们成为字符串数组 并在下面这样定义它们

environment:

 - RabbitMQOptions__ConnectionString=-rabbitmq
 - RabbitMQOptions__BrokerName=event_demo
 - RabbitMQOptions__QueueName=["sampleQueue", "daemonQueue"]

Docker compose文件建立得很好。但是我无法从数组中获取任何值。是否还有其他方法可以设置此env变量。

我正在通过Startup.cs文件从docker-compose文件中注入值

services.AddSingleton<IEventBus, RabbitMQEventBus>(sp =>
        {
            var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
            var logger = sp.GetRequiredService<ILogger<RabbitMQEventBus>>();
            var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionManager>();
            var options = sp.GetRequiredService<IOptionsMonitor<RabbitMQOptions>>();

            return new RabbitMQEventBus(options.CurrentValue, eventBusSubcriptionsManager, rabbitMQPersistentConnection, logger, services);
        });

在构造函数中,我通过依赖注入获得值

 public RabbitMQEventBus(
        RabbitMQOptions options,
        IEventBusSubscriptionManager subscriptionManager,
        IRabbitMQPersistentConnection persistentConnection,
        ILogger<RabbitMQEventBus> logger,
        IServiceCollection services
        )
    {
        _persistentConnection = persistentConnection;
        _subsManager = subscriptionManager;
        _options = options;
        _queueName = _options.QueueName[0];
        _consumerChannel = CreateConsumerChannel();
        _subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
        _logger = logger;
        _services = services;
        BROKER_NAME = _options.BrokerName;
    }

这是RabbitMQOptions类

public class RabbitMQOptions
{
    public string ConnectionString { get; set; }

    public string BrokerName { get; set; }

    public string[] QueueName { get; set; }
}

0 个答案:

没有答案