예제 #1
0
        public static void Main(string[] args)
        {
            StartGCTimer();

            var SINGLE_TENANT_ID   = CloudFoundryEnvironmentUtil.GetTenantIdFromEnv();
            var SINGLE_TENANT_TYPE = CloudFoundryEnvironmentUtil.GetTenantTypeFromEnv();

            Program.INSTANCE_GUID = CloudFoundryEnvironmentUtil.GetInstanceGuidFromEnv();
            Program.INSTANCE_IP   = CloudFoundryEnvironmentUtil.GetInstanceIpFromEnv();
            Program.INSTANCE_PORT = CloudFoundryEnvironmentUtil.GetInstancePortFromEnv();

            Program.cloudFoundryVcapApplication = CloudFoundryEnvironmentUtil.GetApplicationFromEnv();
            Program.cloudFoundryVcapServices    = CloudFoundryEnvironmentUtil.GetServicesFromEnv();

            Program.InstanceConfig = ConfigurationHelper.Load(cloudFoundryVcapApplication, Program.INSTANCE_GUID);

            if (Program.InstanceConfig != null && Program.cloudFoundryVcapApplication != null && Program.cloudFoundryVcapApplication.ApplicationUrls.Count > 0)
            {
                Program.InstanceConfig.Urls = Program.cloudFoundryVcapApplication.ApplicationUrls[0];
            }

            if (!string.IsNullOrEmpty(SINGLE_TENANT_ID) && !string.IsNullOrEmpty(SINGLE_TENANT_TYPE))
            {
                Tenant singleTenant = new Tenant()
                {
                    TenantId = SINGLE_TENANT_ID
                };

                Tenants.Add(singleTenant);
            }

            CreateWebHostBuilder(args).Build().Run();
        }
예제 #2
0
        private void StartupConfigurationLoadFromEnvironment(IApplicationBuilder app)
        {
            // instantiate the MQTTService singleton instance
            Program.serviceProvider = app.ApplicationServices;

            Program.persistencyService = Program.serviceProvider.GetService <IPersistencyService>();

            // get mongo db configuration from VCAP_SERVICES
            #region get_mongodb_configuration

            Program.mongoDbSettings = Program.serviceProvider.GetService <IMongoDBSettings>();

            if (Program.cloudFoundryVcapServices != null && Program.cloudFoundryVcapServices.MongoDBs.Count > 0)
            {
                // get mongo db configuration from CF environment VCAP_SERVICES
                var mongoService = Program.cloudFoundryVcapServices.MongoDBs.FirstOrDefault();

                Program.mongoDbSettings.MongoDbUrl = mongoService.Credentials.URI;
                Program.mongoDbSettings.User       = mongoService.Credentials.Username;
                Program.mongoDbSettings.Password   = mongoService.Credentials.Password;
                Program.mongoDbSettings.HostName   = mongoService.InstanceName;
                Program.mongoDbSettings.Port       = Convert.ToInt32(mongoService.Credentials.Port);
                Program.mongoDbSettings.DbName     = mongoService.Credentials.DbName;
            }
            else
            {
                // default devel mongo settings
                Program.mongoDbSettings.MongoDbUrl = "mongodb://*****:*****@ds235711.mlab.com:35711/nservicediscovery";
                Program.mongoDbSettings.User       = "******";
                Program.mongoDbSettings.Password   = "******";
                Program.mongoDbSettings.HostName   = "ds235711.mlab.com";
                Program.mongoDbSettings.Port       = 35711;
                Program.mongoDbSettings.DbName     = "nservicediscovery";
            }

            #endregion

            // get mqtt configuration from VCAP_SERVICES
            #region get_mqtt_configuration

            Program.mqttSettings = Program.serviceProvider.GetService <IMQTTSettings>();

            var topicTemplate = CloudFoundryEnvironmentUtil.GetMQTTTopicTemplateFromEnv();

            if (!string.IsNullOrEmpty(topicTemplate))
            {
                Program.mqttSettings.TopicTemplate = topicTemplate;
            }
            else
            {
                Program.mqttSettings.TopicTemplate = DefaultConfigurationData.DefaultMQTTTopicTemplate;
            }

            var interval = CloudFoundryEnvironmentUtil.GetMQTTReconnectIntervalInSecondsFromEnv();

            if (interval > 0)
            {
                Program.mqttSettings.ReconnectSeconds = interval;
            }
            else
            {
                Program.mqttSettings.ReconnectSeconds = DefaultConfigurationData.DefaultMQTTReconnectSeconds;
            }

            if (Program.cloudFoundryVcapServices != null && Program.cloudFoundryVcapServices.MQTTBrokers.Count > 0)
            {
                var mqttService = Program.cloudFoundryVcapServices.MQTTBrokers.FirstOrDefault();

                Program.mqttSettings.Host     = mqttService.Credentials.URI;
                Program.mqttSettings.User     = mqttService.Credentials.Username;
                Program.mqttSettings.Password = mqttService.Credentials.Password;
                Program.mqttSettings.Port     = Convert.ToInt32(mqttService.Credentials.Port);
            }
            else
            {
                Program.mqttSettings.Host     = DefaultConfigurationData.DefaultMQTTHost;
                Program.mqttSettings.User     = DefaultConfigurationData.DefaultMQTTUsername;
                Program.mqttSettings.Password = DefaultConfigurationData.DefaultMQTTPassword;
                Program.mqttSettings.Port     = DefaultConfigurationData.DefaultMQTTPort;
            }

            #endregion

            // load tenants from persistency

            #region single_tenant_or_not

            var SINGLE_TENANT_ID   = CloudFoundryEnvironmentUtil.GetTenantIdFromEnv();
            var SINGLE_TENANT_TYPE = CloudFoundryEnvironmentUtil.GetTenantTypeFromEnv();

            if (string.IsNullOrEmpty(SINGLE_TENANT_ID) && string.IsNullOrEmpty(SINGLE_TENANT_TYPE))
            {
                var tenantsRepo            = Program.serviceProvider.GetService <IMemoryTenantsRepository>();
                var persistencyTenantsRepo = Program.serviceProvider.GetService <IPersistencyTenantRepository>();

                var persistencyTenants = persistencyTenantsRepo.LoadPersistedTenants();

                foreach (var t in persistencyTenants)
                {
                    tenantsRepo.Add(new NServiceDiscovery.Entity.Tenant()
                    {
                        TenantId    = t.TenantId,
                        TenantToken = t.TenantToken
                    });
                }

                Program.Tenants = tenantsRepo.GetAll();
            }

            #endregion

            // start persistency sync timer with random interval
            var random = new Random();
            Program.persistencyService.SetPersistencyTimerInterval(random.Next(Program.InstanceConfig.PersistencySyncMinRandomSeconds, Program.InstanceConfig.PersistencySyncMaxRandomSeconds));

            Program.mqttService = Program.serviceProvider.GetService <IMQTTService>();

            Program.evictionService = Program.serviceProvider.GetService <IEvictionService>();
        }