您好,登錄后才能下訂單哦!
這篇文章主要介紹springboot整合RabbitMQ發(fā)送短信的實(shí)現(xiàn)方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
# 安裝 rpm -ivh erlang-21.3.8.9-1.el7.x86_64.rpm rpm -ivh socat-1.7.3.2-1.el6.lux.x86_64.rpm rpm -ivh rabbitmq-server-3.8.1-1.el7.noarch.rpm # 卸載 #rpm -qa | grep rabbitmq # 啟用管理插件 rabbitmq-plugins enable rabbitmq_management # 啟動(dòng)RabbitMQ systemctl start rabbitmq-server.service #systemctl status rabbitmq-server.service #systemctl restart rabbitmq-server.service #systemctl stop rabbitmq-server.service # 查看進(jìn)程 ps -ef | grep rabbitmq
默認(rèn)的用戶密碼,guest/guest,不允許遠(yuǎn)程連接,需要添加管理員賬戶
# 添加管理員賬號(hào)密碼 rabbitmqctl add_user admin admin # 分配賬號(hào)角色 rabbitmqctl set_user_tags admin administrator # 修改密碼(需要修改一次密碼,否則無(wú)法登錄) rabbitmqctl change_password admin 123456 # 查看用戶列表 rabbitmqctl list_users
使用新賬號(hào)(admin/123456)登錄:http://虛擬機(jī)ip:15672
創(chuàng)建用戶 srbuser,配置管理員權(quán)限
創(chuàng)建虛擬機(jī) /srbhost,在Set permission中為新建的用戶分配訪問(wèn)虛擬機(jī) /srbhost 和 / 的權(quán)限
1、創(chuàng)建模塊
srb下創(chuàng)建rabbit-mq模塊
2、pom
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
3、創(chuàng)建MQConfig
MQConfig.java
package com.indi.srb.rabbitutil.config; @Configuration public class MQConfig { @Bean public MessageConverter messageConverter(){ // json字符串轉(zhuǎn)換器 return new Jackson2JsonMessageConverter(); } }
4、創(chuàng)建MQConst
MQConst.java
package com.indi.srb.rabbitutil.constant; public class MQConst { public static final String EXCHANGE_TOPIC_SMS = "exchange.topic.sms";//交換機(jī) public static final String ROUTING_SMS_ITEM = "routing.sms.item";//路由 public static final String QUEUE_SMS_ITEM = "queue.sms.item";//消息隊(duì)列 }
5、創(chuàng)建MQService
MQService.java
package com.indi.srb.rabbitutil.service; @Service @Slf4j public class MQService { @Resource private AmqpTemplate amqpTemplate; /** * 發(fā)送消息 * @param exchange 交換機(jī) * @param routingKey 路由 * @param message 消息 * @return */ public boolean sendMessage(String exchange,String routingKey, Object message){ log.info("發(fā)送消息"); amqpTemplate.convertAndSend(exchange,routingKey,message); return true; } }
dto
SmsDTO.java
package com.indi.srb.base.dto; @Data @ApiModel(description = "短信") public class SmsDTO { @ApiModelProperty(value = "手機(jī)號(hào)") private String mobile; @ApiModelProperty(value = "消息內(nèi)容") private String message; }
1、pom
<!-- 需要依賴(lài)rabbit-mq --> <dependency> <groupId>com.indi</groupId> <artifactId>rabbit-mq</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
2、yml
application.yaml
#spring: rabbitmq: host: 虛擬機(jī)ip port: 5672 virtual-host: /srbhost username: srbuser password: 123456
3、UserInfoService
UserInfoService.java
// 根據(jù)bindCode獲取手機(jī)號(hào) String getMobileByBindCode(String bindCode);
UserInfoServiceImpl.java
@Override public String getMobileByBindCode(String bindCode) { QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>(); queryWrapper.select("mobile").eq("bind_code",bindCode); UserInfo userInfo = baseMapper.selectOne(queryWrapper); return userInfo.getMobile(); }
4、向MQ發(fā)送消息
UserAccountServiceImpl.java
充值回調(diào)方法的最后添加發(fā)送消息的代碼
@Resource private UserInfoService userInfoService; @Resource private MQService mqService; @Transactional(rollbackFor = Exception.class) @Override public String notify(Map<String, Object> paramMap) { log.info("發(fā)消息"); String mobile = userInfoService.getMobileByBindCode(bindCode); SmsDTO smsDTO = new SmsDTO(); smsDTO.setMobile(mobile); smsDTO.setMessage("充值成功"); mqService.sendMessage(MQConst.EXCHANGE_TOPIC_SMS,MQConst.ROUTING_SMS_ITEM,smsDTO); return "success"; }
1、pom
<!-- 需要依賴(lài)rabbit-mq --> <dependency> <groupId>com.indi</groupId> <artifactId>rabbit-mq</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
2、yml
application.yml
#spring: rabbitmq: host: 虛擬機(jī)ip port: 5672 virtual-host: /srbhost username: srbuser password: 123456
3、創(chuàng)建MQ監(jiān)聽(tīng)器
SmsReceiver.java
package com.indi.srb.sms.receiver; @Component @Slf4j public class SmsReceiver { @Resource private SmsService smsService; // 此處的exchange與key必須與發(fā)消息傳的一致 @RabbitListener(bindings = @QueueBinding( value = @Queue(value = MQConst.QUEUE_SMS_ITEM, durable = "true"), exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS), key = {MQConst.ROUTING_SMS_ITEM} )) public void send(SmsDTO smsDTO) throws IOException { log.info("SmsReceiver 消息監(jiān)聽(tīng)"); Map<String, Object> param = new HashMap<>(); param.put("code", smsDTO.getMessage()); smsService.send(smsDTO.getMobile(), SmsProperties.TEMPLATE_CODE, param); } }
完成充值之后,登錄的用戶,手機(jī)上會(huì)收到充值成功的短信提醒。
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。
以上是“springboot整合RabbitMQ發(fā)送短信的實(shí)現(xiàn)方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。