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();
guice相关介绍可以参见以下链接:
关键点在于:
@inject注释---guice会扫描inject注释,并对方法中出现的参数实例寻找对应注册的实例进行初始化。
bind接口-----将接口跟具体实现类绑定
点击打开链接
点击打开链接