• 介绍(已不再维护,建议使用HZERO 微服务应用开发平台->服务选配功能进行创建)

    本文介绍基于 Shell 脚本交互式快速创建项目,本质是基于 maven 骨架生成项目。 目前支持三种模式:

    准备工作

    创建新服务

    依赖服务创建

    批量创建HZERO服务

    服务部署

    服务快速启停

    #!/bin/bash
    
    readonly PRODUCT="hzero"
    # 服务列表
    readonly SERVICE=(
    $PRODUCT-register
    $PRODUCT-config
    $PRODUCT-gateway
    $PRODUCT-gateway-helper
    $PRODUCT-oauth
    $PRODUCT-swagger
    $PRODUCT-iam
    $PRODUCT-platform
    $PRODUCT-asgard
    $PRODUCT-file
    $PRODUCT-message
    )
    # 自定义参数
    readonly IP="192.168.***.***"
    readonly DATABASE_URL="jdbc:mysql://$IP:3306/{databaseName}?useSSL=false&useUnicode=true&characterEncoding=utf-8"
    readonly DATABASE_USERNAME="hzero"
    readonly DATABASE_PASSWORD="*****"
    readonly REDIS_HOST="$IP"
    readonly LOGGING_PATH="log"
    readonly LOGGING_LEVEL="info"
    readonly WAIT=30
    readonly JAVA_OPTS="-Xms512m -Xmx1024m"
    readonly API_PORT="8080"
    
    # 环境参数
    readonly E1="--EUREKA_DEFAULT_ZONE=http://$IP:8000/eureka"
    readonly E2="--SPRING_CLOUD_CONFIG_URI=http://$IP:8010"
    readonly E3="--CHOERODON_OAUTH_LOGIN_SUCCESS_URL=http://$IP/workplace"
    readonly E4="--HZERO_OAUTH_URL=http://$IP:$API_PORT/oauth/oauth/authorize"
    readonly E5="--HZERO_GATEWAY_DOMAIN=$IP:$API_PORT"
    # 数据源参数
    readonly D1="--SPRING_DATASOURCE_URL=$DATABASE_URL"
    readonly D2="--SPRING_DATASOURCE_USERNAME=$DATABASE_USERNAME"
    readonly D3="--SPRING_DATASOURCE_PASSWORD=$DATABASE_PASSWORD"
    readonly D4="--SPRING_REDIS_HOST=$REDIS_HOST"
    # 其他参数
    readonly O1="--LOGGING_LEVEL=$LOGGING_LEVEL"
    
    function stopService() {
        echo "[$$]stop service : $1"
        kill -9 $(jps -ml | grep $1 | awk '{print $1}') > /dev/null 2>&1
        if [[ $? -eq 0 ]]; then
        	echo "[$$]stop success : $1"
        else 
        	echo "[$$]stop failed : $1"
        fi
    }
    
    function startService() {
    		echo "[$$]start service : $1"
    		
    		case $1 in
        "$PRODUCT-config" | "$PRODUCT-governance" | "$PRODUCT-swagger")
            databaseName="hzero_governance"
            ;;
        # 需要启用更多的服务,将服务名称直接追加在后面
        "$PRODUCT-asgard" | "$PRODUCT-file" | "$PRODUCT-message")
            databaseName="hzero_$(echo $1 | cut -d - -f 2-)"
            ;;
        *)
            databaseName="hzero_platform"
            ;;
        esac
        startEnv="$E1 $E2 $E3 $E4 $E5 $D1 $D2 $D3 $D4 $O1"
        loggingPath="$LOGGING_PATH/$1"
        mkdir -p $loggingPath
        nohup java $JAVA_OPTS -jar $1.jar ${startEnv//"{databaseName}"/$databaseName} > "$loggingPath/app.log" & 
        echo "[$$]start success : $1"
    }
    
    function restartService() {
    	stopService $1
    	startService $1
    }
    
    # -----------------------------------------------
    # Process options with priority
    # -----------------------------------------------
    if [[ $1 == '--kill-em-all' ]]; then
    	jps -ml | grep $PRODUCT | awk '{print $1}' | xargs kill -9
        exit $?
    fi
    
    if [[ "$#" -gt 0 ]]; then
        echo "[$$]Restart : $*"
        echo "[$$"
        for service in $*
        do
            restartService $service
            echo "[$$]wait $WAIT s to start : $service"
            echo "[$$]"
            sleep $WAIT
        done
    else
        echo "[$$]Restart : ${SERVICE[*]}"
        echo "[$$]"
        for service in ${SERVICE[@]}
        do
            restartService $service
            echo "[$$]wait $WAIT s to start : $service"
            echo "[$$]"
            sleep $WAIT
        done
    fi
    
    echo "[$$]SUCCESS!"