RocketMQ runbroker.sh 分析JVM启动参数

本文详细介绍了RocketMQ Broker启动脚本runbroker.sh中的JVM参数配置,包括JVM模式选择、内存分配策略、垃圾回收策略等关键设置,并解释了各项参数的作用及推荐配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

runbroker.sh

#===========================================================================================
# JVM Configuration
#===========================================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:PermSize=128m -XX:MaxPermSize=320m"
JAVA_OPT="${JAVA_OPT} -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8 -XX:+DisableExplicitGC"
JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${HOME}/rmq_bk_gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${BASE_DIR}/lib"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"

-server By default, all client applications run in -client mode, while the directory server and certain server utilities run in -server mode. Generally, -server mode provides higher throughput than -client mode, at the expense of slightly longer startup times.

-Xms4g Sets the initial size (in bytes) of the heap. This value must be a multiple of 1024 and greater than 1 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

-Xmx4g Specifies the maximum size (in bytes) of the memory allocation pool in bytes.This value must be a multiple of 1024 and greater than 2 MB. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. The default value is chosen at runtime based on system configuration. For server deployments, -Xms and -Xmx are often set to the same value. See the section "Ergonomics" in Java SE HotSpot Virtual Machine Garbage Collection Tuning Guide.

click to see configuring-the-default-jvm-and-java-arguments

-Xmn2g Sets the initial and maximum size (in bytes) of the heap for the young generation (nursery). Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

The young generation region of the heap is used for new objects. GC is performed in this region more often than in other regions. If the size for the young generation is too small, then a lot of minor garbage collections will be performed. If the size is too large, then only full garbage collections will be performed, which can take a long time to complete. Oracle recommends that you keep the size for the young generation between a half and a quarter of the overall heap size. 7  8

-XX:PermSize=128m Sets the space (in bytes) allocated to the permanent generation that triggers a garbage collection if it is exceeded. This option was deprecated un JDK 8, and superseded by the -XX:MetaspaceSize option.

-XX:MaxPermSize=320m Sets the maximum permanent generation space size (in bytes). This option was deprecated in JDK 8, and superseded by the -XX:MaxMetaspaceSize option.

-XX:+UseConcMarkSweepGC Enables the use of the CMS garbage collector for the old generation. Oracle recommends that you use the CMS garbage collector when application latency requirements cannot be met by the throughput (-XX:+UseParallelGC) garbage collector. The G1 garbage collector (-XX:+UseG1GC) is another alternative. By default, this option is disabled and the collector is chosen automatically based on the configuration of the machine and type of the JVM. When this option is enabled, the -XX:+UseParNewGC option is automatically set and you should not disable it, because the following combination of options has been deprecated in JDK 8: -XX:+UseConcMarkSweepGC -XX:-UseParNewGC.

-XX:+UseParNewGC Enables the use of parallel threads for collection in the young generation. By default, this option is disabled. It is automatically enabled when you set the -XX:+UseConcMarkSweepGC option. Using the -XX:+UseParNewGC option without the -XX:+UseConcMarkSweepGC option was deprecated in JDK 8.

-XX:+UseCMSCompactAtFullCollection 表示在FGC之后进行压缩,因为CMS默认不压缩空间的。 

-XX:CMSInitiatingOccupancyFraction=70 使用cms作为垃圾回收,使用70%后开始CMS收集。

-XX:+CMSParallelRemarkEnabled 开启并行标记。

-XX:SoftRefLRUPolicyMSPerMB=0 每兆堆空闲空间中SoftReference的存活时间。This flag enables aggressive processing of software references. Use this flag if the software reference count has an impact on the Java HotSpot VM garbage collector.

-XX:+CMSClassUnloadingEnabled Enables class unloading when using the concurrent mark-sweep (CMS) garbage collector. This option is enabled by default. To disable class unloading for the CMS garbage collector, specify -XX:-CMSClassUnloadingEnabled.

-XX:SurvivorRatio=8 Sets the ratio between eden space size and survivor space size. By default, this option is set to 8. The following example shows how to set the eden/survivor space ratio to 4:-XX:SurvivorRatio=4

-XX:+DisableExplicitGC 关闭System.gc()

-verbose:gc Displays information about each garbage collection (GC) event.
-Xloggc:${HOME}/rmq_bk_gc.log Sets the file to which verbose GC events information should be redirected for logging. The information written to this file is similar to the output of -verbose:gc with the time elapsed since the first GC event preceding each logged event. The -Xloggc option overrides -verbose:gc if both are given with the same java command.
-XX:+PrintGCDetails Enables printing of detailed messages at every GC. By default, this option is disabled.
-XX:+PrintGCDateStamps Enables printing of a date stamp at every GC. By default, this option is disabled.

转载于:https://www.cnblogs.com/zno2/p/4599345.html

在 Ubuntu 系统中启动 RocketMQ 主要包括以下几个步骤: 1. **安装 Java** RocketMQ 依赖于 Java 运行环境,因此首先需要确保系统中已安装 JDK。可以通过以下命令检查是否已安装 Java: ```bash java -version ``` 如果未安装,可以使用以下命令安装 OpenJDK: ```bash sudo apt update sudo apt install default-jdk ``` 2. **下载并解压 RocketMQ** 访问 [RocketMQ 官方 GitHub 发布页面](https://github.com/apache/rocketmq/releases) 下载所需的版本(以 `4.9.4` 为例): ```bash wget https://archive.apache.org/dist/rocketmq/4.9.4/rocketmq-all-4.9.4-bin-release.zip unzip rocketmq-all-4.9.4-bin-release.zip -d /opt/rocketmq cd /opt/rocketmq/rocketmq-4.9.4 ``` 3. **启动 NameServer** RocketMQ 的运行需要先启动 NameServer,它是整个消息系统的路由中心。 ```bash nohup bin/mqnamesrv & ``` 可以通过查看日志文件确认 NameServer 是否成功启动: ```bash tail -f ~/logs/start.out ``` 4. **启动 Broker** 在启动 Broker 之前,建议设置 `brokerIP1` 为本机的 IP 地址,以便外部客户端能够访问 Broker。 编辑 `bin/runserver.sh` 和 `bin/runbroker.sh` 文件,调整 JVM 内存参数以适应本地环境: ```bash vi bin/runserver.sh vi bin/runbroker.sh ``` 修改类似 `-Xms256m -Xmx256m` 的参数以降低内存占用(适用于资源有限的测试环境)。然后启动 Broker: ```bash nohup bin/mqbroker -n localhost:9876 & ``` 同样可以通过日志文件确认 Broker 是否启动成功: ```bash tail -f ~/logs/start.out ``` 5. **发送与消费消息测试** RocketMQ 提供了简单的生产者和消费者示例用于测试。可以分别启动生产者和消费者进行验证: - 启动生产者并发送消息: ```bash export NAMESRV_ADDR=localhost:9876 bin/tools.sh org.apache.rocketmq.example.quickstart.Producer ``` - 启动消费者并接收消息: ```bash bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer ``` 6. **配置防火墙规则** 如果系统启用了防火墙(如 UFW),需要开放相应的端口(通常是 `9876` 用于 NameServer,`10911` 用于 Broker): ```bash sudo ufw allow 9876/tcp sudo ufw allow 10911/tcp ``` 7. **优化与扩展** 对于生产环境,可以参考分布式架构设计原则对 RocketMQ 进行集群部署,例如利用负载均衡器分散流量压力,并实现无状态设计以避免单点故障[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值