elasticsearch插件開發--環境搭建

從這篇開始,以nacos-elasticsearch爲例,一步步介紹如何開發插件。java

nacos-elasticsearch是一個自動將當前es節點註冊到nacos註冊中心的插件。

環境

基於maven,至少須要引用以下兩個包node

<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還提供了測試框架,方便測試。這裏要注意兩點:git

  • elasticsearch.version須要根據你的目標es版本在指定
  • 設置爲provided避免打包時將es做爲依賴打進來

打包插件推薦這麼配置:github

<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下增長文件:apache

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

同時plugin-descriptor.properties內容以下,注意其中的變量:app

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有衝突的,在最終測試時,根據插件依賴的複雜度,這塊極可能要調整。elasticsearch

基礎類

建立一個繼承自org.elasticsearch.plugins.Plugin的類NacosPlugin,這個類就是插件的入口:maven

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纔是真正的入口,咱們返回的「組件」必需保留須要的對象引用從而開展工做。

相關文章
相關標籤/搜索