版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27232757/article/details/74565095
package example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import mesosphere.marathon.client.Marathon;
import mesosphere.marathon.client.MarathonClient;
import mesosphere.marathon.client.model.v2.App;
import mesosphere.marathon.client.model.v2.Deployment;
import mesosphere.marathon.client.model.v2.GetAppResponse;
import mesosphere.marathon.client.model.v2.GetAppTasksResponse;
import mesosphere.marathon.client.model.v2.GetAppsResponse;
import mesosphere.marathon.client.model.v2.HealthCheck;
import mesosphere.marathon.client.model.v2.Task;
import mesosphere.marathon.client.utils.MarathonException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
public class Example {
Marathon marathon = null;
/**
* 创建Marathon的连接
*/
@Before
public void createClient(){
marathon = MarathonClient.getInstance("http://172.16.12.127:8080");
}
/**
* 在Marathon上面创建一个应用
* @throws MarathonException
*/
@Test
public void createAPP() throws MarathonException {
System.out.println("START");
for (int i = 0; i < 1; i++) {
App app = new App();
app.setId("/test/task1" + i);
app.setCmd("chmod +x maraStart.sh && ./maraStart.sh test 172.16.12.127 /home/test/taskmanager-99413.tar.gz taskmanager-test.tar.gz ftppw1234 $PORT0");
app.setCpus(0.5);
app.setMem(10.0);
List<Integer> ports = new ArrayList<Integer>();
ports.add(0);
app.setPorts(ports);
app.setInstances(1);
List<HealthCheck> healChecks = new ArrayList<HealthCheck>();
HealthCheck healCheck = new HealthCheck();
healCheck.setProtocol("TCP");
healCheck.setPortIndex(0);
healCheck.setGracePeriodSeconds(300);
healCheck.setIntervalSeconds(60);
healCheck.setTimeoutSeconds(20);
healCheck.setMaxConsecutiveFailures(0);
healChecks.add(healCheck);
app.setHealthChecks(healChecks);
// List<String> list = new ArrayList<String>();
// list.add("hostname");
// list.add("LIKE");
// list.add("XCLOUD3[69]");
// List<List<String>> constraints = new ArrayList<List<String>>();
// constraints.add(list);
// System.out.println(constraints);
// app.setConstraints(constraints);
List<String> uris = new ArrayList<String>();
uris.add("ftp://test:[email protected]/modifyConfig.py");
uris.add("ftp://test:[email protected]/maraStart.sh");
app.setUris(uris);
System.out.println(app);
marathon.createApp(app);
}
System.out.println("success");
}
/**
* 获取创建的多个APP,输出APP的信息
* @throws MarathonException
*/
@Test
public void testAPPS() throws MarathonException {
GetAppsResponse getAppsResponse = marathon.getApps();
System.out.println("GetAppsResponse: " + getAppsResponse);
List<App> listAPP = getAppsResponse.getApps();
for (App app : listAPP) {
System.out.println("APP: " + app);
}
}
/**
* 查看配置的CPU和MEM
* @throws MarathonException
*/
@Test
public void testAPP() throws MarathonException {
System.out.println("--------------");
GetAppResponse getAppResponse = marathon.getApp("/likexin/start");
App app = getAppResponse.getApp();
System.out.println("CPUS: " + app.getCpus());
System.out.println("MEM: " + app.getMem());
}
/**
* 查看正在部署的APP
* @throws MarathonException
*/
@Test
public void testDeployment() throws MarathonException {
List<Deployment> list = marathon.getDeployments();
System.out.println(list);
}
/**
* 根据ID获取task
* @throws MarathonException
*/
@Test
public void testTask() throws MarathonException {
GetAppTasksResponse getAppTaskResponse = marathon.getAppTasks("/likexin/task1");
Collection<Task> list = getAppTaskResponse.getTasks();
System.out.println(list);
}
/**
* 根据ID获取APP
* @throws MarathonException
*/
@Test
public void testStatus() throws MarathonException {
GetAppResponse getAppResponse = marathon.getApp("task1");
App app = getAppResponse.getApp();
}
/**
* 删除APP
* @throws MarathonException
*/
@Test
public void testDeleteAPP() throws MarathonException {
System.out.println("START");
GetAppsResponse getAppsResponse = marathon.getApps();
System.out.println("GetAppsResponse: " + getAppsResponse);
List<App> listAPP = getAppsResponse.getApps();
for (App app : listAPP) {
marathon.deleteApp(app.getId());
}
System.out.println("SUCCESS");
}
/**
* 获取任务的ID
* @throws MarathonException
*/
@Test
public void testMesos() throws MarathonException {
GetAppResponse getAppResponse = marathon.getApp("/likexin/start");
App app = getAppResponse.getApp();
Collection<Task> tasks = app.getTasks();
for (Task task : tasks) {
String taskID = task.getId();
System.out.println(taskID);
}
}
/**
* 获取mesos监控界面上面显示的CPU和内存的使用率,通过HTTP来拿到数据的
* @throws InterruptedException
*/
@Test
public void tesMonitor() throws InterruptedException {
while(true){
JSONObject jsonObject0 = getJSONObject("likexin_task3.5103f40f-54ca-11e7-9fc1-002590300172");
double cpus_limit = jsonObject0.getDouble("cpus_limit");
double cpus_system_time_secs0 = jsonObject0.getDouble("cpus_system_time_secs");
double cpus_user_time_secs0 = jsonObject0.getDouble("cpus_user_time_secs");
double timestamp0 = jsonObject0.getDouble("timestamp");
Thread.sleep(1000);
JSONObject jsonObject1 = getJSONObject("likexin_task3.5103f40f-54ca-11e7-9fc1-002590300172");
double cpus_system_time_secs1 = jsonObject1.getDouble("cpus_system_time_secs");
double cpus_user_time_secs1 = jsonObject1.getDouble("cpus_user_time_secs");
double timestamp1 = jsonObject1.getDouble("timestamp");
double cpus_time_total0 = cpus_system_time_secs0 + cpus_user_time_secs0;
double cpus_time_total1 = cpus_system_time_secs1 + cpus_user_time_secs1;
double cpus_time_delta = cpus_time_total1 - cpus_time_total0;
double timestamp_delta = timestamp1 - timestamp0;
double percent = cpus_time_delta / timestamp_delta;
System.out.println("percent : " +percent);
System.out.println("likexin_task2.0d959f48-54c1-11e7-9fc1-002590300172 : " +cpus_limit*percent);
}
}
/**
* 将界面返回的JSON串转换为对象
* @param executor_id
* @return
*/
public JSONObject getJSONObject(String executor_id) {
String result = sendPost("http://172.16.12.126:5051/monitor/statistics.json","");
JSONArray jsonArr = JSONArray.fromObject(result);
for (int i = 0; i < jsonArr.size(); i++) {
JSONObject jsonObject = jsonArr.getJSONObject(i);
if(executor_id.trim().equals(jsonObject.get("executor_id"))){
String statistics = jsonObject.getString("statistics");
JSONObject statObject = JSONObject.fromObject(statistics);
return statObject;
}
}
return null;
}
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
@Test
public void testJSON() {
String jsonStr = "{name:'likexin',age:'hah',money:12.23}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
double cpus_limit = jsonObject.getDouble("money");
System.out.println(cpus_limit);
}
}
注意:在Marathon上面运行的APP一定是服务,能长时间运行