springboot整合RabbitMQ发送短信的实现

来自:互联网
时间:2021-06-28
阅读:
免费资源网 - https://freexyz.cn/

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,配置管理员权限

springboot整合RabbitMQ发送短信的实现

创建虚拟机

创建虚拟机 /srbhost,在Set permission中为新建的用户分配访问虚拟机 /srbhost 和 / 的权限

springboot整合RabbitMQ发送短信的实现

springboot整合RabbitMQ发送短信的实现

springboot整合RabbitMQ发送短信的实现

实现发送短信

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发送短信的实现

springboot整合RabbitMQ发送短信的实现

到此这篇关于springboot整合RabbitMQ发送短信的实现的文章就介绍到这了,更多相关springboot RabbitMQ发送短信内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

免费资源网 - https://freexyz.cn/
返回顶部
顶部