乐趣区

关于elasticsearch:elasticsearch插件开发环境搭建

从这篇开始,以 nacos-elasticsearch 为例,一步步介绍如何开发插件。

nacos-elasticsearch 是一个主动将以后 es 节点注册到 nacos 注册核心的插件。

环境

基于 maven,至多须要援用如下两个包

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>${elasticsearch.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.elasticsearch.test</groupId>
  <artifactId>framework</artifactId>
  <version>${elasticsearch.version}</version>
  <scope>test</scope>
</dependency>

开发 es 插件,必定是要援用 es 的依赖,而且 es 还提供了测试框架,不便测试。这里要留神两点:

  • elasticsearch.version须要依据你的指标 es 版本在指定
  • 设置为 provided 防止打包时将 es 作为依赖打进来

打包插件举荐这么配置:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptors>
        <descriptor>src/assembly/release.xml</descriptor>
      </descriptors>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <!-- here the phase you need -->
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
          <resources>
            <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

resources 下减少文件:

src/main/resources/plugin-descriptor.properties
src/main/resources/plugin-security.policy

同时 plugin-descriptor.properties 内容如下,留神其中的变量:

java.version=1.8
name=${project.artifactId}
version=${version}
description=${project.description}.
elasticsearch.version=${elasticsearch.version}
classname=com.eoi.nacos.elasticsearch.NacosPlugin

maven-resources-plugin会将 src/main/resources 下的资源文件复制到 target/extra-resources 目录,并替换文件中的变量。

maven-assembly-plugin依据 src/assembly/release.xml 的配置进行打包。src/assembly/release.xml如下:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!-- Assembles a packaged version targeting OS installation. -->
    <id>${build.timestamp}</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <!-- maven-resources-plugin will filter the file and copy into the following directory -->
            <directory>${project.basedir}/target/extra-resources</directory>
            <outputDirectory>.</outputDirectory>
            <includes>
                <include>plugin-descriptor.properties</include>
                <include>plugin-security.policy</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <excludes>
                <!-- exclude dependency that elasticsearch already contains -->
                <exclude>org.joda:joda-convert</exclude>
                <exclude>joda-time:joda-time</exclude>
                <exclude>org.yaml:snakeyaml</exclude>
                <exclude>com.fasterxml.jackson.dataformat:jackson-dataformat-yaml</exclude>
                <exclude>jackson-core</exclude>

                <!-- the following jar is duplicated in plugin repository-hdfs -->
                <exclude>commons-collections:commons-collections</exclude>
                <exclude>commons-logging:commons-logging</exclude>
            </excludes>
            <outputDirectory>.</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useTransitiveFiltering>true</useTransitiveFiltering>
        </dependencySet>
    </dependencySets>
</assembly>

这里设置打包次要申明了生成 zip 包(es 插件的规范公布形式),并把替换好的 plugin-descriptor.propertiesplugin-security.policy打到 zip 包中。同时设置排除一些包,这些包是与 es 有抵触的,在最终测试时,依据插件依赖的复杂度,这块很可能要调整。

根底类

创立一个继承自 org.elasticsearch.plugins.Plugin 的类NacosPlugin,这个类就是插件的入口:

public class NacosPlugin extends Plugin {
    protected final Settings settings;
    public NacosPlugin(final Settings settings) {this.settings = settings;}
    
    @Override
    public List<Setting<?>> getSettings() {...}
    
    @Override
    public Collection<Object> createComponents(...) {...}
}

简直所有的插件都须要配置,在插件的实现中,通过重写 getSettings 办法,裸露插件对外反对的配置。用户在 elasticsearch.yml 中配置的相干信息,会通过构造函数传给插件类。
createComponents是返回插件实例化的“组件”。常见的组件能够是 org.elasticsearch.common.component.AbstractLifecycleComponent,该组件需实现doStartdoStop,对应 es 的启动和进行事件。createComponents参数如下:

  • Client client: 罕用。通过 client 拜访 es 的各种接口,同时能够衍生出 ClusterAdminClient。Nacos 插件中须要通过ClusterAdminClient 获取节点的拜访点信息
  • ClusterService clusterService
  • ThreadPool threadPool: 罕用。es 提供的线程池入口,能够通过 threadPool 来启动定时工作线程等异步操作
  • ResourceWatcherService resourceWatcherService
  • ScriptService scriptService
  • NamedXContentRegistry xContentRegistry
  • Environment environment: es 环境相干信息
  • NodeEnvironment nodeEnvironment: 节点环境相干信息,例如获取 nodeId
  • NamedWriteableRegistry namedWriteableRegistry

插件与 es 的交互,简直都是通过上述参数传来的对象进行的。因而大多数状况下 createComponents 才是真正的入口,咱们返回的“组件”必须保留须要的对象援用从而发展工作。

退出移动版