1、backgroupd
ElasticSearch有提供丰富的plugin接口,我们自己能实现plugin来自定义需求。
我们的ElasticSearch是用来做时序性分析数据库,所以就会存在数据的高低峰分别。我们想要控制ElasticSearch的rebalance触发机制,避免在高峰期触发rebalance导致磁盘与网络负载大影响到正常的业务
2、how to write the plugin
(1)项目架构
(2)file
plugin.xml
这个文件不需要修改,可以直接使用
<?xml version="1.0"?>
<assembly>
<id>plugin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.basedir}/src/main/resources/plugin-descriptor.properties</source>
<outputDirectory/>
<filtered>true</filtered>
</file>
</files>
<dependencySets>
<dependencySet>
<outputDirectory/>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
plugin-descriptor.properties
这个文件需要修改classname对应的类名全路径,即自己实现的那个plugin类。
description=test
version=1.0.0
name=time-decider
classname=com.cnc.baiyx.plugin.rebalance.MyDecidersPlugin
java.version=${java.version}
elasticsearch.version=${elasticsearch.version}
pom.xml
这个文件需要按照各自的使用情况修改java.version与elasticsearch.version
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>elasticsearch-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<elasticsearch.version>7.8.0</elasticsearch.version>
<java.version>13</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
<scope>provided</scope>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.test</groupId>
<artifactId>framework</artifactId>
<version>${elasticsearch.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>target</outputDirectory>
<descriptors>
<descriptor>src/main/assembly/plugin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>plugin-descriptor.properties</exclude>
</excludes>
</resource>
</resources>
</build>
</project>
到这步项目的框架就搭建好了。可以根据各自的需求实现自己想要的功能了。
3、部署
- maven 打包,把zip包上传到服务器上
- 到es的bin目录下执行
./elasticsearch-plugin install file://[filename]
,filename
替换成zip包全路径 - 重启服务
- 其他:
elasticsearch-plugin list
可以看到所有plugin;elasticsearch-plugin remove [plugin-name]
卸载plugin
4、test
1、先关闭rebalance
2、手动触发rereoute,使分片分布不均衡。
3、开启rebalance,集群自动触发rebalance。由于在时间控制范围内,rebalance成功
4、超过时间允许范围以后,再重复上面操作,rebalance失效。