SpringBoot2.0之整合RabbitMQ

案例:

 

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

  

 

 

 

posted @ 2018-11-12 13:51  toov5  阅读(1842)  评论(0编辑  收藏  举报