欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot整合RabbitMQ发送短信的实现

程序员文章站 2022-04-05 12:38:12
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.r...

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发送短信内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!