Merge branch 'master' into docker_package

This commit is contained in:
wangliwen 2022-11-07 15:12:38 +08:00
commit 69bf7366c9
4 changed files with 119 additions and 19 deletions

View File

@ -3,40 +3,34 @@ package io.renren.common.controller;
import io.renren.common.annotation.LogOperation;
import io.renren.common.utils.Result;
import io.renren.common.utils.UpdateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Api(tags = "管理员后台")
@RestController
@RequestMapping("/admin")
public class AdminController {
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")
@ApiOperation("进行后台程序更新")
@LogOperation("进行后台程序更新")
public Result<String> update(String updateFile) {
public Result<String> update(String updateFile, String active) {
return new Result<String>().ok("");
}

View File

@ -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));
}
}

View File

@ -1,8 +1,9 @@
#! /bin/bash
#chkconfig: 2345 85 15
#description:auto_run
#processname:zf
#processname:ucs
#JAR根位置
PROFILES_ACTIVE=$2
JAR_ROOT=$(pwd)
#JAR位置
JAR_PATH="$JAR_ROOT"/renren-admin.jar
@ -10,10 +11,15 @@ JAR_PATH="$JAR_ROOT"/renren-admin.jar
#LOG位置
LOG_PATH=/dev/null
# 参数校验
if [ -z "$PROFILES_ACTIVE" ]; then
PROFILES_ACTIVE = "prod"
fi
#开始方法
start() {
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."
}

View File

@ -9,6 +9,7 @@
APP_DIR=$1
UPDATE_PACKAGE=$2
APP_NAME=$3
PROFILES_ACTIVE=$4
# 更新日志写在项目根目录中
LOG_FILE="${APP_DIR}/update.log"
@ -28,6 +29,11 @@ if [ -z "$APP_DIR" ] || [ -z "$UPDATE_PACKAGE" ] || [ -z "$APP_NAME" ]; then
exit 1
fi
# 参数校验
if [ -z "$PROFILES_ACTIVE" ]; then
PROFILES_ACTIVE = "prod"
fi
if [ "$APP_DIR" = "/" ]; then
echo "APP_DIR 不能为 /"
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