概要
Cloudify插件开发
何时需要开发你自己的插件
配方文件旨在处理最常见的情况,它是不可能处理所有的场景,最终,您可能会遇到一个场景,需要编写自定义代码来处理某种事件或性能统计
实现org.cloudifysource.dsl.Plugin接口的java类加上下面描述的一个或多个额外接口组成一个插件
定义在配方文件的POJO类和参数映射
当USM加载时,它会根据接口的具体实现来执行插件
Jar文件包含已编译的插件加上一些额外的类和资源,这些文件在打包前应该放在usmlib目录下的配方文件夹中,当PU部署完成,这些文件将会被放置在PU的lib目录下来保证USM的类加载器来加载
准备IDE工程
建立一个USM插件需要建立一个Java项目,并添加以下jar文件到项目的类路径中:
- 所有GigaSpaces 安装目录下的lib/required下的所有jar文件
-
所有GigaSpaces 安装目录下的lib/platform/usm下的所有jar文件
新建一个类来实现org.cloudifysource.dsl.Plugin接口.,这个是被配方中引用的插件类
你的插件类往往需要实现至少一个其他的接口
查看所有接口请见 USM部分
一旦你完成插件开发并编译打包为一个jar文件,这个jar文件需要放在对应服务文件夹的usmlib目录下
插件接口
/***********
* All USM plugins implementation should implement this interface.
* @author barakme
*
*/
public interface Plugin {
/******************
* Setter for the Service Context of the current service.
* @param context the service context.
*/
public void setServiceContext(ServiceContext context);
/****************
* Setter for the plugin parameters, as defined in the Recipe file.
* @param config the plugin parameters.
*/
public void setConfig(Map<String, Object> config);
}
这些方法是插件来调用ServiceContext API来访问上下文环境和声明在配方里的配置文件
注意调用顺序:
setServiceContext最先被USM调用并注入到ServiceContext实例中.,然后USM调用setConfig 注入一个来自配方的配置Map对象例子
这是一个tomcat配方中的插件的声明例子使用JMX插件来读取JMX属性并发布属性
plugins([
plugin {
name "jmx"
className "org.cloudifysource.usm.jmx.JmxMonitor"
config([
"Current Http Threads Busy": [
"Catalina:type=ThreadPool,name=\"http-bio-80\"",
"currentThreadsBusy"
],
"Current Http Threads count": [
"Catalina:type=ThreadPool,name=\"http-bio-80\"",
"currentThreadCount"
],
"Backlog": [
"Catalina:type=ProtocolHandler,port=80",
"backlog"
],
port : 9999
])
}
])
注意:例子的插件的实现类org.cloudifysource.usm.jmx.JmxMonitor, 它接收到一个初始化配置的键值对,属性名称与JMXObjectName 相关属性的映射以及用于访问JMX代理的JMX端口
内置插件
JMX Monitor
这个插件通过连接JMX-RMI来收集JMX属性,然后暴露给GigaSpaces服务监控器。使用GigaSpaces Admin API收集数据,所有的值必须为数值型的
实现类: org.cloudifysource.usm.jmx.JmxMonitor
插件配置值
Key
Type
Description
port
int
插件需要连接的端口号
host
String
插件需要连接的主机地址,默认为127.0.0.1
username
String
插件需要连接的JMX用户名,这个值只需要JMX授权
password
String
插件需要连接的JMX密码,这个值只需要JMX授权
*
List
两条记录的列表,第一个是JMX的'ObjectName',第二个是JMX属性名。配置的关键是暴露给服务网格的监控器的名称
Sample
plugin {
name "jmxMonitor"
className "org.cloudifysource.usm.jmx.JmxMonitor"
config([
"Current Http Threads Busy":[
"jboss.web:type=ThreadPool,name=http-127.0.0.1-8080",
"currentThreadsBusy"
],
"Current Http Threads Count":[
"jboss.web:type=ThreadPool,name=http-127.0.0.1-8080",
"currentThreadCount"
],
"Server Thread Count":[
"jboss.system:type=ServerInfo",
"ActiveThreadCount"
],
"Free Memory":[
"jboss.system:type=ServerInfo",
"FreeMemory"
],
port: 1090
])
}
JMX Details
和JMX监控器相似,它暴露JMX属性给GigaSpaces服务监控器,使用GigaSpaces Admin API收集数据
实现类: org.cloudifysource.usm.jmx.JmxDetails
插件配置值
和JMX监控器一样
plugin {
name "jmxDetails"
className "org.cloudifysource.usm.jmx.JmxDetails"
config([
"Current Http Threads Busy":[
"jboss.web:type=ThreadPool,name=http-127.0.0.1-8080",
"currentThreadsBusy"
],
"Current Http Threads Count":[
"jboss.web:type=ThreadPool,name=http-127.0.0.1-8080",
"currentThreadCount"
],
"Server Thread Count":[
"jboss.system:type=ServerInfo",
"ActiveThreadCount"
],
"Free Memory":[
"jboss.system:type=ServerInfo",
"FreeMemory"
],
port: 1090
])
}
端口存活探测器
端口存活测器等待本机端口打开,对有些使用TCP端口的服务进程比较有用
这个插件在进程启动前检测端口号 (例如它实现了org.cloudifysource.usm.liveness.LivenessDetector 和org.cloudifysource.usm.events.PreStartListener两个接口)
实现类: org.cloudifysource.usm.liveness.PortLivenessDetector
插件配置值
Key
Type
Default Value
Description
Port
List
插件需要连接的端口列表
Host
String
127.0.0.1
插件需要连接的主机地址
TimeoutInSeconds
int
60
插件检测启动失败秒数
plugin {
name "portLiveness"
className "org.cloudifysource.usm.liveness.PortLivenessDetector"
config ([
"Port" : [8080],
"TimeoutInSeconds" : 15,
"Host" : "127.0.0.1"
])
}
文件存活探测器
探测插件用正则表达式来匹配一个文本文件
插件配置值
Key
Type
Default Value
Description
regularExpression
String
正则表达式
FilePath
String
文件的绝对路径
TimeoutInSeconds
int
60
插件检测启动失败秒数
Sample
plugin {
name "fileLiveness"
className "org.cloudifysource.usm.liveness.FileLivenessDetector"
config ([
"FilePath" : System.getProperty("java.io.tmpdir") + "/groovyLog.log",
"TimeoutInSeconds" : 30,
"regularExpression" : "Hello_World"
])
}