shell脚本搭配yml文件私有化部署

shell脚本直接启动jar读取同级目录下的 yml文件私有化部署

准备yml文件,启动springBoot的jar会默认优先读取同级目录下的yml配置

server:
  port: 10072
spring:
  profiles:
    active: dev
  servlet:
    multipart:
      max-file-size: 500MB
      max-request-size: 500MB
  application:
    name: xxx
  redis:
    cluster:
      timeout: 5000
      nodes: node1:7002,node2:7001,node3:7002,node4:7001,node5:7002
  datasource:
    url: jdbc:mysql://ip:3306/xxxx?useUnicode=yes&useSSL=false&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: xxx
    driver-class-name: com.mysql.cj.jdbc.Driver

# 调用次数 = (ribbon.MaxAutoRetriesNextServer + 1) * (ribbon.MaxAutoRetries + 1)
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000
  # 自动切换实例的重试次数默认为1
  MaxAutoRetriesNextServer: 1
  # 对当前实例的重试次数默认为0
  MaxAutoRetries: 1
  # 对所有操作请求都进行重试默认未false
  OkToRetryOnAllOperations: true
mybatis:
  mapper-locations:
    - classpath*:/mapper/*Mapper.xml
  config-location: classpath:mybatis-config.xml
#日志
logging:
  config: classpath:logback-prod.xml



准备脚本

#!/bin/bash

# 检查参数是否正确
if [ $# -ne 2 ]; then
    echo "Usage: sh xx.sh [start|stop|restart] [prod|dev|test]"
    exit 1
fi

# 获取参数
command=$1
profile=$2

# 定义应用程序名称和JAR文件名
app_name="xxx"
jar_file="${app_name}.jar"

# 启动方法
start() {
    # 检查程序是否已经在运行
    pid=$(pgrep -f ${jar_file})
    if [ -n "${pid}" ]; then
        echo "${app_name} is already running. PID: ${pid}"
        exit 1
    fi

    # 设置JVM参数
    jvm_options="-Xmx2500m -Xms2500m -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+HeapDumpOnOutOfMemoryError -verbose:gc -Xloggc:./logs/gc.log -XX:HeapDumpPath=./logs"

    # 启动应用程序
    nohup java ${jvm_options} -jar ${jar_file} --spring.profiles.active=common,${profile}  > ./logs/${app_name}.log 2>&1 &
    echo "${app_name} is started."
}

# 停止方法
stop() {
    # 检查程序是否在运行
    pid=$(pgrep -f ${jar_file})
    if [ -z "${pid}" ]; then
        echo "${app_name} is not running."
        exit 1
    fi

    # 停止应用程序
    kill ${pid}
    echo "${app_name} is stopped."
}

# 重启方法
restart() {
    stop
    sleep 1
    start
}

# 输出运行状态
status() {
    # 检查程序是否在运行
    pid=$(pgrep -f ${jar_file})
    if [ -n "${pid}" ]; then
        echo "${app_name} is running. PID: ${pid}"
    else
        echo "${app_name} is not running."
    fi
}

# 根据输入参数执行对应方法
case "${command}" in
    "start")
        start
        ;;
    "stop")
        stop
        ;;
    "restart")
        restart
        ;;
    "status")
        status
        ;;
    *)
        echo "Unknown command: ${command}"
        exit 1
        ;;
esac

启动方式

sh start.sh start/restart/stop dev/test/prod 

需要注意:start.sh 需要和 application.yml , xxx.jar在同一目录下

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值