RabbitMQ 在Spring Boot2.*中的使用

rabbit

介紹

RabbitMQ is the most widely deployed open source message broker
官網地址鏈接
RabbitMQ 是開源消息代理軟件(有時稱為面向消息的中間件),用于實現高級消息隊列協議 (AMQP)。RabaMQ 服務器以 Erlang 編程語言編寫,并基于用于群集和故障轉移的開放電信平臺框架。與代理接口的客戶端庫可用于所有主要編程語言。

安裝方式

使用docker 安裝

rabbitMQ

docker地址

docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management

然后,您可以在瀏覽器中http://localhost:8080轉到http://localhost:8080http://host-ip:8080http://host-ip:8080

localhost:8080

spring boot2

在pom中引入jar

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置文件application.yml

server:
  port: 8081 # 8080已被占用
spring:
  application:
    name: bana-mq
  rabbitmq: #默認即可
    addresses: localhost
    port: 5672
    username: guest #這里是默認賬戶,如果不是使用默認,在這里修改
    password: guest # 同上

隊列

package com.congco.banamq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * created on 19-6-19 下午2:25
 *
 * @author congco
 */
@Configuration
public class RabbitmqConfig {

    @Bean
    public Queue queue(){
        return new Queue("hello");
    }
}

發送者

package com.congco.banamq.demo;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * created on 19-6-19 下午2:29
 *
 * @author congco
 */
@Component
public class HelloSend {

    private final AmqpTemplate amqpTemplate;

    public HelloSend(AmqpTemplate amqpTemplate) {
        this.amqpTemplate = amqpTemplate;
    }


    public void send(){
        String message = "hello"+ LocalDateTime.now();
        System.out.println("Send '"+message+"'");
        this.amqpTemplate.convertAndSend("hello",message);
    }
}

接收者

package com.congco.banamq.receive;

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

/**
 * created on 19-6-19 下午2:32
 *
 * @author congco
 */
@Component
@RabbitListener(queues = "hello")
public class HelloReceive {

    @RabbitHandler
    public void receive(String hello) {
        System.out.println("receive '" + hello + "'");
    }
}

Test

BaseTest.java

package com.congco.banamq.base;

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * created on 19-6-19 下午2:36
 *
 * @author congco
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class BaseTest {
}

RabbitMqTest.java

package com.congco.banamq;

import com.congco.banamq.base.BaseTest;
import com.congco.banamq.demo.HelloSend;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * created on 19-6-19 下午2:34
 *
 * @author congco
 */
public class RabbitMqTest extends BaseTest {


    @Autowired
    private HelloSend helloSend;

    @Test
    public void testSend() {
        helloSend.send();
    }
}

以上是簡單的使用

生產中使用

對象的發送和接收

可以在發送和接收中直接傳遞對象

TopicExchange

配置類

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    
    //聲明隊列
    @Bean
    public Queue queue1() {
        return new Queue("hello.queue1", true); // true表示持久化該隊列
    }
    
    @Bean
    public Queue queue2() {
        return new Queue("hello.queue2", true);
    }
    
    //聲明交互器
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    //綁定
    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(queue1()).to(topicExchange()).with("key.1");
    }
    //`#`匹配規則
    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(queue2()).to(topicExchange()).with("key.#");
    }
}

Fanout Exchange

@Configuration
public class FanoutRabbitConfig {

    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }

}

send

public void send() {
    String context = "hi, fanout msg ";
    System.out.println("Sender : " + context);
    this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
}

result

Sender : hi, fanout msg 
...
fanout Receiver B: hi, fanout msg 
fanout Receiver A  : hi, fanout msg 
fanout Receiver C: hi, fanout msg 

參考地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容