RocketMQ部署之动态设置JVM启动参数

这里是weihubeats,觉得文章不错可以关注公众号小奏技术,文章首发。拒绝营销号,拒绝标题党

背景

线上的RocketMQ集群有运行一段时间了。比如测试环境和线上环境的RocketMQ集群部署的机器内存大小肯定不一样。所以可能要写多个部署脚本。非常麻烦
官方的部署脚本是不支持根据宿主机内存大小动态设置JVM启动参数的

RocketMQ部署之动态设置JVM启动参数_第1张图片

可以看到是写死的,给4g。
但是比如我线上的机器内存大小为8g,实际我要给的内存可能是6g。我又需要手动改脚本非常麻烦。所以想要修改基于宿主机内存大小自动调节JVM内存大小

基于jvm自带参数

实际上jdk在11就支持动态参数了,比如上面的脚本我们可以改成如下方式

choose_gc_options()
{
    # Example of JAVA_MAJOR_VERSION value : '1', '9', '10', '11', ...
    # '1' means releases befor Java 9
    JAVA_MAJOR_VERSION=$("$JAVA" -version 2>&1 | sed -r -n 's/.* version "([0-9]*).*$/\1/p')
    if [ -z "$JAVA_MAJOR_VERSION" ] || [ "$JAVA_MAJOR_VERSION" -lt "9" ] ; then
      JAVA_OPT="${JAVA_OPT} -server -XX:MaxRAMPercentage=70 -XX:MinRAMPercentage=70 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
      JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
      JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${GC_LOG_DIR}/rmq_srv_gc_%p_%t.log:time,tags:filecount=5,filesize=30M"
    else
      JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
      JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
      JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${GC_LOG_DIR}/rmq_srv_gc_%p_%t.log:time,tags:filecount=5,filesize=30M"
    fi
}
  1. -XX:MaxRAMPercentage 用于设置JVM可使用的最大RAM百分比。举例来说,你可以使用 -XX:MaxRAMPercentage=75 指定JVM最多可用服务器内存的75%。

但是实际启动会报错,当前的服务器jdk版本不支持
我的jdk版本为1.8.0_362
实际我们是可以改动RocketMQ源码去支持jdk11的部署,但是这种方式比较耗费精力。所以我这里决定使用sh脚本来实现

sh脚本实现

核心脚本代码主要是这几行

# 获取宿主机内存总量(单位:M)
total_mem=$(grep MemTotal /proc/meminfo | awk '{print int($2/1024)}')

# 将内存总量转为单位:G 向上取整
mem_in_g=$((total_mem + 1023 / 1024))

# 判断内存大小,按需设定内存分配比例
if [ $mem_in_g -lt 16 ]; then
  mem_ratio=90
elif [ $mem_in_g -ge 16 -a $mem_in_g -lt 32 ]; then
  mem_ratio=95
elif [ $mem_in_g -ge 32 ]; then
  mem_ratio=95
fi

heap_size=$(($total_mem * $mem_ratio /100 / 1024))
metaspace_size=$(echo "$total_mem * 0.02" | bc | awk '{print int($1+0.5)}' | sed -e 's/^0\{1,\}//')
max_metaspace_size=$(echo "$total_mem * 0.04" | bc | awk '{print int($1+0.5)}' | sed -e 's/^0\{1,\}//')

choose_gc_options()
{
    # Example of JAVA_MAJOR_VERSION value : '1', '9', '10', '11', ...
    # '1' means releases befor Java 9
    JAVA_MAJOR_VERSION=$("$JAVA" -version 2>&1 | sed -r -n 's/.* version "([0-9]*).*$/\1/p')
    if [ -z "$JAVA_MAJOR_VERSION" ] || [ "$JAVA_MAJOR_VERSION" -lt "9" ] ; then
      JAVA_OPT="${JAVA_OPT} -server -Xms${heap_size}g -Xmx${heap_size}g -XX:MetaspaceSize=${metaspace_size}m -XX:MaxMetaspaceSize=${max_metaspace_size}m"
      JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
      JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${GC_LOG_DIR}/rmq_srv_gc_%p_%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps"
      JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
    else
      JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
      JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
      JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${GC_LOG_DIR}/rmq_srv_gc_%p_%t.log:time,tags:filecount=5,filesize=30M"
    fi
}

上面的脚本都有注释。我们大致还是解释下

  1. 获取宿主机的内存大小(非可用内存,我们要保证宿主机分配不到预定内存就启动报错)
  2. 小于16g内存给总内存的90%
  3. 大于等于16g小于32g内存我们给总内存的95%
  4. 大于等于32g的我们也给95%内存(实际这里可以和判断3一起,但是为了以后可能调整分配的内存大小就暂时分开)
  5. 元空间大小我们给总内存的0.02
  6. 最大元空间大小我们给总内存的0。04
  7. 注意获取到的总内存要向上取整,因为8g的机器我们实际获取到的内存大小大概为7296M,我们加一个1023(一个无线趋近于1的数就可以向上取整了)

最后给一个完整的runserver.sh

  • runserver.sh
#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#===========================================================================================
# Java Environment Setting
#===========================================================================================
error_exit ()
{
    echo "ERROR: $1 !!"
    exit 1
}

find_java_home()
{
    case "`uname`" in
        Darwin)
            JAVA_HOME=$(/usr/libexec/java_home)
        ;;
        *)
            JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
        ;;
    esac
}

find_java_home

[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)!"

export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=$(dirname $0)/..
export CLASSPATH=.:${BASE_DIR}/conf:${BASE_DIR}/lib/*:${CLASSPATH}

#===========================================================================================
# JVM Configuration
#===========================================================================================
# The RAMDisk initializing size in MB on Darwin OS for gc-log
DIR_SIZE_IN_MB=600

# 获取宿主机内存总量(单位:M)
total_mem=$(grep MemTotal /proc/meminfo | awk '{print int($2/1024)}')

# 将内存总量转为单位:G 向上取整
mem_in_g=$((total_mem + 1023 / 1024))

# 判断内存大小,按需设定内存分配比例
if [ $mem_in_g -lt 16 ]; then
  mem_ratio=90
elif [ $mem_in_g -ge 16 -a $mem_in_g -lt 32 ]; then
  mem_ratio=95
elif [ $mem_in_g -ge 32 ]; then
  mem_ratio=95
fi

heap_size=$(($total_mem * $mem_ratio /100 / 1024))
metaspace_size=$(echo "$total_mem * 0.02" | bc | awk '{print int($1+0.5)}' | sed -e 's/^0\{1,\}//')
max_metaspace_size=$(echo "$total_mem * 0.04" | bc | awk '{print int($1+0.5)}' | sed -e 's/^0\{1,\}//')


choose_gc_log_directory()
{
    case "`uname`" in
        Darwin)
            if [ ! -d "/Volumes/RAMDisk" ]; then
                # create ram disk on Darwin systems as gc-log directory
                DEV=`hdiutil attach -nomount ram://$((2 * 1024 * DIR_SIZE_IN_MB))` > /dev/null
                diskutil eraseVolume HFS+ RAMDisk ${DEV} > /dev/null
                echo "Create RAMDisk /Volumes/RAMDisk for gc logging on Darwin OS."
            fi
            GC_LOG_DIR="/Volumes/RAMDisk"
        ;;
        *)
            # check if /dev/shm exists on other systems
            if [ -d "/dev/shm" ]; then
                GC_LOG_DIR="/dev/shm"
            else
                GC_LOG_DIR=${BASE_DIR}
            fi
        ;;
    esac
}

choose_gc_options()
{
    # Example of JAVA_MAJOR_VERSION value : '1', '9', '10', '11', ...
    # '1' means releases befor Java 9
    JAVA_MAJOR_VERSION=$("$JAVA" -version 2>&1 | sed -r -n 's/.* version "([0-9]*).*$/\1/p')
    if [ -z "$JAVA_MAJOR_VERSION" ] || [ "$JAVA_MAJOR_VERSION" -lt "9" ] ; then
      JAVA_OPT="${JAVA_OPT} -server -Xms${heap_size}g -Xmx${heap_size}g -XX:MetaspaceSize=${metaspace_size}m -XX:MaxMetaspaceSize=${max_metaspace_size}m"
      JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
      JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${GC_LOG_DIR}/rmq_srv_gc_%p_%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps"
      JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
    else
      JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
      JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
      JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${GC_LOG_DIR}/rmq_srv_gc_%p_%t.log:time,tags:filecount=5,filesize=30M"
    fi
}

choose_gc_log_directory
choose_gc_options
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"

$JAVA ${JAVA_OPT} $@

你可能感兴趣的:(jvm,java-rocketmq,rocketmq)