阿里云RocketMQ使用

本文档介绍了如何在阿里云上使用RocketMQ,包括Maven依赖的引入、阿里云平台上的Topic创建、权限设置、GroupID与用户Key获取,以及详细展示了生产者和消费者的配置与实现代码,提供了一个发送消息的测试示例。
摘要由CSDN通过智能技术生成

参考文档:https://help.aliyun.com/product/29530.html?spm=a2c4g.11186623.6.540.3cc87b4arGmtrs

一、Maven引入

<!--消息队列 RocketMQ-->
<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>ons-client</artifactId>
    <version>1.7.9.Final</version>
</dependency>

二、登录阿里云创建topic,授权,创建groupID,获取用户key

三、代码

1、生产者配置类

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;

@Configuration
@EnableJms
public class AliyunMqProducerConfig {

	@Value("${aliyun.mq.accessKey}")
	private String accessKey;

	@Value("${aliyun.mq.secretKey}")
	private String secretKey;

	@Value("${aliyun.mq.namesrvAddr}")
	private String namesrvAddr;

	@Bean(initMethod = "start", destroyMethod = "shutdown")
	public ProducerBean producerBean() {
		Properties properties = new Properties();
		// 鉴权用 AccessKey,在阿里云服务器管理控制台创建
		properties.put(PropertyKeyConst.AccessKey, accessKey);
		// 鉴权用 SecretKey,在阿里云服务器管理控制台创建
		properties.put(PropertyKeyConst.SecretKey, secretKey);
		// 设置 TCP 接入域名(此处以公共云的公网接入为例)
		properties.put(PropertyKeyConst.NAMESRV_ADDR, namesrvAddr);
		properties.put(PropertyKeyConst.SendMsgTimeoutMillis, "3000");

		ProducerBean producerBean = new ProducerBean();
		producerBean.setProperties(properties);
		return producerBean;
	}
}

2、生产者类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyun.openservi
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值