SpringBoot uses RabbitMQ

Setting up the initial environment

Introduction of depend on

    <! -- RabbitMQ integration dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
Copy the code

Configuration Configuration file

server:
  port: 9090

spring:
  application:
    # Microservices make sense, form a good habit, write it out first
    name: rabbitmq-02-springboot
  rabbitmq:
    host: 127.0. 01.
    port: 5672
    username: codingce
    password: 123456
    virtual-host: /codingce
Copy the code

Test class to inject rabbitTemplate

    / / injection rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
Copy the code

Five message models for Message queue RabbitMQ

The first direct connection model is used

Development producer

    /** * direct connection */
    @Test
    void contextLoads(a) {
        rabbitTemplate.convertAndSend("hello"."hello word");
    }
Copy the code

Develop consumers

/ * * *@author mxz
 */
@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {

    / * * *@param message
     */
    @RabbitHandler
    public void receivel(String message) {
        System.out.println("message: "+ message); }}Copy the code

The second work model is used

Development producer

    /** * work */
    @Test
    void testWork(a) {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work"."The work model"); }}Copy the code

Develop consumers

/** * The second model works **@author mxz
 */
@Component
public class WorkCustomer {
    /** * The first consumer **@param message
     */
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel(String message) {
        System.out.println("message1 = " + message);
    }

    /** * 2nd consumer **@param message
     */
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel2(String message) {
        System.out.println("message2 = "+ message); }}Copy the code

The third Fanout broadcast model

Development producer

    /** * fanout broadcast */
    @Test
    void testFanout(a) {
        rabbitTemplate.convertAndSend("logs".""."Messages sent by Fanout model");
    }
Copy the code

Develop consumers

/**
 * fanout
 *
 * @author mxz
 */
@Component
public class FanoutCustomer {

    @rabbitListener (bindings = {@queuebinding (value = @queue), Type = "fanout") // Bound switch)})
    public void receivel(String message) {
        System.out.println("message1 = " + message);
    }

    @rabbitListener (bindings = {@queuebinding (value = @queue), Type = "fanout") // Bound switch)})
    public void receive2(String message) {
        System.out.println("message2 = "+ message); }}Copy the code

The fourth Route routing model

Development producer

    /** * route */
    @Test
    void testRoute(a) {
        // Exchange Switch name
        rabbitTemplate.convertAndSend("directs"."info"."Info key routing message");
    }
Copy the code

Develop consumers

/ * * *@author mxz
 */
@Component
public class RouteCustomer {

    @queuebinding (bindings = {@queuebinding (value = "post"), Type = "direct"), / / custom switch name and type the key = {" info ", "error", "warn"})})
    public void receivel(String message) {
        System.out.println("message1 = " + message);
    }

    @queuebinding (bindings = {@queuebinding (value = "post"), Type = "direct"), // Customize switch name and type key = {"error"})
    public void receivel2(String message) {
        System.out.println("message1 = "+ message); }}Copy the code

Fifth Topic subscription model (dynamic routing model)

Development producer

    /** * topic subscription mode dynamic routing */
    @Test
    void testTopic(a) {
        rabbitTemplate.convertAndSend("topics"."user.save"."User.save Routing message");
    }
Copy the code

Develop consumers

/** * Subscription model **@author mxz
 */
@Component
public class TopicCustomer {

    @RabbitListener(bindings = { @QueueBinding( value = @Queue, exchange = @Exchange(type = "topic", name = "topics"), key = {"user.save", "user.*"} ) } )
    public void receivel(String message) {
        System.out.println("message1" + message);
    }

    @RabbitListener(bindings = { @QueueBinding( value = @Queue, exchange = @Exchange(type = "topic", name = "topics"), key = {"produce.#", "order.#"} ) } )
    public void receivel2(String message) {
        System.out.println("message2"+ message); }}Copy the code

The article has been uploaded to gitee gitee.com/codingce/he… Project Address: github: github.com/xzMhehe/cod…