三、 springboot+RabbitMQ整合

作者: 小疯子 分类: RabbitMQ 发布时间: 2019-03-28 20:47

一、准备工作

  1. 首先是上一篇博文先创建好一个新的用户并打开远程登录
  2. 然后关于对应的端口需要打开(防火墙还需编写)

二、编码开始

(一)先在消息发送端项目

  1. pom.xml中增加如下代码:
    <!-- rabbitmq -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>

  2. application.yml中配置rabbit的相关信息
    spring:
      application:
        name: people-pro 
      rabbitmq:
        host: 192.168.132.129 #你的rabbitmq服务器ip
        port: 5672
        username: feng
        password: 123456
  3. 编写消息队列配置RabbitConfig.java
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @Author: Xiaofeng
     * @Date: 2019/3/22 20:29
     * @Description: rabbitmq配置
     */
    @Configuration
    public class RabbitConfig {
    
        @Bean
        public Queue queue(){
            return new Queue("hello");
        }
    
    }

  4. 编写发送类HelloSender.java
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: Xiaofeng
     * @Date: 2019/3/22 20:40
     * @Description:
     */
    @Component
    public class HelloSender {
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        public void send(){
            rabbitTemplate.convertAndSend("hello", "hello good person");
        }
    
    }
  5. 测试用例类RabbitMqHelloTest.java
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * @Author: Xiaofeng
     * @Date: 2019/3/22 20:45
     * @Description:
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RabbitMqHelloTest {
    
        @Autowired
        private HelloSender helloSender;
    
        @Test
        public void hello() throws Exception {
            helloSender.send();
        }
    }

(二)接收端项目中编写

  1. 前三步骤同上面发送端一致,除了pom.xml中应用名称的配置要更改,同时项目的端口也要改成个新的,其他不变
    server:
      port: 8082
    
    
    spring:
      application:
        name: people-pro1
  2. 然后编写接收端代码类
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: Xiaofeng
     * @Date: 2019/3/22 20:42
     * @Description:
     */
    @Component
    public class HelloReceiver {
    
        @RabbitListener(queues = "hello")
        public void receiveMessage(Message message) {
            System.out.println("Receive:"+message);
        }
    }

(三)运行测试

  1. 先跑接收端的整个项目
  2.  然后跑发送端的测试用例类:RabbitMqHelloTest.java
  3. 最后看结果有没有成功接收

参考链接:https://www.cnblogs.com/hlhdidi/p/6535677.html

0