1、选择 Rest API
2、保存并获取应用key
3、查看key
4、创建Maven项目
<?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>com.alex.azkaban</groupId>
<artifactId>azkaban_phone</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
5、添加依赖
<?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>com.alex.azkaban</groupId>
<artifactId>azkaban_phone</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.azkaban</groupId>
<artifactId>az-core</artifactId>
<version>3.84.4</version>
<scope>system</scope>
<systemPath>F:/jar/az-core-3.84.4.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.azkaban</groupId>
<artifactId>azkaban-common</artifactId>
<version>3.84.4</version>
<scope>system</scope>
<systemPath>F:/jar/azkaban-common-3.84.4.jar</systemPath>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<!--日志 start-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!--日志end-->
</dependencies>
<build>
<plugins>
<!-- java代码编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- maven打包插件,以maven-assembly-plugin方式打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.alex.json.test.FastJsonUtilTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- this is used for not append id to the jar name -->
<appendAssemblyId>true</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6、HttpUtils
package com.alex.azkaban.phone;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
/**
* @author 黑猴子的家
*/
public class HttpUtils {
public static void get(String url) throws Exception {
//todo 1、创建HttpClient
HttpClient httpClient = new HttpClient();
//todo 2、创建Method方法
GetMethod getMethod = new GetMethod(url);
//todo 3、发起请求
int code = httpClient.executeMethod(getMethod);
//todo 4、判断请求是否成功
if (code == 200) {
//todo 5、打印结果
System.out.println(getMethod.getResponseBodyAsString());
}
}
public static void post(String url, String content) throws Exception {
// todo 1、创建HttpClient
HttpClient httpClient = new HttpClient();
// todo 2、创建Method
PostMethod method = new PostMethod(url);
// todo 3、设置body参数
StringRequestEntity entity = new StringRequestEntity(content, "application/json", "utf-8");
method.setRequestEntity(entity);
// todo 4、发起请求
int code = httpClient.executeMethod(method);
// todo 5、判断请求是否成功
if(code==200){
System.out.println(method.getResponseBodyAsString());
}
}
}
7、PhoneAlerter
package com.alex.azkaban.phone;
import azkaban.alert.Alerter;
import azkaban.executor.ExecutableFlow;
import azkaban.executor.Executor;
import azkaban.executor.ExecutorManagerException;
import azkaban.sla.SlaOption;
import azkaban.utils.Props;
import java.util.List;
/**
* @author 黑猴子的家
*/
public class PhoneAlerter implements Alerter {
private String url;
private String content = "";
private String app_key;
//http://api.aiops.com/alert/api/event?app=a2db6c6083c144759e6fe8c3acd7781e&eventId=xxx&eventType=trigger&alarmName=xxx&priority=2
//http://api.aiops.com/alert/api/event?app=%s&eventId=xxx&eventType=trigger&alarmName=xxx&priority=2
public PhoneAlerter(Props props){
url = props.getString("my.url");
app_key = props.getString("my.app.key");
}
//todo 成功发通知调用
@Override
public void alertOnSuccess(ExecutableFlow executableFlow) throws Exception {
HttpUtils.post(String.format(url,app_key), content);
}
//todo 失败发通知调用
@Override
public void alertOnError(ExecutableFlow executableFlow, String... strings) throws Exception {
}
//todo 第一次失败的时候调用
@Override
public void alertOnFirstError(ExecutableFlow executableFlow) throws Exception {
}
@Override
public void alertOnSla(SlaOption slaOption, String s) throws Exception {
}
@Override
public void alertOnFailedUpdate(Executor executor, List<ExecutableFlow> list, ExecutorManagerException e) {
}
}
8、配置azkaban
[alex@hadoop102 azkaban]$ cd azkaban-web-server/
[alex@hadoop102 azkaban-web-server]$ mkdir -p plugins/alerter/phone-alerter
[alex@hadoop102 azkaban-web-server]$ cd plugins/alerter/phone-alerter/
[alex@hadoop102 phone-alerter]$ mkdir lib
[alex@hadoop102 phone-alerter]$ mkdir conf
[alex@hadoop102 phone-alerter]$ vim conf/plugin.properties
#name一定要设置email,用以覆盖默认的邮件报警
alerter.name=email
alerter.external.classpaths=lib
alerter.class=com.alex.azkaban.phone.PhoneAlerter
#这两个参数和你使用的Alerter API 有关系
my.app.key=a2db6c6083c144759e6fe8c3acd7781e
my.url=http://api.aiops.com/alert/api/event?app=%s&eventId=xxx&eventType=trigger&alarmName=xxx&priority=2
9、重启web 服务
[alex@hadoop102 azkaban-web-server]$ bin/shutdown-web.sh
[alex@hadoop102 azkaban-web-server]$ bin/start-web.sh
网友评论