文章目录
- 一、安装
- 1.1、下载RocketMQ
- 1.2、将下载包上传到linux中,然后解压
- 1.3、修改runserver.sh的jvm参数大小(根据自己服务器配置来修改)
- 1.4、启动mqnamesrv (类似于注册中心)
- 1.5、修改runbroker.sh的jvm参数大小(根据自己服务器配置来修改)
- 1.6、修改broker.conf
- 1.7、启动broker(节点)
- 二、springboot中的使用
- 2.1、pom.xml中添加依赖
- 2.2、application.yml配置
- 2.3、生产者模板类
- 2.4、消费者模板类
- 2.5、消息调用测试
- 2.6、成功案例
- 前言:rocketmq-all-5.3.0-bin-release.zip版本为例
一、安装
1.1、下载RocketMQ
下载地址:https://rocketmq.apache.org/
1.2、将下载包上传到linux中,然后解压
- 解压命令
unzip rocketmq-all-5.3.0-bin-release.zip
1.3、修改runserver.sh的jvm参数大小(根据自己服务器配置来修改)
# 在rocketmq-all-5.3.0-bin-release/bin下修改
vim runserver.sh# 修改jvm大小,如果服务器支持这么大的可以不用修改
# 将以下
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
# 修改为
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
1.4、启动mqnamesrv (类似于注册中心)
# 在rocketmq-all-5.3.0-bin-release/bin中启动
nohup ./mqnamesrv &
1.5、修改runbroker.sh的jvm参数大小(根据自己服务器配置来修改)
# 在rocketmq-all-5.3.0-bin-release/bin下修改
vim runbroker.sh# 修改jvm大小,如果服务器支持这么大的可以不用修改
# 将以下
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g"
# 修改为
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m"
1.6、修改broker.conf
# 在rocketmq-all-5.3.0-bin-release/conf下修改
vim broker.conf# 将以下参数加在文件最下方
# 自动创建topic
autoCreateTopicEnable = true
# namesrv注册地址
namesrvAddr=localhost:9876
1.7、启动broker(节点)
# 在rocketmq-all-5.3.0-bin-release/bin中启动
nohup ./mqbroker -c ../conf/broker.conf &
二、springboot中的使用
2.1、pom.xml中添加依赖
<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.1.1</version>
</dependency>
2.2、application.yml配置
rocketmq:name-server: 192.168.103.81:9876producer:group: my-consumer_testconsumer:group: my-consumer_test
2.3、生产者模板类
package com.yhgc.rocket;
import org.apache.rocketmq.spring.annotation.ExtRocketMQTemplateConfiguration;
import org.apache.rocketmq.spring.core.RocketMQTemplate;/*** @author 王青松*/
@ExtRocketMQTemplateConfiguration(nameServer = "${rocketmq.name-server}")
public class ExtRocketMQTemplate extends RocketMQTemplate {
}
2.4、消费者模板类
package com.yhgc.rocket;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;
@Service
@RocketMQMessageListener(topic = "testTopic", consumerGroup = "my-consumer_test")
public class TestConsumer implements RocketMQListener<String> {@Overridepublic void onMessage(String s) {System.out.println("============Received message: " + s);}
}
2.5、消息调用测试
package com.yhgc.controller;
import com.alibaba.fastjson.JSONObject;
import com.yhgc.rocket.ExtRocketMQTemplate;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;@RestController
@RequestMapping("/test1")
public class TestController {@Autowiredprivate ExtRocketMQTemplate rocketMQTemplate;@RequestMapping("/t2")@ResponseBodypublic JSONObject t2(){rocketMQTemplate.convertAndSend("testTopic", "Hello, RocketMQ!");JSONObject res = new JSONObject();res.put("code",200);res.put("msg","调用成功");return res;}
}