今天部署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
1 public void start() throws Exception { 2 3 lock = lockFile.getChannel().tryLock(0, 1, false); 4 if (lock == null || lock.isShared() || !lock.isValid()) { 5 throw new RuntimeException("Lock failed,MQ already started"); 6 } 7 8 lockFile.getChannel().write(ByteBuffer.wrap("lock".getBytes())); 9 lockFile.getChannel().force(true); 10 11 this.flushConsumeQueueService.start(); 12 this.commitLog.start(); 13 this.storeStatsService.start();
DefaultMessageStore.java
1 public DefaultMessageStore(final MessageStoreConfig messageStoreConfig, final BrokerStatsManager brokerStatsManager, 2 final MessageArrivingListener messageArrivingListener, final BrokerConfig brokerConfig) throws IOException { 3 this.messageArrivingListener = messageArrivingListener; 4 this.brokerConfig = brokerConfig; 5 this.messageStoreConfig = messageStoreConfig; 6 this.brokerStatsManager = brokerStatsManager; 7 this.allocateMappedFileService = new AllocateMappedFileService(this); 8 this.commitLog = new CommitLog(this); 9 this.consumeQueueTable = new ConcurrentHashMap<>(32); 10 11 this.flushConsumeQueueService = new FlushConsumeQueueService(); 12 this.cleanCommitLogService = new CleanCommitLogService(); 13 this.cleanConsumeQueueService = new CleanConsumeQueueService(); 14 this.storeStatsService = new StoreStatsService(); 15 this.indexService = new IndexService(this); 16 this.haService = new HAService(this); 17 18 this.reputMessageService = new ReputMessageService(); 19 20 this.scheduleMessageService = new ScheduleMessageService(this); 21 22 this.transientStorePool = new TransientStorePool(messageStoreConfig); 23 24 if (messageStoreConfig.isTransientStorePoolEnable()) { 25 this.transientStorePool.init(); 26 } 27 28 this.allocateMappedFileService.start(); 29 30 this.indexService.start(); 31 32 this.dispatcherList = new LinkedList<>(); 33 this.dispatcherList.addLast(new CommitLogDispatcherBuildConsumeQueue()); 34 this.dispatcherList.addLast(new CommitLogDispatcherBuildIndex()); 35 36 File file = new File(StorePathConfigHelper.getLockFile(messageStoreConfig.getStorePathRootDir())); 37 MappedFile.ensureDirOK(file.getParent()); 38 lockFile = new RandomAccessFile(file, "rw"); 39 }
BrokerController.java
1 public BrokerController( 2 final BrokerConfig brokerConfig, 3 final NettyServerConfig nettyServerConfig, 4 final NettyClientConfig nettyClientConfig, 5 final MessageStoreConfig messageStoreConfig 6 ) { 7 this.brokerConfig = brokerConfig; 8 this.nettyServerConfig = nettyServerConfig; 9 this.nettyClientConfig = nettyClientConfig; 10 this.messageStoreConfig = messageStoreConfig;
.....剩余代码不贴了
MessageStoreConfig.java
1 public class MessageStoreConfig { 2 //The root directory in which the log data is kept 3 @ImportantField 4 private String storePathRootDir = System.getProperty("user.home") + File.separator + "store"; 5 6 //The directory in which the commitlog is kept 7 @ImportantField 8 private String storePathCommitLog = System.getProperty("user.home") + File.separator + "store" 9 + File.separator + "commitlog"; 10 11 // CommitLog file size,default is 1G 12 private int mapedFileSizeCommitLog = 1024 * 1024 * 1024; 13 // ConsumeQueue file size,default is 30W 14 private int mapedFileSizeConsumeQueue = 300000 * ConsumeQueue.CQ_STORE_UNIT_SIZE; 15 // enable consume queue ext 16 private boolean enableConsumeQueueExt = false; 17 // ConsumeQueue extend file size, 48M 18 private int mappedFileSizeConsumeQueueExt = 48 * 1024 * 1024; 19 // Bit count of filter bit map. 20 // this will be set by pipe of calculate filter bit map. 21 private int bitMapLengthConsumeQueueExt = 64;
如果不加-c就是在默认路径下,自己可以去${user.dir}下去看看,肯定有这个目录
所以多次启动不加-c参数就会报上面错误
1 if (commandLine.hasOption('c')) { 2 String file = commandLine.getOptionValue('c'); 3 if (file != null) { 4 configFile = file; 5 InputStream in = new BufferedInputStream(new FileInputStream(file)); 6 properties = new Properties(); 7 properties.load(in); 8 9 properties2SystemEnv(properties); 10 MixAll.properties2Object(properties, brokerConfig); 11 MixAll.properties2Object(properties, nettyServerConfig); 12 MixAll.properties2Object(properties, nettyClientConfig); 13 MixAll.properties2Object(properties, messageStoreConfig); 14 15 BrokerPathConfigHelper.setBrokerConfigPath(file); 16 in.close(); 17 } 18 }
最后梳理一下启动流程
nohup sh mqbroker -c ${mqdir}/conf/2m-2s-sync/broker-a.properties &的启动流程
1.加载启动类BrokerStartup.java main方法
2.createBrokerController方法加载启动参数并确定使用默认配置还是读取对应的配置文件 主要是-c参数的作用
3.加载配置
有错误之处望指正。