RocketMQ快速上手使用
介绍
基本信息
消息队列(Message Queue,简称 MQ)是构建分布式互联网应用的基础设施,通过 MQ 实现的松耦合架构设计可以提高系统可用性以及可扩展性,是适用于现代应用的最佳设计方案。
使用场景
-
流量削峰
用户的请求,服务器接收后,首先写入消息队列。假如消息队列长度超过最大数量,则直接抛弃用户请求或跳转到错误页面。 -
消息通讯
作为推拉转发的载体 -
应用解耦
-
异步处理
使用
pom依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- 链接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- 适配 IntelliJ IDEA版本和junit版本-->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
yml配置
rocketmq:
name-server: localhost:9876 # rocketMQ服务的地址
producer:
group: cg # 生产者组
server:
port: 8086
消费者
需要提前建好topic
@Component
@RocketMQMessageListener(consumerGroup = "cg", topic = "model1")
public class MyRocketMQListener implements RocketMQListener<HashMap<String, Object>> {
@Override
public void onMessage(HashMap<String, Object> map) {
String name = map.get("name").toString();
String age = map.get("age").toString();
String sex = map.get("sex").toString();
System.out.println("name: " + name + ",age: " + age + ",sex: " + sex);
}
}
生产者
主要有三种生产(发送)方式:oneway,异步,同步
- oneway
@Test
public void oneway() {
// rocketMQTemplate.convertAndSend("model1",map);
rocketMQTemplate.sendOneWay("model1",map);
System.out.println(map);
}
- 异步
/**
* 发送异步消息,不需要等待
*/
@Test
public void testAsyncSend() throws InterruptedException {
/**
* destination: topic
* message: 消息内容
* sendCallback: 回调方法,可以用在做重试补偿机制
* timeout: 超时时间
* timeout: 延迟登记为18个:1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
*/
rocketMQTemplate.asyncSend("model1", map, new
SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println(sendResult);
}
@Override
public void onException(Throwable throwable) {
System.out.println(throwable);
}
});
Thread.sleep(2000);
}
- 同步
/**
* 发送同步消息,需要同时等待
*/
@Test
public void testSyncSend() {
// param1: topic; 若添加tag: topic:tag
// param2: 消息内容
SendResult sendResult = rocketMQTemplate.syncSend("model1", map);
System.out.println(sendResult);
}
- 整体代码
@SpringBootTest(classes = StartApplication.class)
@RunWith(SpringRunner.class)
public class ProducerSendMessageTest {
@Autowired
private RocketMQTemplate rocketMQTemplate;
HashMap<String, Object> map = new HashMap<>();
{
map.put("name","小白4");
map.put("age",22);
map.put("sex","男");
}
/**
* oneway 方式,只管发送,不在意是否成功,日志处理一般这样
*/
@Test
public void oneway() {
// rocketMQTemplate.convertAndSend("model1",map);
rocketMQTemplate.sendOneWay("model1",map);
System.out.println(map);
}
/**
* 发送同步消息,需要同时等待
*/
@Test
public void testSyncSend() {
// param1: topic; 若添加tag: topic:tag
// param2: 消息内容
SendResult sendResult = rocketMQTemplate.syncSend("model1", map);
System.out.println(sendResult);
}
/**
* 发送异步消息,不需要等待
*/
@Test
public void testAsyncSend() throws InterruptedException {
/**
* destination: topic
* message: 消息内容
* sendCallback: 回调方法,可以用在做重试补偿机制
* timeout: 超时时间
* timeout: 延迟登记为18个:1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
*/
rocketMQTemplate.asyncSend("model1", map, new
SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println(sendResult);
}
@Override
public void onException(Throwable throwable) {
System.out.println(throwable);
}
});
Thread.sleep(2000);
}
}