2

从这篇开始,以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才是真正的入口,我们返回的“组件”必需保留需要的对象引用从而开展工作。


P_Chou水冗
5.1k 声望256 粉丝

大数据spark/flink/hadoop/elasticsearch/kafka架构与开发