没有合适的资源?快使用搜索试试~ 我知道了~
首页浅谈Springboot整合RocketMQ使用心得
浅谈Springboot整合RocketMQ使用心得
11 下载量 31 浏览量
更新于2023-05-11
收藏 106KB PDF 举报
本篇文章主要介绍了Springboot整合RocketMQ使用心得,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
资源详情
资源推荐
浅谈浅谈Springboot整合整合RocketMQ使用心得使用心得
本篇文章主要介绍了Springboot整合RocketMQ使用心得,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
一、阿里云官网一、阿里云官网---帮助文档帮助文档
https://help.aliyun.com/document_detail/29536.html?spm=5176.doc29535.6.555.WWTIUh
按照官网步骤,创建Topic、申请发布(生产者)、申请订阅(消费者)
二、代码二、代码
1、配置:、配置:
public class MqConfig {
/**
* 启动测试之前请替换如下 XXX 为您的配置
*/
public static final String PUBLIC_TOPIC = "test";//公网测试
public static final String PUBLIC_PRODUCER_ID = "PID_SCHEDULER";
public static final String PUBLIC_CONSUMER_ID = "CID_SERVICE";
public static final String ACCESS_KEY = "123";
public static final String SECRET_KEY = "123";
public static final String TAG = "";
public static final String THREAD_NUM = "25";//消费端线程数
/**
* ONSADDR 请根据不同Region进行配置
* 公网测试: http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet
* 公有云生产: http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
* 杭州金融云: http://jbponsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
* 深圳金融云: http://mq4finance-sz.addr.aliyun.com:8080/rocketmq/nsaddr4client-internal
*/
public static final String ONSADDR = "http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal";
}
ONSADDR 阿里云用 公有云生产,测试用公网
不同的业务可以设置不同的tag,但是如果发送消息量大的话,建议新建TOPIC
2、生产者、生产者
方式1:
配置文件:producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="producer" class="com.aliyun.openservices.ons.api.bean.ProducerBean"
init-method="start" destroy-method="shutdown">
<property name="properties">
<map>
<entry key="ProducerId" value="" /> <!-- PID,请替换 -->
<entry key="AccessKey" value="" /> <!-- ACCESS_KEY,请替换 -->
<entry key="SecretKey" value="" /> <!-- SECRET_KEY,请替换 -->
<!--PropertyKeyConst.ONSAddr 请根据不同Region进行配置
公网测试: http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet
公有云生产: http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
杭州金融云: http://jbponsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal
深圳金融云: http://mq4finance-sz.addr.aliyun.com:8080/rocketmq/nsaddr4client-internal -->
<entry key="ONSAddr" value="http://onsaddr-internal.aliyun.com:8080/rocketmq/nsaddr4client-internal"/>
</map>
</property>
</bean>
</beans>
启动方式1,在使用类的全局里设置:
//初始化生产者
private ApplicationContext ctx;
private ProducerBean producer;
@Value("${producerConfig.enabled}")//开关,spring配置项,true为开启,false关闭
private boolean producerConfigEnabled;
@PostConstruct
public void init(){
if (true == producerConfigEnabled) {
ctx = new ClassPathXmlApplicationContext("producer.xml");
producer = (ProducerBean) ctx.getBean("producer");
}
}
PS:最近发现一个坑,如果producer用上面这种方式启动的话,一旦启动的多了,会造成fullGC,所以可以换成下面这种注解方式启动,在用到的地方手动start、shutdown
方式2:配置类(不需要xml)
@Configuration
public class ProducerBeanConfig {
@Value("${openservices.ons.producerBean.producerId}")
private String producerId;
@Value("${openservices.ons.producerBean.accessKey}")
private String accessKey;
@Value("${openservices.ons.producerBean.secretKey}")
private String secretKey;
private ProducerBean producerBean;
@Value("${openservices.ons.producerBean.ONSAddr}")
private String ONSAddr;
@Bean
public ProducerBean oneProducer() {
ProducerBean producerBean = new ProducerBean();
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.ProducerId, producerId);
properties.setProperty(PropertyKeyConst.AccessKey, accessKey);
properties.setProperty(PropertyKeyConst.SecretKey, secretKey);
properties.setProperty(PropertyKeyConst.ONSAddr, ONSAddr);
producerBean.setProperties(properties);
return producerBean;
}
}
PS:经过这次双11发现,以上2种方式在大数据量,多线程情况下都不太适用, 性能很差,所以推荐用3
方式3:(不需要xml)
weixin_38657848
- 粉丝: 5
- 资源: 906
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功