從這篇開始,以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.properties
和plugin-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
,該組件需實現doStart
和doStop
,對應es的啓動和中止事件。createComponents
參數以下:
ClusterAdminClient
。Nacos插件中須要經過ClusterAdminClient
獲取節點的訪問點信息插件與es的交互,幾乎都是經過上述參數傳來的對象進行的。所以大多數狀況下createComponents
纔是真正的入口,咱們返回的「組件」必需保留須要的對象引用從而開展工做。