1、Maven依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!-- springboot-web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加springboot对amqp的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <!--fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
2、配置类
import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.FanoutExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; /** * @ClassName RabbitMQConfig * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com * @Version V1.0 **/ @Component public class RabbitMQConfig { /** * 定义交换机 */ private String EXCHANGE_SPRINGBOOT_NAME = "/mayikt_ex"; /** * 短信队列 */ private String FANOUT_SMS_QUEUE = "fanout_sms_queue"; /** * 邮件队列 */ private String FANOUT_EMAIL_QUEUE = "fanout_email_queue"; /** * 配置smsQueue * * @return */ @Bean public Queue smsQueue() { return new Queue(FANOUT_SMS_QUEUE); } /** * 配置emailQueue * * @return */ @Bean public Queue emailQueue() { return new Queue(FANOUT_EMAIL_QUEUE); } /** * 配置fanoutExchange * * @return */ @Bean public FanoutExchange fanoutExchange() { return new FanoutExchange(EXCHANGE_SPRINGBOOT_NAME); } // 绑定交换机 sms @Bean public Binding bindingSmsFanoutExchange(Queue smsQueue, FanoutExchange fanoutExchange) { return BindingBuilder.bind(smsQueue).to(fanoutExchange); } // 绑定交换机 email @Bean public Binding bindingEmailFanoutExchange(Queue emailQueue, FanoutExchange fanoutExchange) { return BindingBuilder.bind(emailQueue).to(fanoutExchange); } }
3、配置文件
spring: rabbitmq: ####连接地址 host: 127.0.0.1 ####端口号 port: 5672 ####账号 username: guest ####密码 password: guest ### 地址 virtual-host: /meiteVirtualHosts
4、生产者
import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class FanoutProducer { @Autowired private AmqpTemplate amqpTemplate; /** * 发送消息 * * @return */ @RequestMapping("/sendMsg") public String sendMsg(String msg) { /** * 1.交换机名称 * 2.路由key名称 * 3.发送内容 */ amqpTemplate.convertAndSend("/mayikt_ex", "", msg); return "success"; } }
5、消费者
import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Slf4j @Component @RabbitListener(queues = "fanout_email_queue") public class FanoutEmailConsumer { @RabbitHandler public void process(String msg) { log.info(">>邮件消费者消息msg:{}<<", msg); } }
6、Entity
import lombok.Data; import org.springframework.stereotype.Component; import java.io.Serializable; @Data public class MsgEntity implements Serializable { private String msgId; private String userId; private String phone; private String email; public MsgEntity(String msgId, String userId, String phone, String email) { this.msgId = msgId; this.userId = userId; this.phone = phone; this.email = email; } }
标签:return,SpringBoot,springframework,RabbitMQ,整合,import,org,public,String From: https://www.cnblogs.com/pinp/p/16797376.html