在 rocketmq官网中下载最新bin
压缩文件(当前最新版本是4.7.0)
在.bash_profile
文件中添加环境变量:
export ROCKETMQ_HOME=/yourPath/rocketmq-all-4.7.0-bin-release
cd
到rocketmq-all-4.7.0-bin-release
目录下执行命令:
nohup sh bin/mqnamesrv &
tail -f ~/logs/rocketmqlogs/namesrv.log
INFO main - The Name Server boot success
cd
到cd到rocketmq-all-4.7.0-bin-release
目录下执行命令:
nohup sh bin/mqbroker -n localhost:9876 &
tail -f ~/logs/rocketmqlogs/broker.log
boot success
rocketmq-console
项目,下载相关依赖application.properties
中配置namesrvAddr
,这里的ip:port
跟rocketmq server
是一一对应的:rocketmq.config.namesrvAddr=localhost:9876
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-clientartifactId>
<version>4.7.0version>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-remotingartifactId>
<version>4.7.0version>
dependency>
<dependency>
<groupId>org.apache.rocketmqgroupId>
<artifactId>rocketmq-commonartifactId>
<version>4.7.0version>
dependency>
@Slf4j
@Component
public class Producer {
private String producerGroup = "test_producer";
private DefaultMQProducer producer;
public Producer(){
//示例生产者
producer = new DefaultMQProducer(producerGroup);
//不开启vip通道 开通口端口会减2
producer.setVipChannelEnabled(false);
//绑定name server
producer.setNamesrvAddr("localhost:9876");
start();
}
/**
* 对象在使用之前必须要调用一次,只能初始化一次
*/
private void start(){
try {
this.producer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
}
public DefaultMQProducer getProducer(){
return this.producer;
}
/**
* 一般在应用上下文,使用上下文监听器,进行关闭
*/
public void shutdown(){
this.producer.shutdown();
}
}
@Slf4j
@Component
public class Consumer {
@Resource
private RocketMQProperties rocketMQProperties;
/**
* 消费者实体对象
*/
private DefaultMQPushConsumer consumer;
/**
* 消费者组
*/
public static final String CONSUMER_GROUP = "test_consumer";
/**
* 通过构造函数 实例化对象
*/
public Consumer() throws MQClientException {
consumer = new DefaultMQPushConsumer(CONSUMER_GROUP);
consumer.setNamesrvAddr("localhost:9876");
// 订阅PushTopic下Tag为push的消息,都订阅消息
consumer.subscribe("firstPushTopic", "push");
// 程序第一次启动从消息队列头获取数据
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
//可以修改每次消费消息的数量,默认设置是每次消费一条
consumer.setConsumeMessageBatchMaxSize(1);
// //注册消费的监听 并在此监听中消费信息,并返回消费的状态信息
consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
// msgs中只收集同一个topic,同一个tag,并且key相同的message
// 会把不同的消息分别放置到不同的队列中
try {
for (Message msg : msgs) {
//消费者获取消息 这里只输出 不做后面逻辑处理
String body = new String(msg.getBody(), "utf-8");
log.info("Consumer-获取消息-主题topic为={}, 消费消息为={}", msg.getTopic(), body);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
consumer.start();
System.out.println("消费者 启动成功=======");
}
}
public static void main(String[] args) throws MQClientException {
new Consumer();
Producer producer = new Producer();
Message message = new Message();
message.setTopic("firstPushTopic");
message.setTags("push");
message.setBody("hello".getBytes());
try {
producer.getProducer().send(message);
} catch (RemotingException e) {
e.printStackTrace();
} catch (MQBrokerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
注意:其中的topic
和tags
必须一一对应!
尝试命令行克隆或者下载zip包,或者添加阿里云的镜像
我mac内存全盘250G,然而只剩下10几个G,确实不太够,但为了能继续使用rocketmq server
,需要做下配置:
rocketmq-all-4.7.0-bin-release/bin
下找到runbroker.cmd
,添加代码:set "JAVA_OPT=%JAVA_OPT% -Drocketmq.broker.diskSpaceWarningLevelRatio=0.98"
rocketmq-all-4.7.0-bin-release/bin
下找到runserver.cmd
,将内存改大一些:set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m"
修改配置之后,重启rocketmq的路由和broker,即可解决问题!