更新接口初步实现
This commit is contained in:
parent
37bbc5fb7b
commit
418c0ce764
|
@ -3,40 +3,34 @@ package io.renren.common.controller;
|
||||||
|
|
||||||
import io.renren.common.annotation.LogOperation;
|
import io.renren.common.annotation.LogOperation;
|
||||||
import io.renren.common.utils.Result;
|
import io.renren.common.utils.Result;
|
||||||
|
import io.renren.common.utils.UpdateUtil;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import okhttp3.ConnectionPool;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Api(tags = "管理员后台")
|
@Api(tags = "管理员后台")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/admin")
|
@RequestMapping("/admin")
|
||||||
public class AdminController {
|
public class AdminController {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
|
private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
|
||||||
private static final Integer CPUNUM = Runtime.getRuntime().availableProcessors();
|
|
||||||
private static final ExecutorService executor = Executors.newFixedThreadPool(CPUNUM);
|
|
||||||
|
|
||||||
private static final OkHttpClient client = new OkHttpClient().newBuilder()
|
|
||||||
.connectTimeout(1, TimeUnit.MINUTES)
|
|
||||||
.readTimeout(1, TimeUnit.MINUTES)
|
|
||||||
.connectionPool(new ConnectionPool(CPUNUM * 2, 2, TimeUnit.MINUTES))
|
|
||||||
.retryOnConnectionFailure(false)
|
|
||||||
.build(); // 下载更新包
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UpdateUtil updateUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param updateFile 更新包下载地址
|
||||||
|
* @param active 重启完成后的配置环境
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@PostMapping(value = "/update")
|
@PostMapping(value = "/update")
|
||||||
@ApiOperation("进行后台程序更新")
|
@ApiOperation("进行后台程序更新")
|
||||||
@LogOperation("进行后台程序更新")
|
@LogOperation("进行后台程序更新")
|
||||||
public Result<String> update(String updateFile) {
|
public Result<String> update(String updateFile, String active) {
|
||||||
return new Result<String>().ok("");
|
return new Result<String>().ok("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
package io.renren.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RuntimeUtil;
|
||||||
|
import okhttp3.ConnectionPool;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UpdateUtil {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(UpdateUtil.class);
|
||||||
|
|
||||||
|
private static final Integer CPUNUM = Runtime.getRuntime().availableProcessors();
|
||||||
|
private static final ExecutorService executor = Executors.newFixedThreadPool(CPUNUM);
|
||||||
|
private static final String pwd = System.getProperty("user.dir");
|
||||||
|
|
||||||
|
private static final OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(1, TimeUnit.MINUTES).readTimeout(2, TimeUnit.MINUTES).connectionPool(new ConnectionPool(CPUNUM * 2, 2, TimeUnit.MINUTES)).retryOnConnectionFailure(false).build(); // 下载更新包的工具
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行更新
|
||||||
|
*
|
||||||
|
* @param updateFile
|
||||||
|
* @param active
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Boolean update(String updateFile, String active) {
|
||||||
|
String updateFilePath = downloadUpdateFile(updateFile);
|
||||||
|
if (StringUtils.isEmpty(updateFile)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
execUpdate(updateFilePath, active);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载更新包
|
||||||
|
*
|
||||||
|
* @param updateFile
|
||||||
|
* @return 下载的更新包磁盘路径
|
||||||
|
*/
|
||||||
|
private String downloadUpdateFile(String updateFile) {
|
||||||
|
File folder = new File(pwd + File.separator + "/update"); // 更新包下载保存文件夹
|
||||||
|
File file = new File(folder, updateFile.substring(updateFile.lastIndexOf("/") + 1)); // 保存文件路径与名称
|
||||||
|
if (!folder.isDirectory()) {
|
||||||
|
folder.mkdirs();
|
||||||
|
}
|
||||||
|
Request request = new Request.Builder().url(updateFile).build();
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
try (InputStream is = response.body().byteStream(); FileOutputStream fos = new FileOutputStream(file)) {
|
||||||
|
long total = response.body().contentLength();
|
||||||
|
byte[] buf = new byte[4096];
|
||||||
|
int len;
|
||||||
|
long sum = 0;
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
fos.write(buf, 0, len);
|
||||||
|
sum += len;
|
||||||
|
int progress = (int) (sum * 1.0f / total * 100);
|
||||||
|
logger.info("更新包下载进度 {}% ", progress);
|
||||||
|
}
|
||||||
|
fos.flush();
|
||||||
|
return file.getAbsolutePath();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.error("下载更新包失败:{}", updateFile);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("下载更新包失败:" + updateFile, e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param updateFilePath 下载到本地的更新文件
|
||||||
|
* @param active 启动程序的配置文件
|
||||||
|
*/
|
||||||
|
private void execUpdate(String updateFilePath, String active) {
|
||||||
|
String cmd = String.format("cd %s && sh update.sh %s %s", pwd, pwd, updateFilePath, "renren-admin", active);
|
||||||
|
logger.info("执行指令:{}", cmd);
|
||||||
|
logger.info(RuntimeUtil.execForStr(cmd));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,9 @@
|
||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
#chkconfig: 2345 85 15
|
#chkconfig: 2345 85 15
|
||||||
#description:auto_run
|
#description:auto_run
|
||||||
#processname:zf
|
#processname:ucs
|
||||||
#JAR根位置
|
#JAR根位置
|
||||||
|
PROFILES_ACTIVE=$2
|
||||||
JAR_ROOT=$(pwd)
|
JAR_ROOT=$(pwd)
|
||||||
#JAR位置
|
#JAR位置
|
||||||
JAR_PATH="$JAR_ROOT"/renren-admin.jar
|
JAR_PATH="$JAR_ROOT"/renren-admin.jar
|
||||||
|
@ -10,10 +11,15 @@ JAR_PATH="$JAR_ROOT"/renren-admin.jar
|
||||||
#LOG位置
|
#LOG位置
|
||||||
LOG_PATH=/dev/null
|
LOG_PATH=/dev/null
|
||||||
|
|
||||||
|
# 参数校验
|
||||||
|
if [ -z "$PROFILES_ACTIVE" ]; then
|
||||||
|
PROFILES_ACTIVE = "prod"
|
||||||
|
fi
|
||||||
|
|
||||||
#开始方法
|
#开始方法
|
||||||
start() {
|
start() {
|
||||||
cd $JAR_ROOT
|
cd $JAR_ROOT
|
||||||
nohup java -Dfile.encoding=utf-8 -server -Xms256m -Xmx2g -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./ -jar $JAR_PATH >$LOG_PATH 2>&1 &
|
nohup java -Dfile.encoding=utf-8 -server -Xms256m -Xmx2g -XX:+HeapDumpOnOutOfMemoryError -Duser.timezone=GMT+08 -XX:HeapDumpPath=./ -jar --spring.profiles.active=$PROFILES_ACTIVE $JAR_PATH >$LOG_PATH 2>&1 &
|
||||||
echo "$JAR_PATH start success."
|
echo "$JAR_PATH start success."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
APP_DIR=$1
|
APP_DIR=$1
|
||||||
UPDATE_PACKAGE=$2
|
UPDATE_PACKAGE=$2
|
||||||
APP_NAME=$3
|
APP_NAME=$3
|
||||||
|
PROFILES_ACTIVE=$4
|
||||||
# 更新日志写在项目根目录中
|
# 更新日志写在项目根目录中
|
||||||
LOG_FILE="${APP_DIR}/update.log"
|
LOG_FILE="${APP_DIR}/update.log"
|
||||||
|
|
||||||
|
@ -28,6 +29,11 @@ if [ -z "$APP_DIR" ] || [ -z "$UPDATE_PACKAGE" ] || [ -z "$APP_NAME" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# 参数校验
|
||||||
|
if [ -z "$PROFILES_ACTIVE" ]; then
|
||||||
|
PROFILES_ACTIVE = "prod"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$APP_DIR" = "/" ]; then
|
if [ "$APP_DIR" = "/" ]; then
|
||||||
echo "APP_DIR 不能为 /"
|
echo "APP_DIR 不能为 /"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -113,5 +119,5 @@ rm -rf ${APP_DIR}/tmp/*
|
||||||
|
|
||||||
|
|
||||||
# 重启平台
|
# 重启平台
|
||||||
sh ${APP_DIR}/renren-admin.sh start
|
sh ${APP_DIR}/renren-admin.sh start ${$PROFILES_ACTIVE}
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in New Issue