调用marathon rest API

郝昊天
2023-12-01

步骤:

1、访问marathon下载源码地址为 https://github.com/mesosphere/marathon-client

2、修改Marathon类 此处多添加一个GET ping 的方法

===============================================================================================

package mesosphere.marathon.client;


import java.util.List;


import javax.inject.Named;


import mesosphere.marathon.client.model.v2.App;
import mesosphere.marathon.client.model.v2.DeleteAppTaskResponse;
import mesosphere.marathon.client.model.v2.DeleteAppTasksResponse;
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.GetQueuesResponse;
import mesosphere.marathon.client.model.v2.GetServerInfoResponse;
import mesosphere.marathon.client.model.v2.GetTasksResponse;
import mesosphere.marathon.client.model.v2.Group;
import mesosphere.marathon.client.model.v2.Result;
import mesosphere.marathon.client.utils.MarathonException;
import feign.RequestLine;


public interface Marathon {
//ping IP
@RequestLine("GET /ping")
void ping();
    // Apps
@RequestLine("GET /v2/apps")
GetAppsResponse getApps();


@RequestLine("GET /v2/apps/{id}")
GetAppResponse getApp(@Named("id") String id) throws MarathonException;


@RequestLine("GET /v2/apps/{id}/tasks")
GetAppTasksResponse getAppTasks(@Named("id") String id);

@RequestLine("POST /v2/apps/{app_id}/restart")
Result reStartApp(@Named("app_id") String app_id) throws MarathonException;


@RequestLine("GET /v2/tasks")
GetTasksResponse getTasks();


@RequestLine("POST /v2/apps")
App createApp(App app) throws MarathonException;


@RequestLine("PUT /v2/apps/{app_id}")
void updateApp(@Named("app_id") String appId, App app) throws MarathonException;

@RequestLine("PUT /v2/apps/{app_id}?force=true")
void forceUpdateApp(@Named("app_id") String appId, App app) throws MarathonException;



@RequestLine("DELETE /v2/apps/{id}")
Result deleteApp(@Named("id") String id) throws MarathonException;

@RequestLine("DELETE /v2/apps/{id}?force=true")
Result forceDeleteApp(@Named("id") String id) throws MarathonException;


@RequestLine("DELETE /v2/apps/{app_id}/tasks?host={host}&scale={scale}")
DeleteAppTasksResponse deleteAppTasks(@Named("app_id") String appId,
@Named("host") String host, @Named("scale") String scale);


@RequestLine("DELETE /v2/apps/{app_id}/tasks/{task_id}?scale={scale}")
DeleteAppTaskResponse deleteAppTask(@Named("app_id") String appId,
@Named("task_id") String taskId, @Named("scale") String scale);


    // Groups
@RequestLine("POST /v2/groups")
Result createGroup(Group group) throws MarathonException;


@RequestLine("PUT /v2/groups/{id}")
Result updateGroup(@Named("id") String id, Group group) throws MarathonException;

@RequestLine("PUT /v2/groups/{id}?force=true")
Result forceUpdateGroup(@Named("id") String id, Group group) throws MarathonException;

@RequestLine("DELETE /v2/groups/{id}")
Result deleteGroup(@Named("id") String id) throws MarathonException;

@RequestLine("DELETE /v2/groups/{id}?force=true")
Result forceDeleteGroup(@Named("id") String id) throws MarathonException;


@RequestLine("GET /v2/groups/{id}")
Group getGroup(@Named("id") String id) throws MarathonException;


    // Tasks


    // Deployments
@RequestLine("GET /v2/deployments")
List<Deployment> getDeployments();

@RequestLine("DELETE /v2/deployments/{deploymentId}")
void cancelDeploymentAndRollback(@Named("deploymentId") String id);

@RequestLine("DELETE /v2/deployments/{deploymentId}?force=true")
void cancelDeployment(@Named("deploymentId") String id);


    // Event Subscriptions


    // Queue
@RequestLine("GET /v2/queue")
GetQueuesResponse getQueues() throws MarathonException;
    // Server Info
    @RequestLine("GET /v2/info")
    GetServerInfoResponse getServerInfo();


    // Miscellaneous
}

=============================================================================================

3、修改MarathonClient类 重载方法getInstance();

package mesosphere.marathon.client;



import mesosphere.marathon.client.utils.MarathonException;
import mesosphere.marathon.client.utils.ModelUtils;
import feign.Feign;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Response;
import feign.codec.ErrorDecoder;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import java.util.List;
import java.util.Map;


public class MarathonClient {
static class MarathonHeadersInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.header("Accept", "application/json");
template.header("Content-Type", "application/json");
}
}

static class MarathonErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
return new MarathonException(response.status(), response.reason());
}
}
public static Marathon getInstance(String endpoint) {
GsonDecoder decoder = new GsonDecoder(ModelUtils.GSON);
GsonEncoder encoder = new GsonEncoder(ModelUtils.GSON);
return Feign.builder().encoder(encoder).decoder(decoder)
.errorDecoder(new MarathonErrorDecoder())
.requestInterceptor(new MarathonHeadersInterceptor())
.target(Marathon.class, endpoint);
/*marathon.ping();
return marathon;*/
}

public static Marathon getInstance(String area,Map<String,List<String>> urlConfs) {
Marathon marathon = null;
GsonDecoder decoder = new GsonDecoder(ModelUtils.GSON);
GsonEncoder encoder = new GsonEncoder(ModelUtils.GSON);
//String []marathonUrl=endpoint.split(",");

List<String> urls=urlConfs.get(area);
for(int i=0;i<urls.size();i++){
//初始化实例
marathon= Feign.builder().encoder(encoder).decoder(decoder)
.errorDecoder(new MarathonErrorDecoder())
.requestInterceptor(new MarathonHeadersInterceptor())
.target(Marathon.class, urls.get(i));
try{
   //测试所选择的url是否可以ping通
marathon.ping();
return marathon;
}catch(Exception e){
System.out.println("连接url="+urls.get(i)+"异常!");
System.out.println("错误信息:"+e.getMessage());
continue;
}
}

System.out.println("连接marathon集群异常!");
return null;
}

public static String getEnableUrl(String area,Map<String,List<String>> urlConfs){
String url="";
Marathon marathon = null;
GsonDecoder decoder = new GsonDecoder(ModelUtils.GSON);
GsonEncoder encoder = new GsonEncoder(ModelUtils.GSON);
//String []marathonUrl=endpoint.split(",");
List<String> urls=urlConfs.get(area);

for(int i=0;i<urls.size();i++){
url=urls.get(i);
//初始化实例
marathon= Feign.builder().encoder(encoder).decoder(decoder)
.errorDecoder(new MarathonErrorDecoder())
.requestInterceptor(new MarathonHeadersInterceptor())
.target(Marathon.class, url);
try{
   //测试所选择的url是否可以ping通
marathon.ping();
return url;
}catch(Exception e){
System.out.println("连接url="+url+"异常!");
System.out.println("错误信息:"+e.getMessage());
continue;
}
}
return null;
}

}

4、test

public class Test {
public static void main(String[] args){

String url = "http://ip:端口号";
Map<String,List<String>> urlConfs = new HashMap<String, List<String>>();
List<String> list = new ArrayList<String>();
list.add(url);
urlConfs.put("1", list);
Marathon marathon = MarathonClient.getInstance("1", urlConfs);
List<Deployment> list3 = marathon.getDeployments();
System.out.println(ModelUtils.toString(list3));
for(Deployment dep : list3){
System.out.println(dep.toString());
marathon.cancelDeployment(dep.getId());
}

}

5、所需jar包 

feign.jar和inject.jar

文档中有可下载


 类似资料: