栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot集成rabbitmq发送消息

SpringBoot集成rabbitmq发送消息

环境:jdk11,springboot2.5.5

以下代码以路由模式(Routing)为示例。

1. pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    spring-rabbitmq-demo
    0.0.1-SNAPSHOT
    spring-rabbitmq-demo
    Demo project for Spring Boot
    
        11
    
    
        
            org.springframework.boot
            spring-boot-starter-amqp
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.amqp
            spring-rabbit-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2. application.yml

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: 123456
    virtual-host: /

3. 配置队列,交换机,以及绑定关系

@Configuration
public class RabbitMQConfiguration {


    // 创建队列
    @Bean
    public Queue orderQueue() {
        
        return new Queue("order-queue", true, false, false, null);
    }

    @Bean
    public Queue courseQueue() {
        
        return new Queue("course-queue", true, false, false, null);
    }

    // 创建交换机
    @Bean
    public DirectExchange directExchange() {
        // 交换机名称,是否持久化,是否自动删除,附加参数
        return new DirectExchange("direct-exchange", true, false, null);
    }

    // 绑定交换机和队列
    @Bean
    public Binding bindingDirect1() {
        return BindingBuilder.bind(orderQueue()).to(directExchange()).with("order");
    }

    @Bean
    public Binding bindingDirect2() {
        return BindingBuilder.bind(courseQueue()).to(directExchange()).with("course");
    }
}

4. 配置消费者监听

@Service
public class ConsumerService {

    @RabbitListener(queues = "order-queue")
    public void orderConsumer(Message message) {
        System.out.println("orderConsumer收到消息: " + new String(message.getBody(), StandardCharsets.UTF_8));
    }

    @RabbitListener(queues = "course-queue")
    public void courseConsumer(Message message) {
        System.out.println("courseConsumer收到消息: " + new String(message.getBody(), StandardCharsets.UTF_8));
    }
}

 5. 编写测试用例发送消息

@SpringBootTest
class SpringRabbitmqDemoApplicationTests {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void sendMessage1() {
        rabbitTemplate.convertAndSend("direct-exchange","order","hello order");
    }

    @Test
    void sendMessage2() {
        rabbitTemplate.convertAndSend("direct-exchange","course","hello course");
    }

}

6. SpringBoot启动类,启动项目

@SpringBootApplication
public class SpringRabbitmqDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringRabbitmqDemoApplication.class, args);
    }

}

7. 运行测试类发送消息,进行测试 

 完整代码

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300796.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号