参考文档: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