官网:https://start.spring.io/
注意:不要选择SNAPSHOT
点击GENERATE,下载zip包。解压zip包,导入IDEA.
配置好后,点击Apply,OK.
配置好后,点击Apply , OK.
IDEA右上角,点击Edit Cinfiguration
查看官方文档:
https://rocketmq.apache.org/docs/simple-example/
添加依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.3</version>
</dependency>
官网说明使用jdk1.8,我使用jdk11也可以。
@Component
public class PayProducer {
/**
* 消息生产者组
*/
private String producerGroup = "pay_group";
/**
* 服务器ip 和 端口
*/
private String nameServerAddr = "39.105.157.183:9876";
/**
* 消息生产者
*/
private DefaultMQProducer producer;
public PayProducer() {
producer = new DefaultMQProducer(producerGroup);
/**
* 创建单节点
*/
producer.setNamesrvAddr(nameServerAddr);
start();
}
/**
* 获取producer
* @return
*/
public DefaultMQProducer getProducer(){
return this.producer;
}
/**
* 对象在使用之前必须要调用一次,只能初始化一次
*/
public void start(){
try {
this.producer.start();
} catch (MQClientException e) {
e.printStackTrace();
}
}
/**
* 一般在应用上下文,使用上下文监听器进行关闭
*/
public void shutdown(){
this.producer.shutdown();
}
}
@RestController
public class PayCotroller {
@Autowired
PayProducer payProducer;
private static final String topic = "pay_test_topic2";
@RequestMapping("/api/v1/pay_cb")
public Object callback(String text) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {
/**
* 创建消息
*/
Message message = new Message(topic,"taga",text.getBytes());
/**
* 发送消息
*/
SendResult sendResult = payProducer.getProducer().send(message);
System.out.println(sendResult);
return new HashMap<>();
}
}
org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException:
sendDefaultImpl call timeout
原因:阿里云存在多网卡,rocketmq都会根据当前网卡选择一个IP使用,当你的机器有多块网卡时,很有可能会有问题。比如,我遇到的问题是我机器上有两个IP,一个公网IP,一个私网IP, 因此需要配置broker.conf 指定当前的公网ip, 然后重新启动broker
新增配置:conf/broker.conf (属性名称brokerIP1=broker所在的公网ip地址 )
新增这个配置:brokerIP1=120.76.62.13
启动命令:nohup sh bin/mqbroker -n localhost:9876 -c ./conf/broker.conf &
MQClientException: No route info of this topic, TopicTest1
原因:Broker 禁止自动创建 Topic,且用户没有通过手工方式创建 此Topic, 或者broker和Nameserver网络不通
解决:
通过 sh bin/mqbroker -m 查看配置
autoCreateTopicEnable=true 则自动创建topic 如果还没有创建的话,应该是客户端和服务端版本不一致。见下图
Centos7关闭防火墙 systemctl stop firewalld
服务端版本:
cat /usr/local/rocketmq-all-4.9.3/pom.xml
控制台查看不了数据,提示连接 10909错误
原因:Rocket默认开启了VIP通道,VIP通道端口为10911-2=10909
解决:阿里云安全组需要增加一个端口 10909
https://blog.csdn.net/qq_14853889/article/details/81053145
https://blog.csdn.net/wangmx1993328/article/details/81588217#%E5%BC%82%E5%B8%B8%E8%AF%B4%E6%98%8E
https://www.jianshu.com/p/bfd6d849f156
https://blog.csdn.net/wangmx1993328/article/details/81588217
@Component
public class PayConsumer {
/**
* 消费者
*/
private DefaultMQPushConsumer consumer;
/**
* 消费组
*/
private String consumerGroup = "pay_consumer_group";
public PayConsumer() throws MQClientException {
//创建消费者
consumer = new DefaultMQPushConsumer(consumerGroup);
//设置名称地址
consumer.setNamesrvAddr(JmsConfig.nameServerAddr);
//设置消费地点
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET); //默认是这一个
//订阅主题
consumer.subscribe(JmsConfig.topic,"*");
//注册监听器
consumer.registerMessageListener((MessageListenerConcurrently) (msgs,context) -> {
try {
Message msg = msgs.get(0);
System.out.printf("Receive new message:",Thread.currentThread().getName(),new String(msg.getBody()));
String topic = msg.getTopic();
String body = new String(msg.getBody(),"utf-8");
String tags = msg.getTags();
String keys = msg.getKeys();
System.out.println("topic="+topic+",tags="+tags+",keys="+keys+",msg="+body);
//返回成功 broker会把消息删除
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
//稍后重发 broker不会标记成功
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
});
consumer.start();
}
}