1. 程式人生 > >elasticsearch原始碼分析--啟動程序

elasticsearch原始碼分析--啟動程序

es啟動指令碼是bin目錄下的elasticsearch。其中需要載入一些環境變數,諸如ES_CLASSPATH JAVA_OPTS  ES_JAVA_OPTS等。推薦設定ES_HEAP_SIZE的值,來設定es程序需要的記憶體,同時設定-p引數來指定pid檔案的生成位置,在es關閉的時候會用到。

指令碼內容不再贅述,java主類位於org.elasticsearch.bootstrap。主要邏輯是生成了一個InternalNode節點,並且啟動了一個keepalive執行緒。結點的啟動代表整個es程序啟動了。

InternalNode節點是通過NodeBuilder來生成的,節點在建構函式中通過ModuleBuilder類來進行模組的注入(是對Guice的一個封裝而已),同時生成injector例項,如下

            ModulesBuilder modules = new ModulesBuilder();
            modules.add(new Version.Module(version));
            modules.add(new CacheRecyclerModule(settings));
            modules.add(new PageCacheRecyclerModule(settings));
            modules.add(new BigArraysModule(settings));
            modules.add(new PluginsModule(settings, pluginsService));
            modules.add(new SettingsModule(settings));
            modules.add(new NodeModule(this));
            modules.add(new NetworkModule());
            modules.add(new ScriptModule(settings));
            modules.add(new EnvironmentModule(environment));
            modules.add(new NodeEnvironmentModule(nodeEnvironment));
            modules.add(new ClusterNameModule(settings));
            modules.add(new ThreadPoolModule(settings));
            modules.add(new DiscoveryModule(settings));
            modules.add(new ClusterModule(settings));
            modules.add(new RestModule(settings));
            modules.add(new TransportModule(settings));
            if (settings.getAsBoolean("http.enabled", true)) {
                modules.add(new HttpServerModule(settings));
            }
            modules.add(new RiversModule(settings));
            modules.add(new IndicesModule(settings));
            modules.add(new SearchModule());
            modules.add(new ActionModule(false));
            modules.add(new MonitorModule(settings));
            modules.add(new GatewayModule(settings));
            modules.add(new NodeClientModule());
            modules.add(new BulkUdpModule());
            modules.add(new ShapeModule());
            modules.add(new PercolatorModule());
            modules.add(new ResourceWatcherModule());
            modules.add(new RepositoriesModule());
            modules.add(new TribeModule());
            injector = modules.createInjector();
在InternalNode的start方法中,用injector對各個模組完成啟動,各自負責各自的功能,如下:
        injector.getInstance(Discovery.class).setAllocationService(injector.getInstance(AllocationService.class));

        for (Class<? extends LifecycleComponent> plugin : pluginsService.services()) {
            injector.getInstance(plugin).start();
        }

        injector.getInstance(MappingUpdatedAction.class).start();
        injector.getInstance(IndicesService.class).start();
        injector.getInstance(IndexingMemoryController.class).start();
        injector.getInstance(IndicesClusterStateService.class).start();
        injector.getInstance(IndicesTTLService.class).start();
        injector.getInstance(RiversManager.class).start();
        injector.getInstance(SnapshotsService.class).start();
        injector.getInstance(ClusterService.class).start();
        injector.getInstance(RoutingService.class).start();
        injector.getInstance(SearchService.class).start();
        injector.getInstance(MonitorService.class).start();
        injector.getInstance(RestController.class).start();
        injector.getInstance(TransportService.class).start();
        DiscoveryService discoService = injector.getInstance(DiscoveryService.class).start();
        discoService.waitForInitialState();

        // gateway should start after disco, so it can try and recovery from gateway on "start"
        injector.getInstance(GatewayService.class).start();

        if (settings.getAsBoolean("http.enabled", true)) {
            injector.getInstance(HttpServer.class).start();
        }
        injector.getInstance(BulkUdpService.class).start();
        injector.getInstance(ResourceWatcherService.class).start();
        injector.getInstance(TribeService.class).start();


至此,es程序啟動完畢。

guice相關介紹可以參見以下連結:

關鍵點在於:

@inject註釋---guice會掃描inject註釋,並對方法中出現的引數例項尋找對應註冊的例項進行初始化。

bind介面-----將介面跟具體實現類繫結