## 上线
//推送到openapi服务,接口地址: 微服务zip http://ip:port/context-path/publish , 推送aimodel http://ip:port/context-path/publishmodel
OkHttpClient requestClient = new OkHttpClient();
MediaType mediaType = MediaType.parse(“application/octet-stream”);
File file = new File(filePath);
if (file == null || !file.exists()) {
logger.error(“微服务文件不存在,uuid=” + microUuid);
throw new IllegalArgumentException(“微服务文件不存在”);
}
RequestBody fileBody = RequestBody.create(mediaType, file);
RequestBody requestBody
= new MultipartBody.Builder().addFormDataPart(“file”,fileName,fileBody).build();
Request request = new Request.Builder().url(clientUrl).post(requestBody).build();
//解析接口返回结果
Response response = requestClient.newCall(request).execute();
ResponseBody body = response.body();
if (response.isSuccessful()) {
String result = body.string();
JSONObject data = JSON.parseObject(result);
Integer code = data.getInteger(“code”);
if (code == null || code != 0) {
logger.info(String.format(“微服务上线失败,客户端响应异常,微服务uuid=%s, 客户端uuid=%s”, microUuid, clientUuid));
body.close();
throw new IllegalArgumentException(“微服务上线失败,客户端响应异常”);
}
body.close();
//更新微服务状态
micro.setUpDownLine(Micro.LINE_ON);
microMapper.update(micro);
return Result.newSuccess();
} else {
logger.error(String.format(“客户端请求失败,地址:%s, 异常:%s”, client.getClientAddress(), response.message()));
throw new IllegalArgumentException(“客户端请求失败”);
}
## 下线
List offResults = new ArrayList<>(clients.size());//最终结果
List successClients = new ArrayList<>(clients.size());//下线成功的客户端
boolean isAllSuccess = true;
String apiPath ;
RequestBody requestBody;
if (Constants.MICRO_TYPE_MODEL.equals(typeName)) {
//模型微服务下线,参数为模型的名称 (与模型微服务名称相同)
apiPath = “/shutmodel”;
AiModel aiModel=aiModelMapper.selectModelByUuid(micro.getPackageUuid());
if (aiModel == null || StringUtil.isBlank(aiModel.getJarName())) {
logger.error(“Ai模型查询结果异常,uuid=” + micro.getPackageUuid());
throw new IllegalArgumentException(“Ai模型不存在”);
}
requestBody =
new FormBody.Builder().add(“modelname”,aiModel.getJarName()).build();
} else {
//普通微服务下线,参数为微服务id
apiPath = “/shutdown”;
requestBody =
new FormBody.Builder() .add(“id”, String.valueOf(micro.getId())).build();
}
for (Client client : clients) {
String clientUrl = client.getClientAddress();
if (!clientUrl.endsWith("/")) {
clientUrl += “/”;
}
clientUrl += apiPath; //url追加接口路径
StringBuilder builder = new StringBuilder(“在客户端:”);
builder.append(client.getClientName());
builder.append(" (").append(client.getClientAddress()).append(") “);
try {
OkHttpClient requestClient = new OkHttpClient();
Request request =
new Request.Builder().url(clientUrl).post(requestBody).build();
//解析接口返回结果
Response response = requestClient.newCall(request).execute();
ResponseBody body = response.body();
if (response.isSuccessful()) {
String result = body.string();
JSONObject data = JSON.parseObject(result);
Integer code = data.getInteger(“code”);
if (code == null || code != 0) {
logger.info(String.format(“微服务下线失败,客户端响应异常,微服务 uuid=%s”, microUuid));
builder.append(” - 下线失败,客户端响应异常");
isAllSuccess = false;
} else {
builder.append(" - 下线成功");
successClients.add(client.getUuid());
}
body.close();
} else {
logger.error(String.format(“客户端请求失败,地址:%s, 异常:%s”, client.getClientAddress(), response.message()));
builder.append(" - 下线失败");
isAllSuccess = false;
}