rocketmq 下载地址:
https://rocketmq.apache.org/download/
本文兼容rocketmq 版本4.9.7
upzip rocketmq-all-4.9.7-bin-release.zip
sh bin/mqnamesrv
nohup sh bin/mqbroker -n 127.0.0.1:9876 -c ../conf/broker.conf autoCreateTopicEnable=true &
nohup sh mqbroker -n 127.0.0.1:9876 -c ../conf/broker.conf autoCreateTopicEnable=true
验证是否启动broker成功
telnet 127.0.0.1 9876
接入demo : https://github.com/Baixiu-code/mq-client-starter-test
完整可运行
```
common-mq.rocketmq.transport.address=127.0.0.1:9876
common-mq.rocketmq.transport.app=mq-client-test
#common-mq.rocketmq.transport.user=baixiu
#common-mq.rocketmq.transport.password=baixiuTest
common-mq.rocketmq.producer.name=messageProducer
common-mq.rocketmq.listeners[0].topicName=test
common-mq.rocketmq.listeners[0].listenerBeanName=testConsumerMsg
#common-mq.rocketmq.listeners[1].topicName=${mq.demo.topic2}
#common-mq.rocketmq.listeners[1].listenerBeanName=topic2Listenerbrew update
```
send msg
package com.baixiu.middleware.test;
import com.baixiu.middleware.mq.core.impl.rocketmq.config.RocketBootConfig;
import com.baixiu.middleware.mq.core.inner.CustomMessageProducer;
import com.baixiu.middleware.mq.model.CommonMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author baixiu
* @date 创建时间 2023/11/29 7:15 PM
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ComponentScan(basePackages={"com.baixiu.middleware.test","com.baixiu.middleware.mq"})
@TestPropertySource("classpath:rocketmq.properties")
public class TestProducerMsg {
@Autowired
private CustomMessageProducer customMessageProducer;
public void sendMsgTest(){
try {
CommonMessage commonMessage=new CommonMessage ();
commonMessage.setTopic("test");
commonMessage.setText ("hello baixiu mq.");
customMessageProducer.send ("test",commonMessage);
System.out.println ("test topic send succeed");
} catch (Exception e) {
throw new RuntimeException (e);
}
}
@Test
public void testSendMsg(){
sendMsgTest();
}
}
consumer msg
package com.baixiu.middleware.test;
import com.baixiu.middleware.mq.core.inner.CustomMessageListener;
import com.baixiu.middleware.mq.model.CommonMessage;
import org.springframework.stereotype.Component;
/**
* @author baixiu
* @date 创建时间 2023/12/5 2:43 PM
*/
@Component
public class TestConsumerMsg implements CustomMessageListener {
@Override
public void onMessage(CommonMessage commonMessage) {
System.out.println("接收到消息:"+commonMessage);
}
}
需要同时启动namesrv
sh mqnamesrv
```brokerIP1 = 127.0.0.1```
``` namesrvAddr = 127.0.0.1:9876```
关闭broker
```sh bin/mqshutdown broker```
关闭namesrv
```sh bin/mqshutdown namesrv```