ActiveMQ Artemis CLI工具提示端口9876被占用问题咨询
ActiveMQ Artemis CLI端口占用问题及用户添加方案
问题描述
我运行的是ActiveMQ Artemis 2.14.0,今天使用Broker的bin目录下的CLI工具时,哪怕执行help命令都会提示端口9876被占用。检查后发现这个端口正是运行Artemis的用户在占用,同时Broker的lock目录下有个空的cli.lock文件。现在我想给Broker添加新用户,想问下是不是必须关闭Broker才能操作,或者有没有其他方式正常使用CLI?
报错信息
WARNING: Failed to instantiate LoggerFinder provider; Using default. Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 9876; nested exception is: java.net.BindException: Address already in use (Bind failed) jdk.internal.agent.AgentConfigurationError: java.rmi.server.ExportException: Port already in use: 9876; nested exception is: java.net.BindException: Address already in use (Bind failed) at jdk.management.agent/sun.management.jmxremote.ConnectorBootstrap.startRemoteConnectorServer(ConnectorBootstrap.java:491) at jdk.management.agent/jdk.internal.agent.Agent.startAgent(Agent.java:447) at jdk.management.agent/jdk.internal.agent.Agent.startAgent(Agent.java:599) Caused by: java.rmi.server.ExportException: Port already in use: 9876; nested exception is: java.net.BindException: Address already in use (Bind failed) at java.rmi/sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:335) at java.rmi/sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:243) at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:412) at java.rmi/sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147) at java.rmi/sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:234) at java.rmi/sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:220) at java.rmi/sun.rmi.registry.RegistryImpl.<init>(RegistryImpl.java:180) at jdk.management.agent/sun.management.jmxremote.SingleEntryRegistry.<init>(SingleEntryRegistry.java:49) at jdk.management.agent/sun.management.jmxremote.ConnectorBootstrap.exportMBeanServer(ConnectorBootstrap.java:836) at jdk.management.agent/sun.management.jmxremote.ConnectorBootstrap.startRemoteConnectorServer(ConnectorBootstrap.java:479) ... 2 more Caused by: java.net.BindException: Address already in use (Bind failed) at java.base/java.net.PlainSocketImpl.socketBind(Native Method) at java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:452) at java.base/java.net.ServerSocket.bind(ServerSocket.java:395) at java.base/java.net.ServerSocket.<init>(ServerSocket.java:257) at java.base/java.net.ServerSocket.<init>(ServerSocket.java:149) at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createServerSocket(TCPDirectSocketFactory.java:45) at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:670) at java.rmi/sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:324) ... 11 more
etc/artemis.profile配置
# 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. ARTEMIS_HOME='/opt/artemis-2.14.0' ARTEMIS_INSTANCE='/var/lib/broker-HA' ARTEMIS_DATA_DIR='/var/lib/broker-HA/data' ARTEMIS_ETC_DIR='/var/lib/broker-HA/etc' ARTEMIS_OOME_DUMP='/var/lib/broker-HA/log/oom_dump.hprof' # The logging config will need an URI # this will be encoded in case you use spaces or special characters # on your directory structure ARTEMIS_INSTANCE_URI='file:/var/lib/broker-HA/' ARTEMIS_INSTANCE_ETC_URI='file:/var/lib/broker-HA/etc/' # Cluster Properties: Used to pass arguments to ActiveMQ Artemis which can be referenced in broker.xml #ARTEMIS_CLUSTER_PROPS="-Dactivemq.remoting.default.port=61617 -Dactivemq.remoting.amqp.port=5673 -Dactivemq.remoting.stomp.port=61614 -Dactivemq.remoting.hornetq.port=5446" # Java Opts if [ -z "$JAVA_ARGS" ]; then JAVA_ARGS=" -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9876 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=[ip] -XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms512M -Xmx2G -Dhawtio.realm=activemq -Dhawtio.offline=true -Dhawtio.role=amq -Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal -Djolokia.policyLocation=${ARTEMIS_INSTANCE_ETC_URI}jolokia-access.xml" fi # # Logs Safepoints JVM pauses: Uncomment to enable them # In addition to the traditional GC logs you could enable some JVM flags to know any meaningful and "hidden" pause that could # affect the latencies of the services delivered by the broker, including those that are not reported by the classic GC logs # and dependent by JVM background work (eg method deoptimizations, lock unbiasing, JNI, counted loops and obviously GC activity). # Replace "all_pauses.log" with the file name you want to log to. # JAVA_ARGS="$JAVA_ARGS -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1 -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+LogVMOutput -XX:LogFile=all_pauses.log" # # Enables the dumping of the java heap when a java.lang.OutOfMemoryError exception is thrown. # JAVA_ARGS="$JAVA_ARGS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${ARTEMIS_OOME_DUMP}" # Debug args: Uncomment to enable debug #DEBUG_ARGS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" # Debug args: Uncomment for async profiler #DEBUG_ARGS="-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints"
解决方案
核心原因
端口9876被占用是因为你在artemis.profile里配置了Broker的JMX远程监听端口为9876,而CLI工具默认会尝试启动一个本地JMX服务并绑定到这个端口,和正在运行的Broker的JMX端口冲突。另外空的cli.lock文件是之前CLI进程异常退出留下的,也会阻碍新的CLI进程启动。
无需关闭Broker的操作步骤
- 清理异常锁文件
先删除lock目录下的cli.lock文件,解除锁限制:
rm /var/lib/broker-HA/lock/cli.lock
- 直接连接Broker的JMX服务
既然Broker已经在监听9876端口的JMX服务,让CLI复用这个连接即可,执行添加用户命令时加上--url参数:
./artemis user add --url service:jmx:rmi:///jndi/rmi://[你的BrokerIP]:9876/jmxrmi
把[你的BrokerIP]替换成artemis.profile里java.rmi.server.hostname配置的IP地址。
- 持久化CLI默认连接(可选)
如果不想每次都加--url参数,可以修改etc/artemis.profile或者新建etc/artemis-cli.profile,添加以下配置:
ARTEMIS_CLI_ARGS="--url service:jmx:rmi:///jndi/rmi://[你的BrokerIP]:9876/jmxrmi"
之后执行CLI命令就会自动连接到运行中的Broker。
备选方案:关闭Broker操作
如果允许Broker停机,直接停止Broker后执行./artemis user add,修改后的用户配置会在Broker重启后生效。
注意事项
你当前的JMX配置没有启用认证(-Dcom.sun.management.jmxremote.authenticate=false),生产环境建议开启JMX认证,避免安全风险。
内容的提问来源于stack exchange,提问作者BenW
相关产品推荐
相关产品推荐

