SpringBoot integra cinco modelos comunes de RabbitMQ

contenido

1. SpringBoot integra el uso de RabbitMQ

1. Introducir dependencias:

2. Haz la configuración: application.yml

3 hola mundo

4.trabajo

5. abanico

6. enrutamiento

7.Modelo de suscripción de temas (modelo de enrutamiento dinámico)


1. SpringBoot integra el uso de RabbitMQ

1. Introducir dependencias:

        <!--引入与rabbitmq集成的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2. Haz la configuración: application.yml

spring:
  application:
    name: rabbitmq-springboot
  rabbitmq:
    host: 192.168.231.141
    port: 5672
    username: ems
    password: 123456
    virtual-host: /ems

3 hola mundo

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
    // 注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    // hello world
    @Test
    public void test() {
        rabbitTemplate.convertAndSend("hello","hello world");
    }
}
// 被工厂扫描道
@Component
// 消费者(声明队列)
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {
    // 代表通过下面方法取消息
    @RabbitHandler
    public void receivel(String message) {
        System.out.println("message = " + message);
    }
}

Pero finalmente fue ejecutado. No veo la cola recién creada en la página web

4.trabajo

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
    // 注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    // work
    @Test
    public void testWork() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","work模型"+i);
        }
    }
}
@Component
public class WorkCustomer {
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel(String message) {
        System.out.println("message1 =" +message);
    }
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel2(String message) {
        System.out.println("message2 =" +message);
    }
}

5. abanico

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
    // 注入rabbitTemplate
    @Resource
    private RabbitTemplate rabbitTemplate;
    // fanout 广播
    @Test
    public void testFanout() {
        rabbitTemplate.convertAndSend("logs","","fanout模型");
    }
@Component
public class FanoutCustomer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    // 直接写@Queue代表创建临时队列
                    value = @Queue,
                    exchange = @Exchange(value = "logs",type = "fanout") //绑定的交换机
            )
    })
    public void receivel(String message) {
        System.out.println("message =" + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    // 直接写@Queue代表创建临时队列
                    value = @Queue,
                    exchange = @Exchange(value = "logs",type = "fanout") //绑定的交换机
            )
    })
    public void receivel2(String message) {
        System.out.println("message2 =" + message);
    }
}

6. enrutamiento

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
    // 注入rabbitTemplate
    @Resource
    private RabbitTemplate rabbitTemplate;
    //route 路由模式
    @Test
    public void testTopic() {
        rabbitTemplate.convertAndSend("directs","info","发送info的key的路由信息");
    }
@Component
public class RouteCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    // 直接写@Queue代表创建临时队列
                    value = @Queue,
                    //绑定的交换机,名称和类型
                    exchange = @Exchange(value = "directs",type = "direct"),
                    key = {"info","error","warn"}
            )
    })
    public void receivel1(String message) {
        System.out.println("message1 =" + message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    // 直接写@Queue代表创建临时队列
                    value = @Queue,
                    //绑定的交换机,名称和类型
                    exchange = @Exchange(value = "directs",type = "direct"),
                    key = {"error"}
            )
    })
    public void receivel2(String message) {
        System.out.println("message2 =" + message);
    }
}

7.Modelo de suscripción de temas (modelo de enrutamiento dinámico)

1. Productor

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
@ComponentScan("com.liziyi")
// @EnableRabbit启用@RabbitListener
@EnableRabbit
public class TestRabbitMQ {
    // 注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //topic 动态路由模式 订阅模式
    @Test
    public void testTopic() {
        rabbitTemplate.convertAndSend("topics","user.save","user.save的路由信息");
    }

2. Consumidores

@Component
public class TopicCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    // 直接写@Queue代表创建临时队列
                    value = @Queue,
                    //绑定的交换机,名称和类型
                    exchange = @Exchange(value = "topics",type = "topic"),
                    key = {"user.save","user.*"}
            )
    })
    public void receivel1(String message) {
        System.out.println("message1 =" + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    // 直接写@Queue代表创建临时队列
                    value = @Queue,
                    //绑定的交换机,名称和类型
                    exchange = @Exchange(value = "topics",type = "topic"),
                    key = {"order.#","produce.#","user.*"}
            )
    })
    public void receivel2(String message) {
        System.out.println("message2 =" + message);
    }
}

Supongo que te gusta

Origin blog.csdn.net/Ciel_Y/article/details/119476266
Recomendado
Clasificación