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