1、启动容器
/**
* 启动容器
* @param name 容器名称或Id
* @return
*/
public static ObjectVo startDocker(String name){
ObjectVo vo = new ObjectVo();
if (StringUtils.isBlank(name)){
vo.setCode(0);
vo.setMsg("参数不能为空");
vo.setObject(null);
return vo;
}
String url = origin + "/containers/" + name + "/start";
try {
Response response = OKHttpUtil.doSyncPost(url, null);
if (null != response) {
vo.setCode(response.code());
vo.setMsg(response.message());
vo.setObject(response.body().string());
}else{
vo.setCode(0);
vo.setMsg("请求出错,没有返回结果");
}
} catch (IOException e) {
// TODO Auto-generated catch block
vo.setCode(0);
vo.setMsg("请求出错,没有返回结果");
vo.setObject(null);
e.printStackTrace();
}
return vo;
}
状态码,ObjectVo.code可与其对比
204 – no error
304 – container already started
404 – no such container
500 – server error
2、ObjectVo代码
@Data
public class ObjectVo {
private Integer code;
private String msg;
private Object object;
}
3、http请求
/**
* 同步实现POST方式请求
* @param url
* @param params
* @return
* @throws IOException
*/
public static Response doSyncPost(String url, Map<String, String> params) throws IOException{
if (StringUtils.isBlank(url)) {
return null;
}
FormBody.Builder builder = new FormBody.Builder();
Request request = null;
if (null != params && params.size() > 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
String value;
if (null == entry.getValue()) {
value = "";
}else{
value = entry.getValue();
}
builder.add(entry.getKey(), value);
}
RequestBody requestBody = builder.build();
request = new Request.Builder().url(url).post(requestBody).build();
}else{
request = new Request.Builder().url(url).post(RequestBody.create(null, "")).build();
}
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.build();
Call call = client.newCall(request);
Response response = call.execute();
return response;
}
网友评论