SpringBoot集成Rabbitmq配置类版

SpringBoot集成Rabbitmq配置类版

consumer(消费者模块)

项目结构图

SpringBoot集成Rabbitmq配置类版_第1张图片

配置文件

  1. pom.xml

    
    <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">
        <parent>
            <artifactId>xc-framework-parentartifactId>
            <groupId>com.xuechenggroupId>
            <version>1.0-SNAPSHOTversion>
            <relativePath>../xc-framework-parent/pom.xmlrelativePath>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>test-rabbitmq-consumerartifactId>
    
        <dependencies>
            
            
                
                
                
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-loggingartifactId>
            dependency>
        dependencies>
    
    project>
    
  2. application.yml

    server:
      port: 45000
    spring:
      application:
        name: test-consumer-consumer
      rabbitmq:
        host: 127.0.0.1  # 安装rabbitmq的ip
        username: guest # rabbitmq的账号
        password: guest # rabbitmq的密码
        virtual-host: / # 虚拟主机
    

配置类

  1. RabbitmqConfig

    package com.xuecheng.test.consumer.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2020/11/9 下午5:17
     * @Description: TODO
     */
    @Configuration
    public class RabbitmqConfig {
    
        public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    
        public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    
        public static final String EXCHANGE_TOPIC_INFORM = "exchange_topic_inform";
    
        public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
    
        public static final String ROUTINGKEY_SMS = "inform.#.sms.#";
    
    
        // 声明交换机
        @Bean(EXCHANGE_TOPIC_INFORM)
        public Exchange EXCHANGE_TOPIC_INFORM(){
            // durable(true) 持久化, mq重启之后交换机还在
            return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
        }
    
        // 声明QUEUE_INFORM_EMAIL队列
        @Bean(QUEUE_INFORM_EMAIL)
        public Queue QUEUE_INFORM_EMAIL(){
            return new Queue(QUEUE_INFORM_EMAIL);
        }
    
        // 声明QUEUE_INFORM_SMS队列
        @Bean(QUEUE_INFORM_SMS)
        public Queue QUEUE_INFORM_SMS(){
            return new Queue(QUEUE_INFORM_SMS);
        }
    
        // ROUTINGKEY_EMAIL队列绑定交换机, 指定routingKey
        // @Qualifier注解给类成员变量使用时需要依赖@Autowired, 但是给方法参数可以单独使用
        @Bean
        public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue, @Qualifier (EXCHANGE_TOPIC_INFORM) Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
        }
    
        // ROUTINGKEY_SMS队列绑定交换机, 指定routingKey
        // @Qualifier注解给类成员变量使用时需要依赖@Autowired, 但是给方法参数可以单独使用
        @Bean
        public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue, @Qualifier (EXCHANGE_TOPIC_INFORM) Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
        }
    }
    

mq消息监听类

  1. ReceiveHandler

    package com.xuecheng.test.consumer.mq;
    
    import com.xuecheng.test.consumer.config.RabbitmqConfig;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2020/11/9 下午5:32
     * @Description: TODO
     */
    @Component
    public class ReceiveHandler {
    
        @RabbitListener(queues = {RabbitmqConfig.QUEUE_INFORM_EMAIL})
        public void accept_email(String msg, Message message) {
            System.out.println(msg);
        }
    
    }
    

启动类

  1. ConsumerApplication

    package com.xuecheng.test.consumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2020/11/9 下午5:47
     * @Description: TODO
     */
    @SpringBootApplication
    public class ConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    
    }
    

producer(生产者模块)

项目结构图

SpringBoot集成Rabbitmq配置类版_第2张图片

配置文件

  1. pom.xml

    
    <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">
        <parent>
            <artifactId>xc-framework-parentartifactId>
            <groupId>com.xuechenggroupId>
            <version>1.0-SNAPSHOTversion>
            <relativePath>../xc-framework-parent/pom.xmlrelativePath>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>test-rabbitmq-producerartifactId>
    
        <dependencies>
            
            
                
                
                
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-amqpartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-loggingartifactId>
            dependency>
        dependencies>
    
    project>
    
  2. application.yml

    server:
      port: 44000
    spring:
      application:
        name: test-producer-producer
      rabbitmq:
        host: 127.0.0.1  # 安装rabbitmq的ip
        username: guest # rabbitmq的账号
        password: guest # rabbitmq的密码
        virtual-host: / # 虚拟主机
    

配置类

  1. RabbitmqConfig

    package com.xuecheng.test.producer.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2020/11/9 下午5:17
     * @Description: TODO
     */
    @Configuration
    public class RabbitmqConfig {
    
        public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    
        public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    
        public static final String EXCHANGE_TOPIC_INFORM = "exchange_topic_inform";
    
        public static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
    
        public static final String ROUTINGKEY_SMS = "inform.#.sms.#";
    
    
        // 声明交换机
        @Bean(EXCHANGE_TOPIC_INFORM)
        public Exchange EXCHANGE_TOPIC_INFORM(){
            // durable(true) 持久化, mq重启之后交换机还在
            return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
        }
    
        // 声明QUEUE_INFORM_EMAIL队列
        @Bean(QUEUE_INFORM_EMAIL)
        public Queue QUEUE_INFORM_EMAIL(){
            return new Queue(QUEUE_INFORM_EMAIL);
        }
    
        // 声明QUEUE_INFORM_SMS队列
        @Bean(QUEUE_INFORM_SMS)
        public Queue QUEUE_INFORM_SMS(){
            return new Queue(QUEUE_INFORM_SMS);
        }
    
        // ROUTINGKEY_EMAIL队列绑定交换机, 指定routingKey
        // @Qualifier注解给类成员变量使用时需要依赖@Autowired, 但是给方法参数可以单独使用
        @Bean
        public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue, @Qualifier (EXCHANGE_TOPIC_INFORM) Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
        }
    
        // ROUTINGKEY_SMS队列绑定交换机, 指定routingKey
        // @Qualifier注解给类成员变量使用时需要依赖@Autowired, 但是给方法参数可以单独使用
        @Bean
        public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue, @Qualifier (EXCHANGE_TOPIC_INFORM) Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
        }
    }
    

启动类

  1. ProducerApplication

    package com.xuecheng.test.producer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2020/11/8 下午10:05
     * @Description: TODO
     */
    @SpringBootApplication
    public class ProducerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class, args);
        }
    
    }
    

测试–生产消息

  1. Producer05_topics_springboot

    package com.xuecheng.test.producer;
    
    import com.xuecheng.test.producer.config.RabbitmqConfig;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * @Author: 潇哥
     * @DateTime: 2020/11/9 下午5:25
     * @Description: TODO
     */
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class Producer05_topics_springboot {
    
        // 这两个模板都能生产消息
    
        // 构建消息
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        // 构建消息
        @Autowired
        private AmqpTemplate amqpTemplate;
    
        @Test
        public void testSendEmail() {
            String message = "send email message to user";
    
            // 生产消息方式一
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPIC_INFORM, "inform.email", message);
    
            // 生产消息方式二
            amqpTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPIC_INFORM, "inform.email", message);
        }
    
    }
    

你可能感兴趣的:(#,JAVA-SpringBoot,#,rabbitmq,spring,boot)