1.下载rocketmq
此项目版本为:rocketmq-all-4.3.0-bin-release
配置:
D:\rocketmq-all-4.3.0-bin-release\rocketmq-externals\rocketmq-console\src\main\resources下
1.修改application.properties
server.contextPath=
server.port=8088
#spring.application.index=true
spring.application.name=rocketmq-console
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
logging.config=classpath:logback.xml
#if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
rocketmq.config.namesrvAddr=127.0.0.1:9876
#if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel=
#rocketmq-console's data path:dashboard/monitor
rocketmq.config.dataPath=/tmp/rocketmq-console/data
#set it false if you don't want use dashboard.default true
rocketmq.config.enableDashBoardCollect=true
2。搞一个可视化工具,我这里就弄了一个(自行百度下个很简单)
然后启动 rocketmq服务和可视化界面,我的是这样
① 启动服务 Cmd命令框执行进入至‘MQ文件夹\bin’下,然后执行‘start mqnamesrv.cmd’
,启动NAMESERVER。成功后会弹出提示框,此框勿关闭
② 启动服务 Cmd命令框执行进入至‘MQ文件夹\bin’下,
然后执行‘start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true’,
启动BROKER。成功后会弹出提示框,此框勿关闭。
③ 启动可视化界面 Cmd命令框执行进入至
‘D:\rocketmq\rocketmq-all-4.3.0-bin-release\rocketmq-externals\rocketmq-console\target’下,
执行 ‘ java -jar rocketmq-console-ng-1.0.1.jar’
4,开始在springboot上整合
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--RocketMq -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
---------------------------------------------------------------
application.yml
server:
port: 80
mq:
group: mymq
topic: mytopic
tag: mytag
namesraddr: 127.0.0.1:9876
---------------------------------------------------
启动类激活生产者和消费者,全局激活一次就够了
@SpringBootApplication
public class App implements CommandLineRunner {
@Autowired
mqProvider provider;
@Autowired
mqConsumer consumer;
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("正在激活RocketMq");
try {
provider.providerstart();
consumer.consumer();
System.out.println("mq已经激活,消息队列已开启");
}catch (Exception e){
System.out.println("消息队列开启失败");
}
}
}
-------------------------------------------------------------
生产者
@Component
public class mqProvider {
@Value("${mq.group}")
String group;
@Value("${mq.topic}")
String topic;
@Value("${mq.tag}")
String mytag;
@Value("${mq.namesraddr}") //ip:端口
String namesraddr;
DefaultMQProducer producer; //mq生产者实例
/**
* 开启消息队列
* @throws MQClientException
*/
public void providerstart() throws MQClientException {
producer = new DefaultMQProducer(group);
producer.setNamesrvAddr(namesraddr); //定义队列地址
producer.start();
}
/**
* 发送消息
* @param name
* @throws InterruptedException
* @throws RemotingException
* @throws MQClientException
* @throws MQBrokerException
*/
public SendResult send(String name) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
SendResult result =null;
try {
Message message = new Message(topic,mytag,name.getBytes());
result = producer.send(message);
result.getMsgId();
System.out.println("生产者--主题topic为"+topic+" 消息已发送="+message);
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}
-----------------------------------------------------------------
消费者
@Component
public class mqConsumer {
@Value("${mq.group}")
String group;
@Value("${mq.topic}")
String topic;
@Value("${mq.tag}")
String tag;
@Value("${mq.namesraddr}") //ip:端口
String namesraddr;
DefaultMQPushConsumer consumer; //mq消费者实例
/**
* 消费消息
*/
public void consumer() throws MQClientException {
consumer =new DefaultMQPushConsumer(group);
consumer.setNamesrvAddr(namesraddr);
//消费模式:一个新的订阅组第一次启动从队列的最后位置开始消费 后续再启动接着上次消费的进度开始消费
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
//订阅主题和二级标题
consumer.subscribe(topic,tag);
//注册消费的监听 并在此监听中消费信息,并返回消费的状态信息
consumer.registerMessageListener((MessageListenerConcurrently) (msgs,context)->{
try {
for (Message msg : msgs) {//获取所有消息
String body = new String(msg.getBody(), "utf-8");
System.out.println("消费者--获取消息-主题topic为"+msg.getTopic()+" 消费消息为="+body);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
consumer.start();
}
}
------------------------------------------------------------------
测试类Controller
@RestController
public class test {
@Autowired
mqProvider provider;
@Autowired
mqConsumer consumer;
/**
* 调用生产者发消息
* @return
*/
@RequestMapping("/send")
public SendResult usemq(@RequestParam("text") String text) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
SendResult result = provider.send(text);
return result;
}
}
访问路径:http://127.0.0.1/send?text=
有返回值说明我们的操作成功了。
现在看看后台打印的吧
看来没问题,我们成功了!!!
在根据id号去可视化界面看下消费记录吧
消息已经被消费了,可以查出来,至此已经实现了本地RocketMq的整合