示例#1
0
        /// <summary>
        ///  Dependency injection configuration
        /// </summary>
        /// <param name="container">Dependency injection container</param>
        /// <param name="args">
        /// Startup parameters
        /// </param>
        /// <returns>The actor system</returns>
        public static ActorSystem ConfigureAndStart(IWindsorContainer container, string[] args)
        {
            Console.WriteLine(@"Starting bootstrapper");

            container.AddFacility<TypedFactoryFacility>();
            container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel, true));
            container.Register(Component.For<IWindsorContainer>().Instance(container));

            container.RegisterWindsorInstallers();

            Console.WriteLine(@"Preparing config");
            var config = BaseInstaller.GetStackedConfig(container, CreateTopLevelConfig(args));

            Log.Debug($"Cluster configuration: seed-nodes { string.Join(", ", config.GetStringList("akka.cluster.seed-nodes") ?? new List<string>())}");
            Log.Debug($"Cluster configuration: min-nr-of-members { config.GetInt("akka.cluster.min-nr-of-members")}");
            var roles = string.Join(", ", config.GetStringList("akka.cluster.roles") ?? new List<string>());
            Log.Debug($"Cluster configuration: roles { roles}");
            Log.Debug($"Cluster node hostname: { config.GetString("akka.remote.helios.tcp.hostname") }");
            var publicHostName = config.GetString("akka.remote.helios.tcp.public-hostname");
            if (!string.IsNullOrWhiteSpace(publicHostName))
            {
                Log.Debug($"Cluster node public hostname: { publicHostName }");
            }

            container.Register(Component.For<Config>().Instance(config));
            Console.WriteLine(@"Config created");

            // log configuration
            LogEventLevel level;
            if (!Enum.TryParse(config.GetString("ClusterKit.Log.minimumLevel"), true, out level))
            {
                level = LogEventLevel.Information;
            }

            var loggerConfig = new LoggerConfiguration().MinimumLevel.Is(level);
            var configurators = container.ResolveAll<ILoggerConfigurator>();

            configurators.ForEach(c => Log.Information("Using log configurator {TypeName}", c.GetType().FullName));

            loggerConfig = configurators.Aggregate(
                loggerConfig,
                (current, loggerConfigurator) => loggerConfigurator.Configure(current, config));

            var hostName = string.IsNullOrWhiteSpace(publicHostName)
                               ? config.GetString("akka.remote.helios.tcp.hostname")
                               : publicHostName;

            loggerConfig = loggerConfig.Enrich.WithProperty("hostName", hostName);
            loggerConfig = loggerConfig.Enrich.WithProperty("roles", roles);

            var logger = loggerConfig.CreateLogger();
            Log.Logger = logger;

            // log configuration finished

            // performing prestart checks
            BaseInstaller.RunPrecheck(container, config);

            // starting Akka system
            Console.WriteLine(@"starting akka system");
            var actorSystem = ActorSystem.Create("ClusterKit", config);
            actorSystem.AddDependencyResolver(new WindsorDependencyResolver(container, actorSystem));

            container.Register(Component.For<ActorSystem>().Instance(actorSystem).LifestyleSingleton());
            ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));

            Console.WriteLine(@"Bootstrapper start finished");

            return actorSystem;
        }