1.根据官方文档指引下载对应的rocketMQ源码包,上传到服务器解压
2. 启动NameServer
nohup sh bin/mqnamesrv &
验证namesrv是否启动成功
tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success…
3.启动Broker+Proxy
nohup sh bin/mqbroker -n localhost:9876 --enable-proxy &
验证broker是否启动成功, 比如, broker的ip是192.168.1.2 然后名字是broker-a
tail -f ~/logs/rocketmqlogs/proxy.log
The broker[broker-a,192.169.1.2:10911] boot success…
4.创建topic
bin/mqadmin updatetopic -n localhost:9876 -t TestTopic -c DefaultCluster
在nameSrv和broker都部署成功情况下,创建消息生产者java工程进行测试
```java
package com.example.rocketmqprovider.msgProvider;import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientConfigurationBuilder;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** @author zhangshunmin* @since 2024/3/30*/
public class RocketmqProducer {private static final Logger logger = LoggerFactory.getLogger(RocketmqProducer.class);public static void main(String[] args) throws ClientException {// 接入点地址,需要设置成Proxy的地址和端口列表,一般是xxx:8081;xxx:8081。String endpoint = "http://ip:8081";// 消息发送的目标Topic名称,需要提前创建。String topic = "TestTopic";ClientServiceProvider provider = ClientServiceProvider.loadService();ClientConfigurationBuilder builder = ClientConfiguration.newBuilder().setEndpoints(endpoint);ClientConfiguration configuration = builder.build();// 初始化Producer时需要设置通信配置以及预绑定的Topic。Producer producer = provider.newProducerBuilder().setTopics(topic).setClientConfiguration(configuration).build();// 普通消息发送。Message message = provider.newMessageBuilder().setTopic(topic)// 设置消息索引键,可根据关键字精确查找某条消息。.setKeys("messageKey")// 设置消息Tag,用于消费端根据指定Tag过滤消息。.setTag("messageTag")// 消息体。.setBody("messageBody".getBytes()).build();try {// 发送消息,需要关注发送结果,并捕获失败等异常。SendReceipt sendReceipt = producer.send(message);logger.info("Send message successfully, messageId={}", sendReceipt.getMessageId());} catch (ClientException e) {logger.error("Failed to send message", e);}// producer.close();}}
启动程序,报如下错误
Expected the service PushConsumerImpl-1 [FAILED] to be RUNNING, but the service has FAILED。
DEADLINE_EXCEEDED: deadline exceeded after 2.981878875s. [buffered_nanos=2985515208, waiting_for_connection]
在网上查询了一系列,没得到想要结果,可检查自己服务器是否开放了对应的nameSrv和broker端口
发现自己服务器入方向规则未开放这两个端口,配置好端口重新启动生产者程序,上述问题成功解决;
但是,此时仍然出现了一个问题
Caused by: java.util.concurrent.CancellationException: Task was cancelled.
解决方案:
在第三步启动broker+proxy的时候分开进行启动
nohup sh bin/mqbroker -n localhost:9876 &
nohup sh bin/mqproxy -n localhost:9876 &
再次重新启动生产者程序,启动成功,提示信息发送成功