Skip to content
鼓励作者:欢迎打赏犒劳

阿里云云效

官方文档:https://help.aliyun.com/document_detail/52223.html

云效工作台:https://devops.aliyun.com/workbench

云效Codeup:https://codeup.aliyun.com/

云效Flow:https://flow.aliyun.com/

云效Flow

就是部署项目用的。

1、需要先在云效Codeup创建项目,写好代码,上传上去,git的账号密码是在。代码管理页面,点击右上角-个人设置-HTTPS密码

2、工作流会在ecs服务器上将代码打包到【下载路径】,/home/admin/app/my-api.tgz 这个路径下,然后会再执行部署脚本脚本 ,将tgz压缩包解压到指定位置,然后执行项目的deploy.sh启动脚本

ali-flow

部署脚本如下: https://help.aliyun.com/document_detail/153848.html

shell
mkdir -p /home/admin/{你的应用名称}
tar zxvf /home/admin/app/{你的应用名称}.tgz -C /home/admin/{你的应用名称}/
sh /home/admin/{你的应用名称}/classes/deploy.sh restart

正常情况下,你的项目中应该有一个部署脚本,然后还需要一个健康检查的脚本

deploy.sh

shell
#!/bin/bash

# 修改APP_NAME为云效上的应用名   注意:这个要修改为你项目pom的finalName名字
APP_NAME=application


PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT=20    # 等待应用启动的时间
APP_PORT=8080          # 应用端口
HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}/${APP_NAME}/check  # 应用健康检查URL
APP_HOME=/home/admin/${APP_NAME} # 从package.tgz中解压出来的jar包放到这个目录下
JAR_NAME=${APP_HOME}/${APP_NAME}.jar # jar包的名字
JAVA_OUT=${APP_HOME}/logs/start.log  #应用的启动日志

# 创建出相关目录
mkdir -p ${APP_HOME}
mkdir -p ${APP_HOME}/logs
usage() {
    echo "Usage: $PROG_NAME {start|stop|restart}"
    exit 2
}

health_check() {
    exptime=0
    echo "checking ${HEALTH_CHECK_URL}"
    while true
        do
            status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code}  ${HEALTH_CHECK_URL}`
            if [ "$?" != "0" ]; then
               echo -n -e "\rapplication not started"
            else
                echo "code is $status_code"
                if [ "$status_code" == "200" ];then
                    break
                fi
            fi
            sleep 1
            ((exptime++))

            echo -e "\rWait app to pass health check: $exptime..."

            if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
                echo 'app start failed'
               exit 1
            fi
        done
    echo "check ${HEALTH_CHECK_URL} success"
}
start_application() {
    echo "starting java process"
    nohup java -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 &
    echo "started java process"
}

stop_application() {
   checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
   
   if [[ ! $checkjavapid ]];then
      echo -e "\rno java process"
      return
   fi

   echo "stop java process"
   times=60
   for e in $(seq 60)
   do
        sleep 1
        COSTTIME=$(($times - $e ))
        checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
        if [[ $checkjavapid ]];then
            kill -9 $checkjavapid
            echo -e  "\r        -- stopping java lasts `expr $COSTTIME` seconds."
        else
            echo -e "\rjava process has exited"
            break;
        fi
   done
   echo ""
}
start() {
    start_application
    health_check
}
stop() {
    stop_application
}
case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    *)
        usage
    ;;
esac

application.properties

properties
server.servlet.context-path=/my-api
server.port=8080

TestController.java

java
import com.example.bootdemo.common.vo.ResultInfoVO;
import com.example.bootdemo.utils.ResultInfoVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@Slf4j
@RestController
@RequestMapping("")
public class TestController {

    @RequestMapping("/check")
    public String index() {
        return "OK";
    }
}

上次更新:

如有转载或 CV 的请标注本站原文地址