今天部署rocketmq集群时一台机器部署一个master 和slave,slave部署总是失败,通过查看日志显示下面的错误
java.lang.RuntimeException: Lock failed,MQ already started
at org.apache.rocketmq.store.DefaultMessageStore.start(DefaultMessageStore.java:214)
at org.apache.rocketmq.broker.BrokerController.start(BrokerController.java:654)
at org.apache.rocketmq.broker.BrokerStartup.start(BrokerStartup.java:62)
at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:56)
23:21:47.512 [main] ERROR RocketmqCommon - Failed to obtain the host name
java.net.UnknownHostException: slave02: slave02: unknown error
at java.net.InetAddress.getLocalHost(InetAddress.java:1505) ~[na:1.8.0_65]
at org.apache.rocketmq.common.BrokerConfig.localHostName(BrokerConfig.java:202) [rocketmq-common-4.2.0.jar:4.2.0]
at org.apache.rocketmq.common.BrokerConfig.<init>(BrokerConfig.java:39) [rocketmq-common-4.2.0.jar:4.2.0]
at org.apache.rocketmq.broker.BrokerStartup.createBrokerController(BrokerStartup.java:101) [rocketmq-broker-4.2.0.jar:4.2.0]
at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:56) [rocketmq-broker-4.2.0.jar:4.2.0]
Caused by: java.net.UnknownHostException: slave02: unknown error
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_65]
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_65]
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_65]
at java.net.InetAddress.getLocalHost(InetAddress.java:1500) ~[na:1.8.0_65]
... 4 common frames omitted
java.lang.RuntimeException: Lock failed,MQ already started
at org.apache.rocketmq.store.DefaultMessageStore.start(DefaultMessageStore.java:214)
at org.apache.rocketmq.broker.BrokerController.start(BrokerController.java:654)
at org.apache.rocketmq.broker.BrokerStartup.start(BrokerStartup.java:62)
at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:56)
23:22:32.218 [main] ERROR RocketmqCommon - Failed to obtain the host name
java.net.UnknownHostException: slave02: slave02: unknown error
at java.net.InetAddress.getLocalHost(InetAddress.java:1505) ~[na:1.8.0_65]
at org.apache.rocketmq.common.BrokerConfig.localHostName(BrokerConfig.java:202) [rocketmq-common-4.2.0.jar:4.2.0]
at org.apache.rocketmq.common.BrokerConfig.<init>(BrokerConfig.java:39) [rocketmq-common-4.2.0.jar:4.2.0]
at org.apache.rocketmq.broker.BrokerStartup.createBrokerController(BrokerStartup.java:101) [rocketmq-broker-4.2.0.jar:4.2.0]
at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:56) [rocketmq-broker-4.2.0.jar:4.2.0]
错误1. slave02: unknown error是由于我启动的时候启动命令没有-c走的默认的代码中的配置
public class BrokerConfig {
private static final Logger log = LoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME); private String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));
@ImportantField
private String namesrvAddr = System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY, System.getenv(MixAll.NAMESRV_ADDR_ENV));
@ImportantField
private String brokerIP1 = RemotingUtil.getLocalAddress();
private String brokerIP2 = RemotingUtil.getLocalAddress();
@ImportantField
private String brokerName = localHostName();
@ImportantField
private String brokerClusterName = "DefaultCluster";
@ImportantField
private long brokerId = MixAll.MASTER_ID;
private int brokerPermission = PermName.PERM_READ | PermName.PERM_WRITE;
错误2. Lock failed,MQ already started也是走的代码默认的配置
DefaultMessageStore.java
public void start() throws Exception { lock = lockFile.getChannel().tryLock(0, 1, false);
if (lock == null || lock.isShared() || !lock.isValid()) {
throw new RuntimeException("Lock failed,MQ already started");
} lockFile.getChannel().write(ByteBuffer.wrap("lock".getBytes()));
lockFile.getChannel().force(true); this.flushConsumeQueueService.start();
this.commitLog.start();
this.storeStatsService.start();
DefaultMessageStore.java
public DefaultMessageStore(final MessageStoreConfig messageStoreConfig, final BrokerStatsManager brokerStatsManager,
final MessageArrivingListener messageArrivingListener, final BrokerConfig brokerConfig) throws IOException {
this.messageArrivingListener = messageArrivingListener;
this.brokerConfig = brokerConfig;
this.messageStoreConfig = messageStoreConfig;
this.brokerStatsManager = brokerStatsManager;
this.allocateMappedFileService = new AllocateMappedFileService(this);
this.commitLog = new CommitLog(this);
this.consumeQueueTable = new ConcurrentHashMap<>(32); this.flushConsumeQueueService = new FlushConsumeQueueService();
this.cleanCommitLogService = new CleanCommitLogService();
this.cleanConsumeQueueService = new CleanConsumeQueueService();
this.storeStatsService = new StoreStatsService();
this.indexService = new IndexService(this);
this.haService = new HAService(this); this.reputMessageService = new ReputMessageService(); this.scheduleMessageService = new ScheduleMessageService(this); this.transientStorePool = new TransientStorePool(messageStoreConfig); if (messageStoreConfig.isTransientStorePoolEnable()) {
this.transientStorePool.init();
} this.allocateMappedFileService.start(); this.indexService.start(); this.dispatcherList = new LinkedList<>();
this.dispatcherList.addLast(new CommitLogDispatcherBuildConsumeQueue());
this.dispatcherList.addLast(new CommitLogDispatcherBuildIndex()); File file = new File(StorePathConfigHelper.getLockFile(messageStoreConfig.getStorePathRootDir()));
MappedFile.ensureDirOK(file.getParent());
lockFile = new RandomAccessFile(file, "rw");
}
BrokerController.java
public BrokerController(
final BrokerConfig brokerConfig,
final NettyServerConfig nettyServerConfig,
final NettyClientConfig nettyClientConfig,
final MessageStoreConfig messageStoreConfig
) {
this.brokerConfig = brokerConfig;
this.nettyServerConfig = nettyServerConfig;
this.nettyClientConfig = nettyClientConfig;
this.messageStoreConfig = messageStoreConfig;
.....剩余代码不贴了
MessageStoreConfig.java
public class MessageStoreConfig {
//The root directory in which the log data is kept
@ImportantField
private String storePathRootDir = System.getProperty("user.home") + File.separator + "store"; //The directory in which the commitlog is kept
@ImportantField
private String storePathCommitLog = System.getProperty("user.home") + File.separator + "store"
+ File.separator + "commitlog"; // CommitLog file size,default is 1G
private int mapedFileSizeCommitLog = 1024 * 1024 * 1024;
// ConsumeQueue file size,default is 30W
private int mapedFileSizeConsumeQueue = 300000 * ConsumeQueue.CQ_STORE_UNIT_SIZE;
// enable consume queue ext
private boolean enableConsumeQueueExt = false;
// ConsumeQueue extend file size, 48M
private int mappedFileSizeConsumeQueueExt = 48 * 1024 * 1024;
// Bit count of filter bit map.
// this will be set by pipe of calculate filter bit map.
private int bitMapLengthConsumeQueueExt = 64;
如果不加-c就是在默认路径下,自己可以去${user.dir}下去看看,肯定有这个目录
所以多次启动不加-c参数就会报上面错误
if (commandLine.hasOption('c')) {
String file = commandLine.getOptionValue('c');
if (file != null) {
configFile = file;
InputStream in = new BufferedInputStream(new FileInputStream(file));
properties = new Properties();
properties.load(in); properties2SystemEnv(properties);
MixAll.properties2Object(properties, brokerConfig);
MixAll.properties2Object(properties, nettyServerConfig);
MixAll.properties2Object(properties, nettyClientConfig);
MixAll.properties2Object(properties, messageStoreConfig); BrokerPathConfigHelper.setBrokerConfigPath(file);
in.close();
}
}
最后梳理一下启动流程
nohup sh mqbroker -c ${mqdir}/conf/2m-2s-sync/broker-a.properties &的启动流程
1.加载启动类BrokerStartup.java main方法
2.createBrokerController方法加载启动参数并确定使用默认配置还是读取对应的配置文件 主要是-c参数的作用
3.加载配置
有错误之处望指正。