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

Springboot整合RabbitMQ实现直连模型

Springboot整合RabbitMQ实现直连模型

引入依赖

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

编辑配置文件
spring:
  rabbitmq:
    host: 106.15.73.43
    port: 5672
    username: admin
    password: 123
    virtual-host: /rabbitmq_zhj
代码结构

生产者
package com.example.newrabbitmq;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
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;

@SpringBootTest( classes = NewRabbitMqApplication.class)
@RunWith( SpringRunner.class )
class NewRabbitMqApplicationTests {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 直连模式
    @Test
    void contextLoads() {
        rabbitTemplate.convertAndSend( "springboot_hello", "spring整合的直连模式" );
    }

}
消费者
package com.example.newrabbitmq.serive;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener( queuesToDeclare = @Queue("springboot_hello") )
public class Customer {


    @RabbitHandler
    public void test1(String s){
        System.out.println(s);
    }

}
运行截图


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

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

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