调试Elasticsearch源码-5.1.1 --搭建(-)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013435007/article/details/88414057

前言

        ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。

软件环境

操作系统 win10

Elasticsearch 版本 5.1.3

JDK版本 1.8

Gradle 和 Intellij Idea

准备环境

1、安装JDK 

    下载地址 https://www.oracle.com/technetwork/java/javase/downloads/index.html

2、下载 elastic search 源码

    下载地址  https://github.com/elastic/elasticsearch

git clone -b 5.1 https://github.com/elastic/elasticsearch

3、下载gradle 安装包

进入下载后的elasticsearch中,查看 elasticsearch\gradle\wrapper\gradle-wrapper.properties 发现如下配置:

distributionUrl=https://services.gradle.org/distributions/gradle-2.13-all.zip

Elasticsearch 5.1.1需要安装gradle-2.13,官方下载地址: 
https://services.gradle.org/distributions/gradle-2.13-all.zip

由于国内网速的问题,我们可以先下载,然后拷贝,

将下载的gradle-2.13-all.zip包放到 elasticsearch\gradle\wrapper 目录下, 保证与 elasticsearch\gradle\wrapper\gradle-wrapper.properties 同目录,然后修改 elasticsearch\gradle\wrapper\gradle-wrapper.properties 配置如下:

distributionUrl=gradle-2.13-all.zip

4、配置 Gradle 仓库

进入elasticsearch目录,找到 benchmarks 和 client\beanchmark 目录中的build.gradle 修改如下:

buildscript {
    repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    }
}

编辑 Gradle 全局 仓库

在${USER_HOME}/.gradle/下面创建新文件 init.gradle,编辑

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        all {
            ArtifactRepository repo ->
    if (repo instanceof MavenArtifactRepository) {
                def url = repo.url.toString()
                if (url.startsWith('https://repo.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}

5、gradle编译

cmd 进入 elasticsearch 根目录,执行如下:

  • gradlew idea
  • gradlew idea -info
  • gradlew idea -debug

以上任何一个 都可以。

6、elasticsearch 导入idea 

导入项目后,会执行 gradle 编译,如下:

运行项目,解决异常

找到 elasticsearch/server/src/main/org/elasticsearch/bootstrap 下启动类 Elasticsearch.java,打开文件,右键 Run Elasticsearch.main(),运行main方法
1、报错如下:

这里需要配置es.pathhome ,首先在 elasticsearch 根路径下新建一个文件夹 home;

 打开 Edit Configurations,编辑VM options,添加如下配置:

-Des.path.home=${ROOT_PATH}\home\

ROOT_PATH指当前elastic search的绝对路径。

再次执行 Elasticsearch.main();

2、报错如下

ERROR: the system property [es.path.conf] must be set

这里需要配置es.path.conf参数,只需在上述问题中home文件夹中新建config文件夹,然后在

https://www.elastic.co/downloads/elasticsearch 下载 5.1.1 elasticsearch 发行版【zip】,解压复制到config 文件夹中数据。

然后在 VM options 追加,如下:

-Des.path.conf=${ROOT_PATH}\home\config

执行Elasticsearch.main();

3、报错如下

这个问题在https://github.com/elastic/elasticsearch/issues/21932 看到是权限问题,但是发现对我们这样启动并不起作用。

可以创建一个java.policy文件放在home文件夹中,然后在里面写入:
grant {
permission javax.management.MBeanTrustPermission "register";
permission javax.management.MBeanServerPermission "createMBeanServer";
};

或者 我们不启用jmx 配置,在VM options中添加如下配置:

-Dlog4j2.disable.jmx=true

然后执行 run;

4、报错如下

上述问题,造成的原因 是没有加载 modules, 由于我们新建了home文件夹,需要配置home\modules文件夹,并且在modules中复制模块elasticsearch\modules内容。

public PluginsService(Settings settings, Path modulesDirectory, Path pluginsDirectory, Collection<Class<? extends Plugin>> classpathPlugins) {
        super(settings);

        List<Tuple<PluginInfo, Plugin>> pluginsLoaded = new ArrayList<>();
        List<PluginInfo> pluginsList = new ArrayList<>();
        // first we load plugins that are on the classpath. this is for tests and transport clients
        for (Class<? extends Plugin> pluginClass : classpathPlugins) {
            Plugin plugin = loadPlugin(pluginClass, settings);
            PluginInfo pluginInfo = new PluginInfo(pluginClass.getName(), "classpath plugin", "NA", pluginClass.getName());
            if (logger.isTraceEnabled()) {
                logger.trace("plugin loaded from classpath [{}]", pluginInfo);
            }
            pluginsLoaded.add(new Tuple<>(pluginInfo, plugin));
            pluginsList.add(pluginInfo);
        }

        List<PluginInfo> modulesList = new ArrayList<>();
        // load modules
        if (modulesDirectory != null) {
            try {
                List<Bundle> bundles = getModuleBundles(modulesDirectory);
                List<Tuple<PluginInfo, Plugin>> loaded = loadBundles(bundles);
                pluginsLoaded.addAll(loaded);
                for (Tuple<PluginInfo, Plugin> module : loaded) {
                    modulesList.add(module.v1());
                }
            } catch (IOException ex) {
                throw new IllegalStateException("Unable to initialize modules", ex);
            }
        }

        // now, find all the ones that are in plugins/
        if (pluginsDirectory != null) {
            try {
                List<Bundle> bundles = getPluginBundles(pluginsDirectory);
                List<Tuple<PluginInfo, Plugin>> loaded = loadBundles(bundles);
                pluginsLoaded.addAll(loaded);
                for (Tuple<PluginInfo, Plugin> plugin : loaded) {
                    pluginsList.add(plugin.v1());
                }
            } catch (IOException ex) {
                throw new IllegalStateException("Unable to initialize plugins", ex);
            }
        }

        this.info = new PluginsAndModules(pluginsList, modulesList);
        this.plugins = Collections.unmodifiableList(pluginsLoaded);

        // We need to build a List of plugins for checking mandatory plugins
        Set<String> pluginsNames = new HashSet<>();
        for (Tuple<PluginInfo, Plugin> tuple : this.plugins) {
            pluginsNames.add(tuple.v1().getName());
        }

        // Checking expected plugins
        List<String> mandatoryPlugins = MANDATORY_SETTING.get(settings);
        if (mandatoryPlugins.isEmpty() == false) {
            Set<String> missingPlugins = new HashSet<>();
            for (String mandatoryPlugin : mandatoryPlugins) {
                if (!pluginsNames.contains(mandatoryPlugin) && !missingPlugins.contains(mandatoryPlugin)) {
                    missingPlugins.add(mandatoryPlugin);
                }
            }
            if (!missingPlugins.isEmpty()) {
                throw new ElasticsearchException("Missing mandatory plugins [" + Strings.collectionToDelimitedString(missingPlugins, ", ") + "]");
            }
        }

        // we don't log jars in lib/ we really shouldn't log modules,
        // but for now: just be transparent so we can debug any potential issues
        logPluginInfo(info.getModuleInfos(), "module", logger);
        logPluginInfo(info.getPluginInfos(), "plugin", logger);
    }

// load modules if (modulesDirectory != null)  时 modulesDirectory 文件路径显示的时 home\modules,因此我们需要把elasticsearch\modules相应依赖导进去,或者去官网下个zip包:
https://www.elastic.co/downloads/elasticsearch

解压后把里面config、modules、plugins 复制到home里面

运行Elasticsearch.main()

访问浏览器 http://localhost:9200

{
  "name" : "stqjcuC",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "EGPbxLviQZeKdFrX6TcTxA",
  "version" : {
    "number" : "5.1.1",
    "build_hash" : "Unknown",
    "build_date" : "Unknown",
    "build_snapshot" : true,
    "lucene_version" : "6.3.0"
  },
  "tagline" : "You Know, for Search"
}

至此搭建已经完成。

猜你喜欢

转载自blog.csdn.net/u013435007/article/details/88414057
今日推荐