springboot整合RabbitMQ發送短信的實現
RabbitMQ安裝和運行
# 安裝 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 # 啟動RabbitMQ systemctl start rabbitmq-server.service #systemctl status rabbitmq-server.service #systemctl restart rabbitmq-server.service #systemctl stop rabbitmq-server.service # 查看進程 ps -ef | grep rabbitmq
默認的用戶密碼,guest/guest,不允許遠程連接,需要添加管理員賬戶
# 添加管理員賬號密碼 rabbitmqctl add_user admin admin # 分配賬號角色 rabbitmqctl set_user_tags admin administrator # 修改密碼(需要修改一次密碼,否則無法登錄) rabbitmqctl change_password admin 123456 # 查看用戶列表 rabbitmqctl list_users
使用新賬號(admin/123456)登錄:http://虛擬機ip:15672
MQ服務器設置
創建用戶
創建用戶 srbuser,配置管理員權限
創建虛擬機
創建虛擬機 /srbhost,在Set permission中為新建的用戶分配訪問虛擬機 /srbhost 和 / 的權限
實現發送短信
rabbit-mq
1、創建模塊
srb下創建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、創建MQConfig
MQConfig.java
package com.indi.srb.rabbitutil.config; @Configuration public class MQConfig { @Bean public MessageConverter messageConverter(){ // json字符串轉換器 return new Jackson2JsonMessageConverter(); } }
4、創建MQConst
MQConst.java
package com.indi.srb.rabbitutil.constant; public class MQConst { public static final String EXCHANGE_TOPIC_SMS = "exchange.topic.sms";//交換機 public static final String ROUTING_SMS_ITEM = "routing.sms.item";//路由 public static final String QUEUE_SMS_ITEM = "queue.sms.item";//消息隊列 }
5、創建MQService
MQService.java
package com.indi.srb.rabbitutil.service; @Service @Slf4j public class MQService { @Resource private AmqpTemplate amqpTemplate; /** * 發送消息 * @param exchange 交換機 * @param routingKey 路由 * @param message 消息 * @return */ public boolean sendMessage(String exchange,String routingKey, Object message){ log.info("發送消息"); amqpTemplate.convertAndSend(exchange,routingKey,message); return true; } }
service-base
dto
SmsDTO.java
package com.indi.srb.base.dto; @Data @ApiModel(description = "短信") public class SmsDTO { @ApiModelProperty(value = "手機號") private String mobile; @ApiModelProperty(value = "消息內容") private String message; }
service-core中發送消息
1、pom
<!-- 需要依賴rabbit-mq --> <dependency> <groupId>com.indi</groupId> <artifactId>rabbit-mq</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
2、yml
application.yaml
#spring: rabbitmq: host: 虛擬機ip port: 5672 virtual-host: /srbhost username: srbuser password: 123456
3、UserInfoService
UserInfoService.java
// 根據bindCode獲取手機號 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發送消息
UserAccountServiceImpl.java
充值回調方法的最後添加發送消息的代碼
@Resource private UserInfoService userInfoService; @Resource private MQService mqService; @Transactional(rollbackFor = Exception.class) @Override public String notify(Map<String, Object> paramMap) { log.info("發消息"); 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"; }
service-sms中監聽消息
1、pom
<!-- 需要依賴rabbit-mq --> <dependency> <groupId>com.indi</groupId> <artifactId>rabbit-mq</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
2、yml
application.yml
#spring: rabbitmq: host: 虛擬機ip port: 5672 virtual-host: /srbhost username: srbuser password: 123456
3、創建MQ監聽器
SmsReceiver.java
package com.indi.srb.sms.receiver; @Component @Slf4j public class SmsReceiver { @Resource private SmsService smsService; // 此處的exchange與key必須與發消息傳的一致 @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 消息監聽"); Map<String, Object> param = new HashMap<>(); param.put("code", smsDTO.getMessage()); smsService.send(smsDTO.getMobile(), SmsProperties.TEMPLATE_CODE, param); } }
測試
完成充值之後,登錄的用戶,手機上會收到充值成功的短信提醒。
到此這篇關於springboot整合RabbitMQ發送短信的實現的文章就介紹到這瞭,更多相關springboot RabbitMQ發送短信內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Springboot整合Rabbitmq之Confirm和Return機制
- RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合
- springboot整合消息隊列RabbitMQ
- RabbitMQ開啟SSL與SpringBoot連接測試的配置方法
- springBoot整合rabbitmq測試常用模型小結