阿里云RocketMQ

本文详细介绍了如何在阿里云上配置和使用RocketMQ,包括创建资源、配置Maven依赖、设置配置文件、创建生产者和消费者实例,以及消息的发送和消费。同时提供了生产者发送普通消息、定时消息的示例,以及消费者同步接收和处理消息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

阿里云RocketMQ

1、maven依赖

首先去阿里云控制台创建所需消息队列资源,包括消息队列 RocketMQ 的实例、Topic、Group ID (GID),以及鉴权需要的 AccessKey(AK)。

<dependency>
      <groupId>com.aliyun.openservices</groupId>
      <artifactId>ons-client</artifactId>
      <version>1.8.4.Final</version>
    </dependency>

2、配置文件

##-------鉴权需要的 AccessKey(AK)(实际项目,这里填写阿里云自己的账号信息)---
rocketmq.accessKey=xxAxxxxxxxxxx
rocketmq.secretKey=xxxxxxxxxiHxxxxxxxxxxxxxx## 实例TCP 协议公网接入地址(实际项目,填写自己阿里云MQ的公网地址)
rocketmq.nameSrvAddr=http://MQ_INST_***********85_BbM********************yuncs.com:80
#普通消息topic (实际项目,填写自己阿里云MQ中的topic名称和groupid)
rocketmq.topic=common
rocketmq.groupId=GID-message`在这里插入代码片`
rocketmq.tag=*
#定时/延时消息
rocketmq.timeTopic=time-lapse
rocketmq.timeGroupId=GID-message
rocketmq.timeTag=*

3、配置类

/**
 * MQ配置加载
 * @author xf
 */
@Slf4j
@Component
@Data
@Configuration
@EnableApolloConfig(value = {
   "sysConfig.yml"})
public class MqConfig {
   

    private String accessKey;
    private String secretKey;
    private String nameSrvAddr;
    private String topic;
    private String groupId;
    private String tag;
    private String timeTopic;
    private String timeGroupId;
    private String timeTag;

    public Properties getMqProperties() {
   
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, this.accessKey);
        properties.setProperty(PropertyKeyConst.SecretKey, this.secretKey);
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, this.nameSrvAddr);
        //设置发送超时时间,单位毫秒
        properties.setProperty(PropertyKeyConst.SendMsgTimeoutMillis, "4000");
        return properties;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值