案例:

Springboot 对RabbitMQ的支持

公共的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.toov5</groupId>
<artifactId>RabbitMQProject</artifactId>
<version>0.0.1-SNAPSHOT</version> <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>
</dependencies> </project>

Producer: 

 controller

package com.toov5.rabbitMQ.comtroller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.toov5.rabbitMQ.Producer.FanoutProducer; @RestController
public class MemberProcuderController {
@Autowired
private FanoutProducer fanoutProducer; @RequestMapping("/sendMsg")
public String sendMsg(String queueName) {
fanoutProducer.send(queueName);
return "success";
}
}

config

package com.toov5.rabbitMQ.config;

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; @Component
public class FanoutConfig {
// 邮件队列
private String FANOUT_EMAIL_QUEUE = "fanout_eamil_queue"; // 短信队列
private String FANOUT_SMS_QUEUE = "fanout_sms_queue";
// 短信队列
private String EXCHANGE_NAME = "fanoutExchange"; // 定义队列
//邮件队列
@Bean
public Queue fanoutEmailQueue() {
return new Queue(FANOUT_EMAIL_QUEUE);
}
//短信队列
@Bean
public Queue fanoutSMSQueue() {
return new Queue(FANOUT_SMS_QUEUE);
}
//定义交换机
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange(EXCHANGE_NAME);
}
//队列和交换机绑定 参数名称和定义好的方法名称一致
@Bean
Binding bindingExchangeEamil(Queue fanoutEmailQueue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutEmailQueue).to(fanoutExchange);
} @Bean
Binding bindingExchangeSMS(Queue fanoutSMSQueue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutSMSQueue).to(fanoutExchange);
} }

producer

package com.toov5.rabbitMQ.Producer;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class FanoutProducer {
@Autowired
private AmqpTemplate amqpTemplate; public void send(String queueName) {
System.out.println("queueName"+queueName);
String mString="msg"+new Date();
amqpTemplate.convertAndSend(queueName,mString); //发送消息
} }

yml:

spring:
rabbitmq:
####连接地址
host: 192.168.91.6
####端口号
port: 5672
####账号
username: admin
####密码
password: admin
### 地址 主机独立的virtualhost
virtual-host: /admin_toov5
server:
port: 8081

 启动类:

 

package com.toov5.rabbitMQ;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class AppMemberProducer {
public static void main(String[] args) {
SpringApplication.run(AppMemberProducer.class, args);
}
}

Producer启动时候不会创建这个交换机哦,懒加载

启动后:

Consumer:

EmailConsumer:

package com.toov5.msg.email;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component //注册到Spring 容器里面
@RabbitListener(queues="fanout_eamil_queue") //监听
public class EmailConsumer { @RabbitHandler //表示接收消息
public void process(String mString) {
System.out.println("邮件消费者获取生产者消息msg"+mString);
}
}

SMSConsumer:

package com.toov5.msg.SMS;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; @Component //注册到Spring 容器里面
@RabbitListener(queues="fanout_sms_queue") //监听
public class SMSConsumer { @RabbitHandler //表示接收消息
public void process(String mString) {
System.out.println("短信消费者获取生产者消息msg"+mString);
}
}

启动类:

package com.toov5.msg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class AppMsgConsumer {
public static void main(String[] args) {
SpringApplication.run(AppMsgConsumer.class, args);
}
}

yml:

spring:
rabbitmq:
####连接地址
host: 192.168.91.6
####端口号
port: 5672
####账号
username: admin
####密码
password: admin
### 地址 主机独立的virtualhost
virtual-host: /admin_toov5

SpringBoot2.0之整合RabbitMQ的更多相关文章

  1. SpringBoot2.0之整合Kafka

    maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

  2. SpringBoot2.0之整合Apollo

    Spring Boot客户端对接阿波罗服务器端 核心源码都在这个压缩包里面 封装好了环境 运行shell脚本就ok了 下面进入到本地maven仓库: 远程仓库apollo的jar包 只能打包到本地或者 ...

  3. SpringBoot2.0之整合ElasticSearch

    就类比数据库到时候去实现 服务器端配置 集群名字  与yml名字一致 pom: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...

  4. SpringBoot2.0之整合ActiveMQ(发布订阅模式)

    发布订阅模式与前面的点对点模式很类似,简直一毛一样 注意:发布订阅模式 先启动消费者 公用pom: <project xmlns="http://maven.apache.org/PO ...

  5. SpringBoot2.0之整合ActiveMQ(点对点模式)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. SpringBoot2.0之整合Dubbo

    Dubbo支持协议 Dubbo支持dubbo.rmi.hessian.http.webservice.thrift.redis等多种协议,但是Dubbo官网是推荐我们使用Dubbo协议的. Sprin ...

  7. 基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

    SpringBoot 整合 Mybatis 有两种常用的方式,一种就是我们常见的 xml 的方式 ,还有一种是全注解的方式.我觉得这两者没有谁比谁好,在 SQL 语句不太长的情况下,我觉得全注解的方式 ...

  8. springboot2.0 Mybatis 整合

    原文:https://blog.csdn.net/Winter_chen001/article/details/80010967 环境/版本一览: 开发工具:Intellij IDEA 2017.1. ...

  9. 每天学点SpringCloud(一):使用SpringBoot2.0.3整合SpringCloud

    最近开始学习SpringCloud,在此把我学习的过程记录起来,跟大家分享一下,一起学习.想学习SpringCloud的同学赶快上车吧. 本次学习使用得SpringBoot版本为2.0.3.RELEA ...

随机推荐

  1. PSSM特征-从生成到处理

    以下代码均为个人原创,如有疑问,欢迎交流.新浪微博:拾毅者 本节内容: pssm生成 pssm简化 标准的pssm构建 滑动pssm生成 在基于蛋白质序列的相关预測中.使用PSSM打分矩阵会得将预測效 ...

  2. lua学习笔记(十三)

    math库     定义在math中     所有三角函数都使用弧度     指数和对数函数     取整函数     伪随机数math.random         调用时没有参数返回0~1之间的随 ...

  3. 动态注册HttpModule管道,实现global.asax功能

    1.所用类库有 Microsoft.Web.Infrastructure.dll 和WebActivator.dll 2.类代码如下 using System; using System.Collec ...

  4. Rocchio算法

    一.引子 查询扩展(Query Expansion)是信息检索领域的一个重要话题. 一方面.用户本身可能会出错,他会输入一些错别字,比方把"冯小刚",错写成"冯晓刚&qu ...

  5. 大师养成计划之二:hibernate框架的使用------实例演示

    搭建hibernate项目框架的步骤: 一.导入jar包 二.new    .cfg.xml配置文件 <?xml version="1.0" encoding="U ...

  6. Ubuntu配置apache2.4配置虚拟主机遇到的问题

    update: 偶然看到了 apache的更新说明,直接贴个地址过来吧. http://httpd.apache.org/docs/2.4/upgrading.html 最近想把web开发目录从/va ...

  7. The Pilots Brothers' refrigerator - poj 2965

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   Special Judg ...

  8. 我在面试.NET/C#程序员时会提出的问题(转载)

    转自:http://blog.zhaojie.me/2011/03/my-interview-questions-for-dotnet-programmers.html 说起来我也面试过相当数量的.N ...

  9. 微信小程序事件

    微信小程序事件1.什么是事件2.事件类别3.事件冒泡4.事件绑定5.事件对象详解笔记:1.事件是一种用户的行为,是一种通讯方式.2.事件类别:    点击事件:tap    长按事件:longtap  ...

  10. Android设备各种使用尺寸整理

    // 获取屏幕的宽度.高度 Display defDip = getWindowManager().getDefaultDisplay(); int disWidth = defDip.getWidt ...